Module: MongoTranslatable::Translated
- Defined in:
- lib/mongo_translatable.rb
Overview
MongoTranslatable is for taking advantage of MongoDB for storing translations of ActiveRecord models. Here is how it works in practice:
class Item < ActiveRecord::Base
mongo_translate :label
end
I18n.locale = :en
item = Item.create(:label => "a label")
p item.locale
"en"
item = Item.find(1)
p item.label
"a label"
item.translate(:label => "etiketissä", :locale => :fi).save
or you could have set I18n.locale = :fi in calling env and dropped locale from args
I18n.locale = :fi
item = Item.find(1)
p item.label
"etiketissä"
p item.locale
"fi"
If The general approach is inspired by this code in globalize2: github.com/joshmh/globalize2/blob/master/lib/globalize/active_record.rb grab what the normal finder would return and look up corresponding translated version of the objects in question swap in corresponding translated attributes of object creates TranslatableClass::Translation when the declaration method is defined ala acts_as_versioned VersionedClass::Version every translatable class gets their own Translation class under it
TODO: translations aren’t real associations and the translations method is thus not chainable as you would expect currently investigating adding a plugin for mongo_mapper that will do associations declaired from activerecord models in the meantime, you will like want to use the “translation_for(locale)” method
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
54 55 56 |
# File 'lib/mongo_translatable.rb', line 54 def self.included(base) base.extend(ClassMethods) end |