Archive for January, 2008
January 21, 2008 | rails
Recently I wanted to create a mixin for String in my Rails 2.0 project that allowed me to DRY up pluralization given an arbitrary number. Here's an example usage:
[sourcecode lang="ruby"]
"mile".pluralize_for(trip.miles) #returns "mile" when trip.miles is one and "miles" otherwise.
[/sourcecode]
The mixin code is simple:
[sourcecode lang="ruby"]
# martian_extensions.rb
module MartianExtensions
def pluralize_for(number)
unless number == 1
self.to_s.pluralize
else
self.to_s
end
end
end
[/sourcecode]
The challenge came with finding a simple, configuration-free way to ensure that this mixin is loaded. Following the guidance of Jamis Buck in this (by now ancient) blog article, i eventually settled on his solution of creating my simple mixin as a full-fledged plugin.
In order to do this without adding any configuration, you can follow Rails' automagic loading rules, namely, create an apt folder structure under your vendor/plugins directory:
/vendor/plugins/martian_extensions/
/vendor/plugins/martian_extensions/init.rb
/vendor/plugins/martian_extensions/lib
/vendor/plugins/martian_extensions/lib/martian_extensions.rb
You've already seen martian_extensions.rb, as for init.rb, that's what does the work of ensuring String loads my module and includes pluralize_for:
[sourcecode lang="ruby"]
#init.rb
String.send :include, MartianExtensions
[/sourcecode]
Personally, I'll keep my core extensions in this single plugin so that I can keep it portable between my applications. It seems pretty clean to me, but if there's a better solution that you have found, please comment!
No Comments »
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.
No Comments »