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
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #add_translation_fields ⇒ Object
- #add_translation_fields!(fields, options = {}) ⇒ Object
- #clear_schema_cache! ⇒ Object
- #column_type(name) ⇒ Object
-
#complete_translated_fields ⇒ Object
This adds all the current translated attributes of the model It’s a problem because in early migrations would add all the translated attributes.
- #create_translation_table ⇒ Object
- #create_translation_table!(fields = {}, options = {}) ⇒ Object
- #create_translations_index(options) ⇒ Object
- #drop_translation_table ⇒ Object
- #drop_translation_table!(options = {}) ⇒ Object
- #drop_translations_index ⇒ Object
- #fields ⇒ Object
-
#initialize(model) ⇒ Migrator
constructor
A new instance of Migrator.
- #move_data_to_model_table ⇒ Object
- #move_data_to_translation_table ⇒ Object
- #remove_source_columns ⇒ Object
- #translation_index_name ⇒ Object
- #translation_locale_index_name ⇒ Object
- #translation_unique_index_name ⇒ Object
- #valid_field_name?(name) ⇒ Boolean
- #validate_translated_fields ⇒ Object
Constructor Details
#initialize(model) ⇒ Migrator
Returns a new instance of Migrator.
21 22 23 |
# File 'lib/globalize/active_record/migration.rb', line 21 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
17 18 19 |
# File 'lib/globalize/active_record/migration.rb', line 17 def model @model end |
Instance Method Details
#add_translation_fields ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/globalize/active_record/migration.rb', line 93 def add_translation_fields connection.change_table(translations_table_name) do |t| fields.each do |name, | if .is_a? Hash t.column name, .delete(:type), ** else t.column name, end end end end |
#add_translation_fields!(fields, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/globalize/active_record/migration.rb', line 45 def add_translation_fields!(fields, = {}) @fields = fields validate_translated_fields add_translation_fields clear_schema_cache! move_data_to_translation_table if [:migrate_data] remove_source_columns if [:remove_source_columns] clear_schema_cache! end |
#clear_schema_cache! ⇒ Object
207 208 209 210 211 |
# File 'lib/globalize/active_record/migration.rb', line 207 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
187 188 189 |
# File 'lib/globalize/active_record/migration.rb', line 187 def column_type(name) columns.detect { |c| c.name == name.to_s }.try(:type) || :string end |
#complete_translated_fields ⇒ Object
This adds all the current translated attributes of the model It’s a problem because in early migrations would add all the translated attributes
74 75 76 77 78 |
# File 'lib/globalize/active_record/migration.rb', line 74 def complete_translated_fields translated_attribute_names.each do |name| @fields[name] ||= column_type(name) end end |
#create_translation_table ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/globalize/active_record/migration.rb', line 80 def create_translation_table connection.create_table(translations_table_name) do |t| t.references table_name.sub(/^#{table_name_prefix}/, '').singularize, :null => false, :index => false, :type => column_type(model.primary_key).try(:to_sym), :limit => model.columns.detect { |c| c.name == model.primary_key }.try(:limit) t.string :locale, :null => false t. :null => false end end |
#create_translation_table!(fields = {}, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/globalize/active_record/migration.rb', line 29 def create_translation_table!(fields = {}, = {}) extra = .keys - [:migrate_data, :remove_source_columns, :unique_index] if extra.any? raise ArgumentError, "Unknown migration #{'option'.pluralize(extra.size)}: #{extra}" end @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, ) create_translations_index() clear_schema_cache! end |
#create_translations_index(options) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/globalize/active_record/migration.rb', line 118 def create_translations_index() foreign_key = "#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id".to_sym connection.add_index( translations_table_name, foreign_key, :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 ) if [:unique_index] connection.add_index( translations_table_name, [foreign_key, :locale], :name => translation_unique_index_name, unique: true ) end end |
#drop_translation_table ⇒ Object
142 143 144 |
# File 'lib/globalize/active_record/migration.rb', line 142 def drop_translation_table connection.drop_table(translations_table_name) end |
#drop_translation_table!(options = {}) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/globalize/active_record/migration.rb', line 64 def drop_translation_table!( = {}) add_missing_columns if [:create_source_columns] move_data_to_model_table if [:migrate_data] drop_translations_index drop_translation_table clear_schema_cache! end |
#drop_translations_index ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/globalize/active_record/migration.rb', line 146 def drop_translations_index if connection.indexes(translations_table_name).map(&:name).include?(translation_index_name) connection.remove_index(translations_table_name, :name => translation_index_name) end if connection.indexes(translations_table_name).map(&:name).include?(translation_locale_index_name) connection.remove_index(translations_table_name, :name => translation_locale_index_name) end end |
#fields ⇒ Object
25 26 27 |
# File 'lib/globalize/active_record/migration.rb', line 25 def fields @fields ||= complete_translated_fields end |
#move_data_to_model_table ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/globalize/active_record/migration.rb', line 165 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]}) # Remove attributes that will no longer be translated translated_attribute_names.delete(name) end # Now, update the actual model's record with the hash. model.where(model.primary_key.to_sym => translated_record[model.primary_key]).update_all(fields_to_update) end end |
#move_data_to_translation_table ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/globalize/active_record/migration.rb', line 155 def move_data_to_translation_table model.find_each do |record| translation = record.translation_for(I18n.locale) || record.translations.build(:locale => I18n.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_columns ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/globalize/active_record/migration.rb', line 55 def remove_source_columns column_names = *fields.keys column_names.each do |column| if connection.column_exists?(table_name, column) connection.remove_column(table_name, column) end end end |
#translation_index_name ⇒ Object
195 196 197 |
# File 'lib/globalize/active_record/migration.rb', line 195 def translation_index_name truncate_index_name "index_#{translations_table_name}_on_#{table_name.singularize}_id" end |
#translation_locale_index_name ⇒ Object
199 200 201 |
# File 'lib/globalize/active_record/migration.rb', line 199 def translation_locale_index_name truncate_index_name "index_#{translations_table_name}_on_locale" end |
#translation_unique_index_name ⇒ Object
203 204 205 |
# File 'lib/globalize/active_record/migration.rb', line 203 def translation_unique_index_name truncate_index_name "index_#{translations_table_name}_on_#{table_name.singularize}_id_and_locale" end |
#valid_field_name?(name) ⇒ Boolean
191 192 193 |
# File 'lib/globalize/active_record/migration.rb', line 191 def valid_field_name?(name) translated_attribute_names.include?(name) end |
#validate_translated_fields ⇒ Object
181 182 183 184 185 |
# File 'lib/globalize/active_record/migration.rb', line 181 def validate_translated_fields fields.each do |name, | raise BadFieldName.new(name) unless valid_field_name?(name) end end |