Class: Globalize::ActiveRecord::Migration::Migrator
- Inherits:
-
Object
- Object
- Globalize::ActiveRecord::Migration::Migrator
- Includes:
- Exceptions
- Defined in:
- lib/globalize/active_record/migration.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #column_type(name) ⇒ Object
- #complete_translated_fields ⇒ Object
- #create_translation_table ⇒ Object
- #create_translation_table!(fields = {}, options = {}) ⇒ Object
- #create_translations_index ⇒ Object
- #drop_translation_table ⇒ Object
- #drop_translation_table!(options = {}) ⇒ Object
- #drop_translations_index ⇒ Object
-
#initialize(model) ⇒ Migrator
constructor
A new instance of Migrator.
- #move_data_to_model_table ⇒ Object
- #move_data_to_translation_table ⇒ Object
- #translation_index_name ⇒ Object
- #valid_field_name?(name) ⇒ Boolean
- #valid_field_type?(name, type) ⇒ Boolean
- #validate_translated_fields ⇒ Object
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
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
18 19 20 |
# File 'lib/globalize/active_record/migration.rb', line 18 def fields @fields end |
#model ⇒ Object (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
105 106 107 |
# File 'lib/globalize/active_record/migration.rb', line 105 def column_type(name) columns.detect { |c| c.name == name.to_s }.try(:type) || translated_columns_hash[name.to_s] end |
#complete_translated_fields ⇒ Object
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_table ⇒ Object
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. 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 = {}, = {}) @fields = fields complete_translated_fields validate_translated_fields create_translation_table move_data_to_translation_table if [:migrate_data] create_translations_index end |
#create_translations_index ⇒ Object
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_table ⇒ Object
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!( = {}) move_data_to_model_table if [:migrate_data] drop_translations_index drop_translation_table end |
#drop_translations_index ⇒ Object
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_table ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/globalize/active_record/migration.rb', line 84 def move_data_to_model_table # 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_table ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/globalize/active_record/migration.rb', line 73 def move_data_to_translation_table # Find all of the existing untranslated attributes for this model. all_model_fields = @model.all model_attributes = all_model_fields.collect {|m| m.untranslated_attributes} all_model_fields.each do |model_record| # Assign the attributes back to the model which will enable globalize3 to translate them. model_record.attributes = model_attributes.detect{|a| a['id'] == model_record.id} model_record.save! end end |
#translation_index_name ⇒ Object
117 118 119 120 121 |
# File 'lib/globalize/active_record/migration.rb', line 117 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
109 110 111 |
# File 'lib/globalize/active_record/migration.rb', line 109 def valid_field_name?(name) translated_attribute_names.include?(name) end |
#valid_field_type?(name, type) ⇒ Boolean
113 114 115 |
# File 'lib/globalize/active_record/migration.rb', line 113 def valid_field_type?(name, type) !translated_attribute_names.include?(name) || [:string, :text].include?(type) end |
#validate_translated_fields ⇒ Object
98 99 100 101 102 103 |
# File 'lib/globalize/active_record/migration.rb', line 98 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 |