Class: Countrizable::ActiveRecord::Migration::Migrator
- Inherits:
-
Object
- Object
- Countrizable::ActiveRecord::Migration::Migrator
- Includes:
- Exceptions
- Defined in:
- lib/countrizable/active_record/migration.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #add_country_value_fields ⇒ Object
- #add_country_value_fields!(fields, options = {}) ⇒ Object
- #clear_schema_cache! ⇒ Object
- #column_type(name) ⇒ Object
-
#complete_country_fields ⇒ Object
This adds all the current country attributes of the model It’s a problem because in early migrations would add all the country attributes.
- #country_value_country_code_index_name ⇒ Object
- #country_value_index_name ⇒ Object
- #country_value_unique_index_name ⇒ Object
- #create_country_value_table ⇒ Object
- #create_country_value_table!(fields = {}, options = {}) ⇒ Object
- #create_country_values_index(options) ⇒ Object
- #drop_country_table!(options = {}) ⇒ Object
- #drop_country_value_table ⇒ Object
- #drop_country_values_index ⇒ Object
- #fields ⇒ Object
-
#initialize(model) ⇒ Migrator
constructor
A new instance of Migrator.
- #move_data_to_country_value_table ⇒ Object
- #move_data_to_model_table ⇒ Object
- #remove_source_columns ⇒ Object
- #valid_field_name?(name) ⇒ Boolean
- #validate_country_fields ⇒ Object
Constructor Details
#initialize(model) ⇒ Migrator
Returns a new instance of Migrator.
21 22 23 |
# File 'lib/countrizable/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/countrizable/active_record/migration.rb', line 17 def model @model end |
Instance Method Details
#add_country_value_fields ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/countrizable/active_record/migration.rb', line 87 def add_country_value_fields connection.change_table(country_values_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_country_value_fields!(fields, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/countrizable/active_record/migration.rb', line 45 def add_country_value_fields!(fields, = {}) @fields = fields validate_country_fields add_country_value_fields clear_schema_cache! move_data_to_country_table if [:migrate_data] remove_source_columns if [:remove_source_columns] clear_schema_cache! end |
#clear_schema_cache! ⇒ Object
188 189 190 191 192 |
# File 'lib/countrizable/active_record/migration.rb', line 188 def clear_schema_cache! connection.schema_cache.clear! if connection.respond_to? :schema_cache model::CountryValue.reset_column_information model.reset_column_information end |
#column_type(name) ⇒ Object
168 169 170 |
# File 'lib/countrizable/active_record/migration.rb', line 168 def column_type(name) columns.detect { |c| c.name == name.to_s }.try(:type) || :string end |
#complete_country_fields ⇒ Object
This adds all the current country attributes of the model It’s a problem because in early migrations would add all the country attributes
73 74 75 76 77 |
# File 'lib/countrizable/active_record/migration.rb', line 73 def complete_country_fields country_attribute_names.each do |name| @fields[name] ||= column_type(name) end end |
#country_value_country_code_index_name ⇒ Object
180 181 182 |
# File 'lib/countrizable/active_record/migration.rb', line 180 def country_value_country_code_index_name truncate_index_name "index_#{country_values_table_name}_on_country_code" end |
#country_value_index_name ⇒ Object
176 177 178 |
# File 'lib/countrizable/active_record/migration.rb', line 176 def country_value_index_name truncate_index_name "index_#{country_values_table_name}_on_#{table_name.singularize}_id" end |
#country_value_unique_index_name ⇒ Object
184 185 186 |
# File 'lib/countrizable/active_record/migration.rb', line 184 def country_value_unique_index_name truncate_index_name "index_#{country_values_table_name}_on_#{table_name.singularize}_id_and_country_code" end |
#create_country_value_table ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/countrizable/active_record/migration.rb', line 79 def create_country_value_table connection.create_table(country_values_table_name) do |t| t.references table_name.sub(/^#{table_name_prefix}/, '').singularize, :null => false, :index => false, :type => column_type(model.primary_key).to_sym t.string :country_code, :null => false t. :null => false end end |
#create_country_value_table!(fields = {}, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/countrizable/active_record/migration.rb', line 29 def create_country_value_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 country table with those fields complete_country_fields if fields.blank? validate_country_fields create_country_value_table add_country_value_fields!(fields, ) create_country_values_index() clear_schema_cache! end |
#create_country_values_index(options) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/countrizable/active_record/migration.rb', line 99 def create_country_values_index() foreign_key = "#{table_name.sub(/^#{table_name_prefix}/, "").singularize}_id".to_sym connection.add_index( country_values_table_name, foreign_key, :name => country_value_index_name ) # index for select('DISTINCT country_code') call in country_value.rb connection.add_index( country_values_table_name, :country_code, :name => country_value_country_code_index_name ) if [:unique_index] connection.add_index( country_values_table_name, [foreign_key, :country_code], :name => country_value_unique_index_name, unique: true ) end end |
#drop_country_table!(options = {}) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/countrizable/active_record/migration.rb', line 64 def drop_country_table!( = {}) move_data_to_model_table if [:migrate_data] drop_country_values_index drop_country_value_table clear_schema_cache! end |
#drop_country_value_table ⇒ Object
123 124 125 |
# File 'lib/countrizable/active_record/migration.rb', line 123 def drop_country_value_table connection.drop_table(country_values_table_name) end |
#drop_country_values_index ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/countrizable/active_record/migration.rb', line 127 def drop_country_values_index if connection.indexes(country_values_table_name).map(&:name).include?(country_value_index_name) connection.remove_index(country_values_table_name, :name => country_value_index_name) end if connection.indexes(country_values_table_name).map(&:name).include?(country_value_country_code_index_name) connection.remove_index(country_values_table_name, :name => country_value_country_code_index_name) end end |
#fields ⇒ Object
25 26 27 |
# File 'lib/countrizable/active_record/migration.rb', line 25 def fields @fields ||= complete_country_fields end |
#move_data_to_country_value_table ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/countrizable/active_record/migration.rb', line 136 def move_data_to_country_value_table model.find_each do |record| country_value = record.country_value_for(I18n.country_code) || record.country_values.build(:country_code => I18n.country_code) fields.each do |attribute_name, attribute_type| country_value[attribute_name] = record.read_attribute(attribute_name, {:country_attributed => false}) end country_value.save! end end |
#move_data_to_model_table ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/countrizable/active_record/migration.rb', line 146 def move_data_to_model_table add_missing_columns # Find all of the country attributes for all records in the model. all_country_attributes = model.all.collect{|m| m.attributes} all_country_attributes.each do |country_record| # Create a hash containing the country column names and their values. country_attribute_names.inject(fields_to_update={}) do |f, name| f.update({name.to_sym => country_record[name.to_s]}) end # Now, update the actual model's record with the hash. model.where(model.primary_key.to_sym => country_record[model.primary_key]).update_all(fields_to_update) end end |
#remove_source_columns ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/countrizable/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 |
#valid_field_name?(name) ⇒ Boolean
172 173 174 |
# File 'lib/countrizable/active_record/migration.rb', line 172 def valid_field_name?(name) country_attribute_names.include?(name) end |
#validate_country_fields ⇒ Object
162 163 164 165 166 |
# File 'lib/countrizable/active_record/migration.rb', line 162 def validate_country_fields fields.each do |name, | raise BadFieldName.new(name) unless valid_field_name?(name) end end |