activerecord-archiver 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72dfa5f71ad5437d192444235c123b54138d6084
4
- data.tar.gz: c338aaffb0254810abee254e666aeaa4b4aef923
3
+ metadata.gz: deb91c95d7c5e133554018262c2d5bb8ef928126
4
+ data.tar.gz: d43388d052fef1228384bb2c510c2108b7bc8924
5
5
  SHA512:
6
- metadata.gz: b7e432e8b835befe4f49345f0d0bdbe8b7f2d03de578acb426c9b6c9b653512862f2e615321b2332af5e52dd43277ffc6aa981323d1ec37f54db922b6b0ca9c8
7
- data.tar.gz: 555a568642af8949346b6278c8883602bc357118ed7a5dfb740792d8405704ad5aa2e20580e4663cd83dbba07023208629ca3eec158ad365f99734ffb360b2ad
6
+ metadata.gz: f15f61ec60073da124467322e4c706641e587e678e1c730e1028f48c390d236fffcfd1ea9e38568f512d24f53df4b7a26b5d4c579e11c6ae963b95805ef80891
7
+ data.tar.gz: 1b9966bae6b81b0a16ba804aa1e23fc326cdc1fd97b3fdb2b4a8e4db9ebe12bfdd44db52017eeabd3e2682e4dcfffd20af53f241372dccc6a53128cc5a011ee3
@@ -25,7 +25,8 @@ class ActiveRecordArchiver
25
25
 
26
26
  def self.relation_index record, attribute
27
27
  relevant_records = @models_hash[relation_model(record.class, attribute)].first
28
- relevant_records.present? && relevant_records.map(&:id).index(record.send(attribute).id)
28
+ relevant_records.present? and
29
+ relevant_records.compact.map(&:id).index(record.send(attribute).try(:id))
29
30
  end
30
31
 
31
32
  def self.relation_id model, key, value
@@ -40,8 +41,12 @@ class ActiveRecordArchiver
40
41
  def self.insertable_hash model, hash
41
42
  ret = {}
42
43
  hash.each_pair do |key, value|
43
- if column(model, key)
44
- ret[column(model, key)] = value
44
+ if (col = column(model, key))
45
+ if col.type == :datetime and value.present?
46
+ ret[col] = DateTime.parse(value).to_s(:db)
47
+ else
48
+ ret[col] = value
49
+ end
45
50
  elsif belongs_to?(model, key)
46
51
  foreign_key = relation_foreign_key(model, key)
47
52
  if !hash.include?(foreign_key) and column_required(model, foreign_key)
@@ -66,8 +71,8 @@ class ActiveRecordArchiver
66
71
  end
67
72
 
68
73
  def self.assert_instance_of instance, klass
69
- unless instance.class <= klass
70
- raise "Object #{instance} is not an instance of the #{klass} model"
74
+ unless instance.is_a?(klass)
75
+ raise "Object #{instance.inspect} is not an instance of the #{klass.inspect} model"
71
76
  end
72
77
  end
73
78
 
@@ -114,4 +119,16 @@ class ActiveRecordArchiver
114
119
  end
115
120
  end.compact
116
121
  end
122
+
123
+ def self.without_foreign_key_constraints
124
+ conn = ActiveRecord::Base.connection
125
+ if (conn.instance_of?(ActiveRecord::ConnectionAdapters::MysqlAdapter) or
126
+ conn.instance_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter))
127
+ conn.execute('SET foreign_key_checks = 0')
128
+ yield
129
+ conn.execute('SET foreign_key_checks = 1')
130
+ else
131
+ yield
132
+ end
133
+ end
117
134
  end
@@ -43,11 +43,10 @@ class ActiveRecordArchiver
43
43
  result = {}
44
44
 
45
45
  # serialize
46
- @models_hash.each_pair do |model, pair|
47
- records, attributes = pair
48
-
46
+ @models_hash.each_pair do |model, (records, attributes)|
47
+ next unless records.compact.present?
49
48
  result[model.to_s] = []
50
- records.each do |record|
49
+ [*records].each do |record|
51
50
 
52
51
  assert_instance_of record, model
53
52
 
@@ -92,17 +91,19 @@ class ActiveRecordArchiver
92
91
  if collection.is_a? ActiveRecord::Base
93
92
  options[[collection]] = options[collection]
94
93
  options.delete(collection)
94
+ elsif !collection.compact.present?
95
+ options.delete(collection)
95
96
  end
96
97
  end
97
98
 
98
- models = options.each_pair.map do |collection, _|
99
- collection.first.class
99
+ models = options.keys.map do |collection|
100
+ collection.first.class.base_class
100
101
  end
101
102
 
102
103
  models_hash = {}
103
104
 
104
105
  options.each_pair do |collection, cols|
105
- models_hash[collection.first.class] =
106
+ models_hash[collection.first.class.base_class] =
106
107
  [collection,
107
108
  if cols.is_a? Array then cols
108
109
  elsif cols == :all or cols.is_a? Hash
@@ -114,4 +115,4 @@ class ActiveRecordArchiver
114
115
 
115
116
  models_hash
116
117
  end
117
- end
118
+ end
@@ -16,34 +16,33 @@ Effects:
16
16
 
17
17
  class ActiveRecordArchiver
18
18
  def self.import json
19
-
20
19
  @data = JSON.parse json
21
20
 
22
21
  ActiveRecord::Base.transaction do
23
-
24
- # insert records
25
- @data.each_pair do |model_name, records|
26
- model = model_name.constantize
27
-
28
- assert_model model
22
+ without_foreign_key_constraints do
29
23
 
30
- records.each do |record|
31
- record[:id] = model.all.insert(insertable_hash(model, record))
24
+ # insert records
25
+ @data.each_pair do |model_name, records|
26
+ model = model_name.constantize
27
+
28
+ assert_model model
29
+
30
+ records.each do |record|
31
+ record[:id] = model.all.insert(insertable_hash(model, record))
32
+ end
32
33
  end
33
- end
34
-
35
- # add relations
36
- @data.each_pair do |model_name, records|
37
- model = model_name.constantize
38
34
 
39
- records.each do |record|
40
- if (update_hash = relations_update_hash(model, record))
41
- instance = model.find(record[:id])
42
- instance.update update_hash
35
+ # add relations
36
+ @data.each_pair do |model_name, records|
37
+ model = model_name.constantize
38
+
39
+ records.each do |record|
40
+ if (update_hash = relations_update_hash(model, record))
41
+ model.where(:id => record[:id]).update_all(update_hash)
42
+ end
43
43
  end
44
44
  end
45
45
  end
46
-
47
46
  end
48
47
 
49
48
  true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-archiver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Auciello