Class: Globalize::Automatic::Translation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/globalize/automatic/translation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_fields!(*fields) ⇒ Object



64
65
66
67
68
69
70
# 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_name(field) ⇒ Object



72
73
74
# File 'lib/globalize/automatic/translation.rb', line 72

def automatically_column_name(field)
  :"#{field}_automatically"
end

.create_table!(*fields) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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!Object



60
61
62
# File 'lib/globalize/automatic/translation.rb', line 60

def drop_table!
  connection.drop_table(table_name)
end

Instance Method Details

#from_locale(attr_name) ⇒ Object



6
7
8
# 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) ⇒ Object



40
# File 'lib/globalize/automatic/translation.rb', line 40

def reject(attr_name, error); end

#resolve(attr_name, translated) ⇒ Object



31
32
33
34
35
36
37
38
# 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) ⇒ Object



22
23
24
25
26
27
28
29
# 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) ⇒ Object



18
19
20
# 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) ⇒ Object



10
11
12
# File 'lib/globalize/automatic/translation.rb', line 10

def translation_from(attr_name)
  translation_for(from_locale(attr_name))
end

#translation_toObject



14
15
16
# File 'lib/globalize/automatic/translation.rb', line 14

def translation_to
  translation_for(locale)
end