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.



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

def initialize(model)
  @model = model
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#add_translation_fieldsObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/globalize/active_record/migration.rb', line 77

def add_translation_fields
  connection.change_table(translations_table_name) do |t|
    fields.each do |name, options|
      if options.is_a? Hash
        t.column name, options.delete(:type), options
      else
        t.column name, options
      end
    end
  end
end

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/globalize/active_record/migration.rb', line 39

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

  add_translation_fields
  clear_schema_cache!
  move_data_to_translation_table if options[:migrate_data]
  remove_source_columns if options[:remove_source_columns]
  clear_schema_cache!
end

#clear_schema_cache!Object



172
173
174
175
176
# File 'lib/globalize/active_record/migration.rb', line 172

def clear_schema_cache!
  connection.schema_cache.clear! if connection.respond_to? :schema_cache
  model::Translation.reset_column_information
  model.reset_column_information
end

#column_type(name) ⇒ Object



148
149
150
# File 'lib/globalize/active_record/migration.rb', line 148

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

#complete_translated_fieldsObject

This adds all the current translated attributes of the model It’s a problem because in early migrations would add all the translated attributes



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

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

#create_translation_tableObject



69
70
71
72
73
74
75
# File 'lib/globalize/active_record/migration.rb', line 69

def create_translation_table
  connection.create_table(translations_table_name) do |t|
    t.references table_name.sub(/^#{table_name_prefix}/, '').singularize
    t.string :locale, :null => false
    t.timestamps
  end
end

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



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

def create_translation_table!(fields = {}, options = {})
  @fields = fields
  # If we have fields we only want to create the translation table with those fields
  complete_translated_fields if fields.blank?
  validate_translated_fields

  create_translation_table
  add_translation_fields!(fields, options)
  create_translations_index
  clear_schema_cache!
end

#create_translations_indexObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/globalize/active_record/migration.rb', line 89

def create_translations_index
  connection.add_index(
    translations_table_name,
    "#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id",
    :name => translation_index_name
  )
  # index for select('DISTINCT locale') call in translation.rb
  connection.add_index(
    translations_table_name,
    :locale,
    :name => translation_locale_index_name
  )
end

#drop_translation_tableObject



103
104
105
# File 'lib/globalize/active_record/migration.rb', line 103

def drop_translation_table
  connection.drop_table(translations_table_name)
end

#drop_translation_table!(options = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/globalize/active_record/migration.rb', line 54

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

#drop_translations_indexObject



107
108
109
# File 'lib/globalize/active_record/migration.rb', line 107

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

#move_data_to_model_tableObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/globalize/active_record/migration.rb', line 121

def move_data_to_model_table
  add_missing_columns

  # Find all of the translated attributes for all records in the model.
  all_translated_attributes = @model.all.collect{|m| m.attributes}
  all_translated_attributes.each do |translated_record|
    # Create a hash containing the translated column names and their values.
    translated_attribute_names.inject(fields_to_update={}) do |f, name|
      f.update({name.to_sym => translated_record[name.to_s]})
    end

    # Now, update the actual model's record with the hash.
    @model.update_all(fields_to_update, {:id => translated_record['id']})
  end
end

#move_data_to_translation_tableObject



111
112
113
114
115
116
117
118
119
# File 'lib/globalize/active_record/migration.rb', line 111

def move_data_to_translation_table
  model.find_each do |record|
    translation = record.translation_for(I18n.default_locale) || record.translations.build(:locale => I18n.default_locale)
    fields.each do |attribute_name, attribute_type|
      translation[attribute_name] = record.read_attribute(attribute_name, {:translated => false})
    end
    translation.save!
  end
end

#remove_source_columnsObject



50
51
52
# File 'lib/globalize/active_record/migration.rb', line 50

def remove_source_columns
  connection.remove_columns(table_name, *fields.keys)
end

#translation_index_nameObject



160
161
162
163
164
# File 'lib/globalize/active_record/migration.rb', line 160

def translation_index_name
  index_name = "index_#{translations_table_name}_on_#{table_name.singularize}_id"
  index_name.size < connection.index_name_length ? index_name :
    "index_#{Digest::SHA1.hexdigest(index_name)}"[0, connection.index_name_length]
end

#translation_locale_index_nameObject



166
167
168
169
170
# File 'lib/globalize/active_record/migration.rb', line 166

def translation_locale_index_name
  index_name = "index_#{translations_table_name}_on_locale"
  index_name.size < connection.index_name_length ? index_name :
    "index_#{Digest::SHA1.hexdigest(index_name)}"[0, connection.index_name_length]
end

#valid_field_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/globalize/active_record/migration.rb', line 152

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

#valid_field_type?(name, type) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/globalize/active_record/migration.rb', line 156

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

#validate_translated_fieldsObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/globalize/active_record/migration.rb', line 137

def validate_translated_fields
  fields.each do |name, options|
    raise BadFieldName.new(name) unless valid_field_name?(name)
    if options.is_a? Hash
      raise BadFieldType.new(name, options[:type]) unless valid_field_type?(name, options[:type])
    else
      raise BadFieldType.new(name, options) unless valid_field_type?(name, options)
    end
  end
end