Module: Translatable::ActiveRecord
- Defined in:
- lib/translatable/orm/active_record.rb
Overview
In order to made the model Translatable, an additional fields should should be added first to it. Here is an example of it might be implemented:
Examples:
class Author < ActiveRecord::Base
validates :name, :presence => true
end
class TranslatedNews < ActiveRecord::Base #
attr_accessible :title, :content
end
class News < ActiveRecord::Base
belongs_to :author
translatable do
field :title, :presence => true, :uniqueness => true
field :content, :presence => true
class_name "TranslatedNews"
foreign_key :origin_id
end
accepts_nested_attributes_for :translations, :current_translation
attr_accessible :translations_attributes, :current_translation_attributes
attr_accessible :author_id, :author
end
An example of application:
news = News.create :translations_attributes => [{title: "Resent News", content: "That is where the text goes", locale: "en"}]
news.translations.create title: "Заголовок", content: "Содержание",locale: "ru"
news.content
# => "That is where the text goes"
news.set_current_translation :ru
news.content
# => "Сюди идет текст"
news.set_current_translation :de
news.content
# => nil
news.set_current_translation
news.content
# => "That is where the text goes"
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Instance Method Summary collapse
Instance Method Details
#translatable(&block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/translatable/orm/active_record.rb', line 54 def translatable(&block) extend Translatable::ActiveRecord::ClassMethods include Translatable::ActiveRecord::InstanceMethods @translatable_base = Translatable::Base.new(self) @translatable_base.instance_eval(&block) t_register_origin t_register_translations t_register_locale end |