Arriving at a DSL for XPath Testing in Rails

February 17, 2008 | code, rails, ruby, testing, xml

Recently I was writing custom Xml exporting for some of our Rails models. I favor TDD over just shoot-from-the-hip development, so I had to write some tests. For testing the validity of the XML, I chose XPath for its simplicity. As I added functionality (and thereby added tests) I noticed that my tests were becoming increasingly redundant, and that Test::Unit's syntax was making my test files ugly and more importantly hard to read. As I started DRY-ing up the individual tests and trying to find better ways to share code and make the tests more succinct, I noticed that I was driving very close to creating my own mini-DSL for testing Xml with Xpath. My Requirements for Xml Exporting A required element must always exist in the resulting Xml regardless of the model's state An optional element: Must exist when the model meets a certain state condition (i.e. the member is not null) Must not exist when that condition is not met A DSL That Meets My Testing Needs As mentioned earlier, this DSL evolved from repeated refactoring my tests in order to make them more clear. Here is an example of an XPath-based Xml test: [sourcecode lang='ruby'] xpath_tests_for @video_library do # ensure that the Xml always contains: #

Disable Globalize Translate() to Reduce Log Output

January 8, 2008 | rails

If you're using globalize to translate or localize your rails site you'll have noticed that your log quickly fills up with messages like the following (especially in the devlopment environment): [sourcecode language='sql'] Globalize::Language Columns (0.000516) SHOW FIELDS FROM globalize_languages Globalize::Language Load (0.000202) SELECT * FROM globalize_languages WHERE (globalize_languages.`rfc_3066` = 'en-us') LIMIT 1 Globalize::Language Load (0.000131) SELECT * FROM globalize_languages WHERE (globalize_languages.`iso_639_1` = 'en') LIMIT 1 Globalize::Country Columns (0.000383) SHOW FIELDS FROM globalize_countries Globalize::Country Load (0.000152) SELECT * FROM globalize_countries WHERE (globalize_countries.`code` = 'us') LIMIT 1 [/sourcecode] We do a lot of view text translation in our templates, a lot like the following: [sourcecode lang='ruby'] [/sourcecode] And over time, you'll end up with tons of .translate calls, each one causing extra log cruft. If you're developing a feature that doesn't require your localization to be enabled, and you want to skim down the globalize log cruft from this type of translation, here's a simple fix to softly disable the extensions to String Edit vendor/plugins/globalize/lib/gloablize/localization/core_ext.rb and make it look like the following: [sourcecode lang='ruby'] def translate(default = nil, arg = nil, namespace = nil) # Locale.translate(self, default, arg, namespace) self.to_s end [/sourcecode] You can switch the comment from line #2 to line#3 to re-enable translate easily. Note: make sure to restart your server to ensure this takes effect.