Attribute Localizer

I like to keep my Active Record models as strict as possible but I also like the further layer of protection/restriction setting database columns to not allow NULL adds. Normalizing to nil helps enforce this better by not letting ’’ slip through the cracks and I can still prevent those who insist on direct DB access from entering in shitty data as much as possible.

Install as a Ruby gem

The attribute_localizer gem is hosted over at Gemcutter

Install the Attribute Localizer gem

sudo gem install attribute_localizer

Install for Ruby on Rails

# config/environement.rb
  config.gem 'attribute_localizer'

Usage

This is eager loaded into Active Record. It is usable inside of other ruby classes outside of ActiveRecord by just including the module AttributeLocalizer.


class Klass < ActiveRecord::Base

  # Can take an array of attributes if you want
  localize_attributes :name, :description

end

object = Klass.new
object.locale = :fr
object.update_attributes(:name => 'french_name', :description => 'french_description')
object.locale = :en
object.update_attributes(:name => 'english_name', :description => 'english_description')
object.reload

object.locale = nil
object.name # => Will us the Rails I18n.local that has been set at the application level in your controller or I18n.default_locale
object.locale = :fr
object.name # => Will return the french name translation pulled from 'config/locales/klasses/fr.yml' if all defaults are used.

h2. Copyright

Copyright (c) 2010 "Michael Deering(Edmonton Ruby on Rails)":http://mdeering.com See MIT-LICENSE for details.