module Voltron::Translatable::InstanceMethods

Public Instance Methods

build_translations() click to toggle source

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

# 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

Private Instance Methods

translate_translations() click to toggle source
# File lib/voltron/translatable.rb, line 111
def translate_translations
  self.class.instance_variable_get('@_translations')
end
translation_methods() click to toggle source
# File lib/voltron/translatable.rb, line 115
def translation_methods
  translate_translations.map do |attribute, locales|
    locales.map { |locale| { :"#{attribute}_#{locale}" => { attribute: attribute, locale: locale } } }
  end.flatten.reduce(Hash.new, :merge)
end