class Hover::Importer::ActiveRecord
Attributes
column_changes[RW]
remote_local_map[RW]
update_finder[RW]
Public Class Methods
new(remote_local_map, update_finder = nil, column_changes = {})
click to toggle source
# File lib/hover/importer/active_record.rb, line 6 def initialize(remote_local_map, update_finder = nil, column_changes = {}) self.remote_local_map = Hash.new { |hash, key| hash[key] = {} } self.remote_local_map.merge!(remote_local_map) self.update_finder = update_finder self.column_changes = column_changes end
Public Instance Methods
change_columns(attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 13 def change_columns(attributes) attributes.inject({}) do |hash, key_value| hash[(column_changes[key_value[0]] || key_value[0])] = key_value[1] hash end end
find_record_to_update(klass, attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 51 def find_record_to_update(klass, attributes) self.update_finder.call(self, klass, attributes) end
import_record(klass, attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 75 def import_record(klass, attributes) attributes = change_columns(attributes.dup) external_primary_key = attributes.delete(klass.primary_key) s3_file_attributes = s3_file_keys(klass, attributes) attributes.reject! { |key, value| !klass.column_names.include?(key) } attributes.merge!(s3_file_attributes) if s3_file_attributes.is_a?(Hash) record = klass.new(attributes) return unless last_insert_id = insert_record(record) remote_local_map[klass.table_name][external_primary_key] = last_insert_id [klass, last_insert_id] end
import_results(results)
click to toggle source
# File lib/hover/importer/active_record.rb, line 55 def import_results(results) class_id_sets = [] results.each do |table_name, records_attributes| if table_class_id_sets = import_table(table_name, records_attributes) class_id_sets.concat(table_class_id_sets) end end class_id_sets end
import_table(table_name, records_attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 67 def import_table(table_name, records_attributes) klass = table_name.singularize.camelize.constantize records_attributes.collect { |attributes| import_record(klass, attributes) unless attributes.nil? } end
insert_record(record)
click to toggle source
see github.com/rails/rails/blob/v5.2.0/activerecord/lib/active_record/persistence.rb#L729-L741
# File lib/hover/importer/active_record.rb, line 92 def insert_record(record) attribute_names = record.class.column_names attributes_values = record.send(:attributes_with_values_for_create, attribute_names) primary_key = record.class._insert_record(attributes_values) record[record.class.primary_key] = primary_key end
new_foreign_keys(record)
click to toggle source
# File lib/hover/importer/active_record.rb, line 101 def new_foreign_keys(record) record.class.reflect_on_all_associations.inject({}) do |hash, association| method = "new_foreign_keys_for_#{association.macro}" method += "_polymorphic" if association.options[:polymorphic] if respond_to?(method) && before_after = send(method, record, association) hash.store(*before_after) end hash end end
new_foreign_keys_for_belongs_to(record, association)
click to toggle source
# File lib/hover/importer/active_record.rb, line 114 def new_foreign_keys_for_belongs_to(record, association) return unless id = self.remote_local_map[association.klass.table_name].try(:[], record[association.foreign_key]) [association.foreign_key, id] end
new_foreign_keys_for_belongs_to_polymorphic(record, association)
click to toggle source
# File lib/hover/importer/active_record.rb, line 119 def new_foreign_keys_for_belongs_to_polymorphic(record, association) return unless id = self.remote_local_map[record[association.foreign_type].tableize].try(:[], record[association.foreign_key]) [association.foreign_key, id] end
s3_file_keys(klass, attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 41 def s3_file_keys(klass, attributes) klass.try(:s3_file_versions).try(:keys).try(:inject, {}) do |output, attribute| if value = attributes.delete(attribute.to_s) output["#{attribute}_s3_keys"] = value end output end end
update(table_name, records_attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 20 def update(table_name, records_attributes) klass = table_name.singularize.camelize.constantize records_attributes.collect { |attributes| update_record(klass, attributes) } end
update_foreign_keys(record)
click to toggle source
# File lib/hover/importer/active_record.rb, line 124 def update_foreign_keys(record) foreign_key_attributes = new_foreign_keys(record) return if foreign_key_attributes.empty? record.class.unscoped.where(id: record.id).update_all(foreign_key_attributes) [record.reload.class, record.id] end
update_record(klass, attributes)
click to toggle source
# File lib/hover/importer/active_record.rb, line 25 def update_record(klass, attributes) return unless record = find_record_to_update(klass, attributes) attributes = change_columns(attributes.dup) s3_file_attributes = s3_file_keys(klass, attributes) attributes.delete(klass.primary_key) attributes.reject! { |key, value| !klass.column_names.include?(key) } attributes.merge!(s3_file_attributes) if s3_file_attributes.is_a?(Hash) record.assign_attributes(attributes) record.save!(validate: false) [klass, record.id] end