Class: Globalize::ActiveRecord::Migration::Migrator

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/globalize/active_record/migration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Migrator

Returns a new instance of Migrator.



22
23
24
# File 'lib/globalize/active_record/migration.rb', line 22

def initialize(model)
  @model = model
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



18
19
20
# File 'lib/globalize/active_record/migration.rb', line 18

def fields
  @fields
end

#modelObject (readonly)

Returns the value of attribute model.



18
19
20
# File 'lib/globalize/active_record/migration.rb', line 18

def model
  @model
end

Instance Method Details

#column_type(name) ⇒ Object



88
89
90
# File 'lib/globalize/active_record/migration.rb', line 88

def column_type(name)
  columns.detect { |c| c.name == name.to_s }.try(:type)
end

#complete_translated_fieldsObject



42
43
44
45
46
# File 'lib/globalize/active_record/migration.rb', line 42

def complete_translated_fields
  translated_attribute_names.each do |name|
    fields[name] = column_type(name) unless fields[name]
  end
end

#create_translation_tableObject



48
49
50
51
52
53
54
55
# File 'lib/globalize/active_record/migration.rb', line 48

def create_translation_table
  connection.create_table(translations_table_name) do |t|
    t.references table_name.sub(/^#{table_name_prefix}/, '').singularize
    t.string :locale
    fields.each { |name, type| t.column name, type }
    t.timestamps
  end
end

#create_translation_table!(fields = {}, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/globalize/active_record/migration.rb', line 26

def create_translation_table!(fields = {}, options = {})
  @fields = fields
  complete_translated_fields
  validate_translated_fields

  create_translation_table
  move_data_to_translation_table if options[:migrate_data]
  create_translations_index
end

#create_translations_indexObject



57
58
59
60
61
62
63
# File 'lib/globalize/active_record/migration.rb', line 57

def create_translations_index
  connection.add_index(
    translations_table_name,
    "#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id",
    :name => translation_index_name
  )
end

#drop_translation_tableObject



65
66
67
# File 'lib/globalize/active_record/migration.rb', line 65

def drop_translation_table
  connection.drop_table(translations_table_name)
end

#drop_translation_table!(options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/globalize/active_record/migration.rb', line 36

def drop_translation_table!(options = {})
  move_data_to_model_table if options[:migrate_data]
  drop_translations_index
  drop_translation_table
end

#drop_translations_indexObject



69
70
71
# File 'lib/globalize/active_record/migration.rb', line 69

def drop_translations_index
  connection.remove_index(translations_table_name, :name => translation_index_name) rescue nil
end

#move_data_to_model_tableObject



77
78
79
# File 'lib/globalize/active_record/migration.rb', line 77

def move_data_to_model_table
  # TODO
end

#move_data_to_translation_tableObject



73
74
75
# File 'lib/globalize/active_record/migration.rb', line 73

def move_data_to_translation_table
  # TODO
end

#translation_index_nameObject



100
101
102
103
104
# File 'lib/globalize/active_record/migration.rb', line 100

def translation_index_name
  # FIXME what's the max size of an index name?
  index_name = "index_#{translations_table_name}_on_#{table_name.singularize}_id"
  index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
end

#valid_field_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/globalize/active_record/migration.rb', line 92

def valid_field_name?(name)
  translated_attribute_names.include?(name)
end

#valid_field_type?(name, type) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/globalize/active_record/migration.rb', line 96

def valid_field_type?(name, type)
  !translated_attribute_names.include?(name) || [:string, :text].include?(type)
end

#validate_translated_fieldsObject



81
82
83
84
85
86
# File 'lib/globalize/active_record/migration.rb', line 81

def validate_translated_fields
  fields.each do |name, type|
    raise BadFieldName.new(name) unless valid_field_name?(name)
    raise BadFieldType.new(name, type) unless valid_field_type?(name, type)
  end
end