table_differ 0.6.4 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -0
- data/lib/table_differ/version.rb +1 -1
- data/lib/table_differ.rb +13 -7
- data/spec/remap_spec.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 144d5fbf2df2d63302aba0a2492629817f6d2c56
|
4
|
+
data.tar.gz: c2f2672f06a326665b9b1ad69cb816d765d6c105
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dac4225f3b0c3b3994342f1ed435c4b6ae4503b3963e60d21434f974c13b12622ce421d96f981156d63b59a94d5ce2be7f6dc7e0b225d14e00581c8423d86963
|
7
|
+
data.tar.gz: d9371103866e97e4292b9fb7d1cb850737a8d75e34ffe2869dd11072f58feb60280fa32d40b86f8499a7da218b3f2f5ada03770191976ea79873458f377d1d93
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
**0.6.0** 2 July 2014
|
4
4
|
|
5
|
+
* Removed models with unique_by are returned with their old ID intact.
|
5
6
|
* Added the `unique_by` option to discover changes even when ignoring the primary key.
|
6
7
|
* original_attributes now contains all original attributes, not just the changed ones.
|
7
8
|
* Added the ability to delete all snapshots: `delete_snapshots(:all)`
|
data/lib/table_differ/version.rb
CHANGED
data/lib/table_differ.rb
CHANGED
@@ -50,13 +50,19 @@ module TableDiffer
|
|
50
50
|
snaps.each { |name| delete_snapshot(name) }
|
51
51
|
end
|
52
52
|
|
53
|
-
def table_differ_remap_objects params, records
|
53
|
+
def table_differ_remap_objects params, records, table
|
54
|
+
model = self
|
55
|
+
if table != table_name
|
56
|
+
model = self.dup
|
57
|
+
model.table_name = table
|
58
|
+
end
|
59
|
+
|
54
60
|
params = Array(params)
|
55
61
|
records.map do |record|
|
56
62
|
result = record
|
57
63
|
if record.id.nil? # don't look up real ActiveRecord object if we already have one
|
58
64
|
args = params.inject({}) { |hash,key| hash[key] = record[key]; hash }
|
59
|
-
real_record = where(args).first
|
65
|
+
real_record = model.where(args).first
|
60
66
|
if real_record
|
61
67
|
real_record.original_attributes = record.attributes
|
62
68
|
result = real_record
|
@@ -90,19 +96,19 @@ module TableDiffer
|
|
90
96
|
# [*added, *removed].select { |o| !o.id }.each { |o| o.instance_variable_set("@new_record", true) }
|
91
97
|
|
92
98
|
if options[:unique_by]
|
93
|
-
added = table_differ_remap_objects(options[:unique_by], added)
|
94
|
-
removed = table_differ_remap_objects(options[:unique_by], removed)
|
99
|
+
added = table_differ_remap_objects(options[:unique_by], added, newtable)
|
100
|
+
removed = table_differ_remap_objects(options[:unique_by], removed, oldtable)
|
95
101
|
end
|
96
102
|
|
97
|
-
changed = added
|
103
|
+
changed = added.select { |a| removed.find { |r| a.id == r.id }}
|
98
104
|
changed.each do |obj|
|
99
|
-
orig = removed.find { |r| r == obj }
|
105
|
+
orig = removed.find { |r| r.id == obj.id }
|
100
106
|
raise "this is impossible" if orig.nil?
|
101
107
|
obj.original_attributes = (orig.original_attributes || orig.attributes).except(*ignore)
|
102
108
|
end
|
103
109
|
|
104
110
|
added -= changed
|
105
|
-
removed
|
111
|
+
removed.reject! { |r| changed.find { |c| r.id == c.id }}
|
106
112
|
[*added, *removed].each { |o| o.original_attributes = nil }
|
107
113
|
|
108
114
|
[added, removed, changed]
|
data/spec/remap_spec.rb
CHANGED
@@ -4,6 +4,7 @@ describe "diffing a model" do
|
|
4
4
|
it "detects a changed field using a single surrogate" do
|
5
5
|
first = SurrogateModel.create!(name: 'one', original_name: 'one')
|
6
6
|
second = SurrogateModel.create!(name: 'two', original_name: 'two')
|
7
|
+
secid = second.id
|
7
8
|
|
8
9
|
SurrogateModel.create_snapshot('original')
|
9
10
|
|
@@ -16,7 +17,7 @@ describe "diffing a model" do
|
|
16
17
|
# we can find added and changed records by surrogate IDs but, of course, can't find removed ones
|
17
18
|
expect(added).to eq [third]
|
18
19
|
expect(added.first.original_attributes).to eq nil
|
19
|
-
expect(removed.map(&:attributes)).to eq [{"id" =>
|
20
|
+
expect(removed.map(&:attributes)).to eq [{"id" => secid, "name" => "two", "original_name" => "two", "alternate_value" => nil}]
|
20
21
|
expect(removed.first.original_attributes).to eq nil
|
21
22
|
expect(changed).to eq [first]
|
22
23
|
expect(changed.first.name).to eq 'uno'
|