Class: Hover::Importer::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/hover/importer/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_local_map, update_finder = nil, column_changes = {}) ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



6
7
8
9
10
11
# 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

Instance Attribute Details

#column_changesObject

Returns the value of attribute column_changes.



4
5
6
# File 'lib/hover/importer/active_record.rb', line 4

def column_changes
  @column_changes
end

#remote_local_mapObject

Returns the value of attribute remote_local_map.



4
5
6
# File 'lib/hover/importer/active_record.rb', line 4

def remote_local_map
  @remote_local_map
end

#update_finderObject

Returns the value of attribute update_finder.



4
5
6
# File 'lib/hover/importer/active_record.rb', line 4

def update_finder
  @update_finder
end

Instance Method Details

#change_columns(attributes) ⇒ Object



13
14
15
16
17
18
# 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) ⇒ Object



51
52
53
# 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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
# 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) ⇒ Object



92
93
94
95
96
97
98
99
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



114
115
116
117
# 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) ⇒ Object



119
120
121
122
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



20
21
22
23
# 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) ⇒ Object



124
125
126
127
128
129
130
131
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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