Module: Translatable::ActiveRecord
- Defined in:
- lib/translatable/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 TranslatableNews < ActiveRecord::Base #
attr_accessible :title, :content
end
class News < ActiveRecord::Base
belongs_to :author
translatable do
translatable :title, :presence => true, :uniqueness => true
translatable :content, :presence => true
translatable_model "TranslatedNews"
translatable_origin :origin_id
end
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"
::I18n.locale = "ru"
news.content
# => "Сюди идет текст"
::I18n.locale = "de"
news.content
# => nil
::I18n.locale = ::I18n.default_locale
news.content
# => "That is where the text goes"
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Instance Method Summary collapse
Instance Method Details
#translatable ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/translatable/active_record.rb', line 55 def translatable extend Translatable::ActiveRecord::ClassMethods include Translatable::ActiveRecord::InstanceMethods translatable_define_hash yield translatable_register end |