class TranslatableRecords::Builder
Attributes
concern[R]
model[R]
Public Class Methods
new(model)
click to toggle source
# File lib/translatable_records/builder.rb, line 6 def initialize(model) @model = model @concern = Module.new do extend ActiveSupport::Concern include Concern end end
Public Instance Methods
define(attributes)
click to toggle source
# File lib/translatable_records/builder.rb, line 14 def define(attributes) ensure_association attributes.each do |attribute| define_writer attribute define_readers attribute model.translations << attribute end model.include concern end
Private Instance Methods
define_readers(attribute)
click to toggle source
# File lib/translatable_records/builder.rb, line 50 def define_readers(attribute) names = [ attribute, "#{attribute}_was", "#{attribute}_changed?", "#{attribute}_before_type_cast", "#{attribute}_came_from_user?" ] concern.class_eval do names.each do |name| define_method name do find_translation(locale).try name end end end end
define_writer(attribute)
click to toggle source
# File lib/translatable_records/builder.rb, line 37 def define_writer(attribute) name = "#{attribute}=" concern.class_eval do define_method name do |value| if translation = find_translation(locale) translation.send name, value else translations.build(locale: locale, attribute => value) end end end end
ensure_association()
click to toggle source
# File lib/translatable_records/builder.rb, line 26 def ensure_association unless model.reflections.has_key?(:translations) model.has_many( :translations, class_name: "#{model.name}Translation", dependent: :destroy ) model.accepts_nested_attributes_for :translations end end