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



60
61
62
63
64
65
66
# File 'lib/globalize/automatic/translation.rb', line 60

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



68
69
70
# File 'lib/globalize/automatic/translation.rb', line 68

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

.create_table!(*fields) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/globalize/automatic/translation.rb', line 45

def create_table!(*fields)
  connection.create_table(table_name) do |t|
    t.references automatic_translated_model_foreign_key.sub(/_id$/, ''), null: false, index: true
    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



56
57
58
# File 'lib/globalize/automatic/translation.rb', line 56

def drop_table!
  connection.drop_table(table_name)
end

Instance Method Details

#from_locale(attr_name) ⇒ Object



4
5
6
# File 'lib/globalize/automatic/translation.rb', line 4

def from_locale(attr_name)
  automatic_translated_model.automatic_translation_locale(attr_name)
end

#reject(attr_name, error) ⇒ Object



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

def reject(attr_name, error); end

#resolve(attr_name, translated) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/globalize/automatic/translation.rb', line 28

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



20
21
22
23
24
25
26
# File 'lib/globalize/automatic/translation.rb', line 20

def translate(attr_name)
  if automatically_for?(attr_name)
    Globalize::Automatic.asynchronously ?
        Globalize::Automatic::TranslationJob.perform_later(self, attr_name) :
        Globalize::Automatic::TranslationJob.perform_now(self, attr_name)
  end
end

#translation_for(target_locale) ⇒ Object



16
17
18
# File 'lib/globalize/automatic/translation.rb', line 16

def translation_for(target_locale)
  automatic_translated_model.translation_for(target_locale)
end

#translation_from(attr_name) ⇒ Object



8
9
10
# File 'lib/globalize/automatic/translation.rb', line 8

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

#translation_toObject



12
13
14
# File 'lib/globalize/automatic/translation.rb', line 12

def translation_to
  translation_for(locale)
end