class Globalize::Automatic::Translation

Private Class Methods

add_fields!(*fields) click to toggle source
# File lib/globalize/automatic/translation.rb, line 64
def add_fields!(*fields)
  connection.change_table(table_name) do |t|
    fields.each do |field|
      t.boolean *automatically_column_args(field)
    end
  end
end
automatically_column_args(field) click to toggle source
# File lib/globalize/automatic/translation.rb, line 77
def automatically_column_args(field)
  args = [automatically_column_name(field), default: false, null: false]
end
automatically_column_name(field) click to toggle source
# File lib/globalize/automatic/translation.rb, line 72
def automatically_column_name(field)
  :"#{field}_automatically"
end
create_table!(*fields) click to toggle source
# File lib/globalize/automatic/translation.rb, line 48
def create_table!(*fields)
  relation_name = automatic_translated_model_foreign_key.sub(/_id$/, '')
  connection.create_table(table_name) do |t|
    t.references relation_name, null: false, index: { name: "index_#{Digest::MD5.hexdigest([table_name, relation_name].join('/'))}" }
    t.string :locale, null: false
    fields.each do |field|
      t.boolean *automatically_column_args(field)
    end
    t.timestamps null: false
  end
end
drop_table!() click to toggle source
# File lib/globalize/automatic/translation.rb, line 60
def drop_table!
  connection.drop_table(table_name)
end

Public Instance Methods

from_locale(attr_name) click to toggle source
# File lib/globalize/automatic/translation.rb, line 6
def from_locale(attr_name)
  automatic_translated_model.automatic_translation_locale(attr_name)
end
reject(attr_name, error) click to toggle source
# File lib/globalize/automatic/translation.rb, line 40
def reject(attr_name, error); end
resolve(attr_name, translated) click to toggle source
# File lib/globalize/automatic/translation.rb, line 31
def resolve(attr_name, translated)
  obj = translation_to
  obj.transaction do
    obj.lock!
    obj[attr_name] = translated
    obj.save!(validate: false)
  end
end
translate(attr_name) click to toggle source
# File lib/globalize/automatic/translation.rb, line 22
def translate(attr_name)
  if automatically_for?(attr_name)
    save if new_record?
    Globalize::Automatic.asynchronously ?
        Globalize::Automatic::TranslationJob.perform_later(self, attr_name.to_s) :
        Globalize::Automatic::TranslationJob.perform_now(self, attr_name.to_s)
  end
end
translation_for(target_locale) click to toggle source
# File lib/globalize/automatic/translation.rb, line 18
def translation_for(target_locale)
  automatic_translated_model.translation_for(target_locale)
end
translation_from(attr_name) click to toggle source
# File lib/globalize/automatic/translation.rb, line 10
def translation_from(attr_name)
  translation_for(from_locale(attr_name))
end
translation_to() click to toggle source
# File lib/globalize/automatic/translation.rb, line 14
def translation_to
  translation_for(locale)
end

Private Instance Methods

automatically_for?(attr_name) click to toggle source
# File lib/globalize/automatic/translation.rb, line 43
def automatically_for?(attr_name)
  self[self.class.automatically_column_name(attr_name)]
end