Module: Voltron::Translatable::InstanceMethods

Defined in:
lib/voltron/translatable.rb

Instance Method Summary collapse

Instance Method Details

#build_translationsObject

Before validation, iterate over all possible translation methods and either update the corresponding translation record or build it, so it can be saved when the parent record is saved



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/voltron/translatable.rb', line 96

def build_translations
  self.translations_attributes = translation_methods.map do |m, t|
    if send(:"#{m}_changed?")
      # Find the translation if it previously existed, or create new one
      translation = translations.where(attribute_name: t[:attribute], locale: t[:locale]).first || Voltron::Translation.new(attribute_name: t[:attribute], locale: t[:locale])
      # Set the translation text on our returned translation object
      translation.translation = instance_variable_get("@#{m}")
      # Return the attributes of our translation that will be assigned to self.translations_attributes
      translation.attributes
    end
  end.compact
end