active_merge 1.1.0 → 1.2.0

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: 3a852fec8deb9d07be3fc9c377d1094a12a3574c
4
- data.tar.gz: 9dc95d04b550e50ffbf1250c11ee578a35c60729
3
+ metadata.gz: 8bde8bd1023ad75735b0b43d1590a3c5c850f1de
4
+ data.tar.gz: 59c0323922f36d6a90a261c47ecedf28fc054a5a
5
5
  SHA512:
6
- metadata.gz: 2c57dcfd9df06c76630812169fc38e815b059b33e23b6f9c623eb3324469177ed4f41384ac5eda12ed199e82770b5a4f0bbbcb3c55d239b7b946e6e364edfaf7
7
- data.tar.gz: 5a606c8a1aa4ade555780ecad0dee9e303ae76b3a05c118ce42150b4b8d46485ab82009aa3762d52380110772d6d6fca16828db7380e8b9e4177a803a63d1c65
6
+ metadata.gz: 0ee977c4db6b5322463aa3b2d5c044c117d89c17a348f690f2e0ddeac8901ebb1eff7427b553f0ed9ac350f0e06a9f410d5f9a1a5407e7f7a81b7a51c8daca65
7
+ data.tar.gz: a6876cd10e845f2abb6e34696652b45f69740b517dcf3259c8184c3e373e3ccbdca6ef3e69f1a97d54c2ec47273b002fbd1ffdc3dc88e5a8a1d7379e39b7ad23
data/CHANGELOG.rdoc CHANGED
@@ -1,5 +1,27 @@
1
1
  = Список изменений в версиях
2
2
 
3
+ == v.1.2.0
4
+
5
+ * Rebinding algorithm was changed. Now the links saved AFTER the second record
6
+ destroyed.
7
+
8
+ === Why (example)
9
+
10
+ Suppose we have named items and their synonyms:
11
+ * a synonym can be identical to the item (item "John" can have both "JONH" and
12
+ "John" in synonyms).
13
+ * a synonym cannot be equal to another item's name (if the "John" item
14
+ exists, the "John" cannot be added as a synonym to the "john" item).
15
+
16
+ Right algorithm of rebinding is:
17
+ * at first, the "John" synonym linked to the "john" item without saving to db;
18
+ * then the "John" item is deleted;
19
+ * at last, the "John" synonym (linked to "john" item) is saved to db.
20
+
21
+ == v.1.1.0
22
+
23
+ * Skipping validation added
24
+
3
25
  == v.1.0.4
4
26
 
5
27
  * The gem is allowed to use with rails 4.1
@@ -77,7 +77,7 @@ module ActiveMerge
77
77
  #
78
78
  # You can use the <tt>validate: false</tt> option.
79
79
  #
80
- # With this option set any activerecord validation and callback are skipped.
80
+ # With this option set any activerecord validation and callback skipped.
81
81
  #
82
82
  # Service.new(Kingdom.all).provide validate: false
83
83
  #
@@ -14,65 +14,58 @@ module ActiveMerge
14
14
  include ActiveModel::Validations
15
15
 
16
16
  def initialize(first, second)
17
-
18
- if first.class.ancestors.include?(ActiveRecord::Base) && first.persisted?
19
- @first = first
20
- end
21
-
22
- if @first && (second.class == @first.class) && second.persisted?
23
- @second = second
24
- end
17
+ @klass = first.class
18
+ return unless klass.ancestors.include?(ActiveRecord::Base) &&
19
+ second.is_a?(klass) && first.persisted? && second.persisted?
20
+ @first, @second = first, second
21
+ @links = find_links
25
22
  end
26
- attr_reader :first, :second
23
+ attr_reader :klass, :first, :second, :links
27
24
 
28
- validates :second, presence: true
25
+ validates :first, :second, presence: true
29
26
 
30
- def provide(options = {})
27
+ def provide(validate: true)
31
28
  transaction do
32
- Links.new(second).each { |item, key| rebind(item, key, options) }
33
- remove(options)
29
+ remove_second! validate
30
+ save_links! validate
34
31
  end
35
32
  end
36
33
 
37
34
  private
38
35
 
39
- # Initializes a hash, whose keys are instances linked to given one,
40
- # and their values are names of methods for linking to another object.
41
- class Links
42
- class << self
43
-
44
- def new(item)
45
- @item = item
46
- return hash
47
- end
48
-
49
- # Returns a hash whose keys are "has_many" associations' names
50
- # and their values are corresponding foreign keys
51
- def refs
52
- @item.class.reflect_on_all_associations(:has_many).
53
- inject({}){ |hash, item| hash.merge(item.name => item.foreign_key) }
54
- end
55
-
56
- def hash
57
- @hash = {}
58
- refs.each do |name, foreign_key|
59
- @item.send(name).each{ |item| @hash[item] = "#{ foreign_key }=" }
60
- end
61
- @hash
36
+ # Finds links to the second record and rebinds it to the first one.
37
+ # Doesn't validate and save links yet!
38
+ def find_links
39
+ return [] unless first && second
40
+ klass.reflect_on_all_associations(:has_many).inject([]) do |arr, item|
41
+ arr + second.send(item.name).map do |link|
42
+ link.send "#{ item.foreign_key }=", first.id
43
+ link
62
44
  end
63
45
  end
64
46
  end
65
47
 
66
- # Deletes or destroys second object
67
- def remove(validate: true)
68
- change(second) { validate ? second.destroy! : second.delete }
69
- end
48
+ # Removes the second record.
49
+ # Any errors are moved to the service.
50
+ def remove_second!(validate)
51
+ begin
52
+ validate ? second.destroy! : second.delete
53
+ rescue => error
54
+ second.errors.each{ |_, message| errors.add :second, message }
55
+ raise error
56
+ end
57
+ end
70
58
 
71
- # Re-assigns given object to the #first
72
- def rebind(item, key, options)
73
- change item do
74
- item.send key, first.id
75
- item.save! options
59
+ # Saves changed links.
60
+ # Any errors are moved to the service.
61
+ def save_links!(validate)
62
+ links.each do |link|
63
+ begin
64
+ validate ? link.save! : link.save(validate: false)
65
+ rescue => error
66
+ link.errors.each{ |_, message| errors.add :base, message }
67
+ raise error
68
+ end
76
69
  end
77
70
  end
78
71
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveMerge
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -147,9 +147,9 @@ module ActiveMerge
147
147
  let!(:men) { lords.map{ |lord| lord.men.create! }}
148
148
  let!(:service) { Service.new lords }
149
149
 
150
- context "if :validate option isn't set" do
150
+ context "(validate: true)" do
151
151
 
152
- context "and no exceptions raised:" do
152
+ context "if no exceptions raised:" do
153
153
 
154
154
  it "merges the objects" do
155
155
  service.provide
@@ -159,7 +159,7 @@ module ActiveMerge
159
159
  end
160
160
  end
161
161
 
162
- context "and the service's own validation returns false:" do
162
+ context "if service validation fails" do
163
163
 
164
164
  before { service.stub(:valid?).and_return false }
165
165
 
@@ -174,7 +174,7 @@ module ActiveMerge
174
174
  end
175
175
  end
176
176
 
177
- context "and a validation returns false when rebinding a link:" do
177
+ context "if a link validation fails" do
178
178
 
179
179
  before do
180
180
  Man.any_instance.stub(:valid?).and_return false
@@ -197,7 +197,7 @@ module ActiveMerge
197
197
  end
198
198
  end
199
199
 
200
- context "and a callback returns false when destroying an object:" do
200
+ context "if a destroy fails" do
201
201
 
202
202
  before do
203
203
  Lord.before_destroy(){ false }
@@ -222,9 +222,9 @@ module ActiveMerge
222
222
  end
223
223
  end
224
224
 
225
- context "if :validate option is set to false" do
225
+ context "(validate: false)" do
226
226
 
227
- context "and no exceptions raised:" do
227
+ context "if no exceptions raised" do
228
228
 
229
229
  it "merges the objects" do
230
230
  service.provide validate: false
@@ -234,7 +234,7 @@ module ActiveMerge
234
234
  end
235
235
  end
236
236
 
237
- context "and a validation returns false when rebinding a link:" do
237
+ context "if a link validation fails" do
238
238
 
239
239
  before do
240
240
  Man.any_instance.stub(:valid?).and_return false
@@ -249,7 +249,7 @@ module ActiveMerge
249
249
  end
250
250
  end
251
251
 
252
- context "and a callback returns false when destroying an object:" do
252
+ context "if destroy validation fails" do
253
253
 
254
254
  before do
255
255
  Lord.before_destroy(){ false }
@@ -265,7 +265,7 @@ module ActiveMerge
265
265
  end
266
266
  end
267
267
 
268
- context "and the service's own validation returns false:" do
268
+ context "if service validation fails" do
269
269
 
270
270
  before { service.stub(:valid?).and_return false }
271
271
 
@@ -280,10 +280,10 @@ module ActiveMerge
280
280
  end
281
281
  end
282
282
 
283
- context "and db recording fails when rebinding a link:" do
283
+ context "if a db request fails" do
284
284
 
285
285
  before do
286
- Man.any_instance.stub(:save!){ fail }
286
+ Man.any_instance.stub(:save){ fail }
287
287
  Man.any_instance.stub(:errors).and_return({ base: "record_invalid" })
288
288
  end
289
289
 
@@ -48,6 +48,23 @@ module ActiveMerge
48
48
  end
49
49
  end
50
50
 
51
+ describe "#links" do
52
+
53
+ let!(:lords) { 2.times.map{ Lord.create }}
54
+ let!(:domain) { lords.last.domains.create! }
55
+ let!(:man) { lords.last.men.create! }
56
+ let!(:service){ SimpleService.new lords.first, lords.last }
57
+
58
+ it "contains links rebinded from the second record to the first one" do
59
+ links = service.links
60
+ links.should include(domain)
61
+ links.should include(man)
62
+ service.links.each do |link|
63
+ link.lord_id.should eq lords.first.id
64
+ end
65
+ end
66
+ end
67
+
51
68
  describe "#valid?" do
52
69
 
53
70
  it "возвращает true если #second инициализирован" do
File without changes
Binary file
@@ -36,3 +36,239 @@ Migrating to CreateDomains (20140416183059)
36
36
  FROM sqlite_temp_master
37
37
  WHERE name='index_men_on_lord_id' AND type='index'
38
38
  
39
+  (0.0ms) begin transaction
40
+  (0.0ms) commit transaction
41
+  (0.0ms) begin transaction
42
+  (0.1ms) rollback transaction
43
+  (0.0ms) begin transaction
44
+  (0.0ms) commit transaction
45
+  (0.0ms) begin transaction
46
+  (0.0ms) rollback transaction
47
+  (0.0ms) begin transaction
48
+  (0.0ms) commit transaction
49
+  (0.0ms) begin transaction
50
+  (0.0ms) rollback transaction
51
+  (0.0ms) begin transaction
52
+  (0.0ms) commit transaction
53
+  (0.0ms) begin transaction
54
+  (0.0ms) rollback transaction
55
+  (0.0ms) begin transaction
56
+  (0.0ms) commit transaction
57
+  (0.0ms) begin transaction
58
+  (0.0ms) rollback transaction
59
+  (0.0ms) begin transaction
60
+  (0.0ms) commit transaction
61
+  (0.0ms) begin transaction
62
+  (0.0ms) rollback transaction
63
+  (0.0ms) begin transaction
64
+  (0.0ms) commit transaction
65
+  (0.0ms) begin transaction
66
+  (0.0ms) rollback transaction
67
+  (0.0ms) begin transaction
68
+  (0.0ms) commit transaction
69
+  (0.0ms) begin transaction
70
+  (0.0ms) rollback transaction
71
+  (0.0ms) begin transaction
72
+  (0.0ms) commit transaction
73
+  (0.0ms) begin transaction
74
+  (0.0ms) rollback transaction
75
+  (0.0ms) begin transaction
76
+  (0.0ms) commit transaction
77
+  (0.0ms) begin transaction
78
+  (0.0ms) rollback transaction
79
+  (0.0ms) begin transaction
80
+  (0.1ms) commit transaction
81
+  (0.0ms) begin transaction
82
+  (0.0ms) rollback transaction
83
+  (0.0ms) begin transaction
84
+  (0.0ms) commit transaction
85
+  (0.0ms) begin transaction
86
+  (0.0ms) rollback transaction
87
+  (0.0ms) begin transaction
88
+  (0.0ms) commit transaction
89
+  (0.0ms) begin transaction
90
+  (0.0ms) rollback transaction
91
+  (0.0ms) begin transaction
92
+  (0.0ms) commit transaction
93
+  (0.1ms) begin transaction
94
+  (0.0ms) rollback transaction
95
+  (0.0ms) begin transaction
96
+  (0.0ms) commit transaction
97
+  (0.0ms) begin transaction
98
+  (0.0ms) rollback transaction
99
+  (0.0ms) begin transaction
100
+  (0.0ms) commit transaction
101
+  (0.0ms) begin transaction
102
+  (0.0ms) rollback transaction
103
+  (0.0ms) begin transaction
104
+  (0.0ms) commit transaction
105
+  (0.0ms) begin transaction
106
+  (0.0ms) rollback transaction
107
+  (0.0ms) begin transaction
108
+  (0.0ms) commit transaction
109
+  (0.0ms) begin transaction
110
+  (0.0ms) rollback transaction
111
+  (0.0ms) begin transaction
112
+  (0.0ms) commit transaction
113
+  (0.0ms) begin transaction
114
+  (0.0ms) rollback transaction
115
+  (0.0ms) begin transaction
116
+  (0.0ms) commit transaction
117
+  (0.0ms) begin transaction
118
+  (0.0ms) rollback transaction
119
+  (0.0ms) begin transaction
120
+  (0.0ms) commit transaction
121
+  (0.0ms) begin transaction
122
+  (0.0ms) rollback transaction
123
+  (0.0ms) begin transaction
124
+  (0.0ms) commit transaction
125
+  (0.0ms) begin transaction
126
+  (0.0ms) rollback transaction
127
+  (0.0ms) begin transaction
128
+  (0.0ms) commit transaction
129
+  (0.0ms) begin transaction
130
+  (0.0ms) rollback transaction
131
+  (0.0ms) begin transaction
132
+  (0.0ms) commit transaction
133
+  (0.0ms) begin transaction
134
+  (0.0ms) rollback transaction
135
+  (0.0ms) begin transaction
136
+  (0.0ms) commit transaction
137
+  (0.0ms) begin transaction
138
+  (0.0ms) rollback transaction
139
+  (0.0ms) begin transaction
140
+  (0.0ms) commit transaction
141
+  (0.1ms) begin transaction
142
+  (0.0ms) rollback transaction
143
+  (0.0ms) begin transaction
144
+  (0.0ms) commit transaction
145
+  (0.0ms) begin transaction
146
+  (0.0ms) rollback transaction
147
+  (0.0ms) begin transaction
148
+  (0.0ms) commit transaction
149
+  (0.0ms) begin transaction
150
+  (0.0ms) rollback transaction
151
+  (0.0ms) begin transaction
152
+  (0.0ms) commit transaction
153
+  (0.0ms) begin transaction
154
+  (0.0ms) rollback transaction
155
+  (0.0ms) begin transaction
156
+  (0.0ms) commit transaction
157
+  (0.0ms) begin transaction
158
+  (0.0ms) rollback transaction
159
+  (0.0ms) begin transaction
160
+  (0.0ms) commit transaction
161
+  (0.0ms) begin transaction
162
+  (0.0ms) rollback transaction
163
+  (0.0ms) begin transaction
164
+  (0.0ms) commit transaction
165
+  (0.0ms) begin transaction
166
+  (0.1ms) rollback transaction
167
+  (0.0ms) begin transaction
168
+  (0.0ms) commit transaction
169
+  (0.0ms) begin transaction
170
+  (0.0ms) rollback transaction
171
+  (0.0ms) begin transaction
172
+  (0.0ms) commit transaction
173
+  (0.0ms) begin transaction
174
+  (0.0ms) rollback transaction
175
+  (0.0ms) begin transaction
176
+  (0.0ms) commit transaction
177
+  (0.0ms) begin transaction
178
+  (0.0ms) rollback transaction
179
+  (0.0ms) begin transaction
180
+  (0.0ms) commit transaction
181
+  (0.0ms) begin transaction
182
+  (0.0ms) rollback transaction
183
+  (0.0ms) begin transaction
184
+  (0.0ms) commit transaction
185
+  (0.0ms) begin transaction
186
+  (0.0ms) rollback transaction
187
+  (0.0ms) begin transaction
188
+  (0.0ms) commit transaction
189
+  (0.0ms) begin transaction
190
+  (0.0ms) rollback transaction
191
+  (0.0ms) begin transaction
192
+  (0.0ms) commit transaction
193
+  (0.0ms) begin transaction
194
+  (0.0ms) rollback transaction
195
+  (0.0ms) begin transaction
196
+  (0.0ms) commit transaction
197
+  (0.0ms) begin transaction
198
+  (0.0ms) rollback transaction
199
+  (0.0ms) begin transaction
200
+  (0.0ms) commit transaction
201
+  (0.0ms) begin transaction
202
+  (0.0ms) rollback transaction
203
+  (0.0ms) begin transaction
204
+  (0.0ms) commit transaction
205
+  (0.0ms) begin transaction
206
+  (0.0ms) rollback transaction
207
+  (0.0ms) begin transaction
208
+  (0.0ms) commit transaction
209
+  (0.0ms) begin transaction
210
+  (0.0ms) rollback transaction
211
+  (0.0ms) begin transaction
212
+  (0.0ms) commit transaction
213
+  (0.0ms) begin transaction
214
+  (0.0ms) rollback transaction
215
+  (0.0ms) begin transaction
216
+  (0.0ms) commit transaction
217
+  (0.0ms) begin transaction
218
+  (0.0ms) rollback transaction
219
+  (0.0ms) begin transaction
220
+  (0.0ms) commit transaction
221
+  (0.0ms) begin transaction
222
+  (0.0ms) rollback transaction
223
+  (0.0ms) begin transaction
224
+  (0.0ms) commit transaction
225
+  (0.0ms) begin transaction
226
+  (0.0ms) rollback transaction
227
+  (0.0ms) begin transaction
228
+  (0.0ms) commit transaction
229
+  (0.0ms) begin transaction
230
+  (0.0ms) rollback transaction
231
+  (0.0ms) begin transaction
232
+  (0.0ms) commit transaction
233
+  (0.0ms) begin transaction
234
+  (0.0ms) rollback transaction
235
+  (0.0ms) begin transaction
236
+  (0.0ms) commit transaction
237
+  (0.0ms) begin transaction
238
+  (0.0ms) rollback transaction
239
+  (0.0ms) begin transaction
240
+  (0.0ms) commit transaction
241
+  (0.0ms) begin transaction
242
+  (0.0ms) rollback transaction
243
+  (0.0ms) begin transaction
244
+  (0.0ms) commit transaction
245
+  (0.0ms) begin transaction
246
+  (0.0ms) rollback transaction
247
+  (0.0ms) begin transaction
248
+  (0.0ms) commit transaction
249
+  (0.0ms) begin transaction
250
+  (0.0ms) rollback transaction
251
+  (0.0ms) begin transaction
252
+  (0.0ms) commit transaction
253
+  (0.0ms) begin transaction
254
+  (0.0ms) rollback transaction
255
+  (0.0ms) begin transaction
256
+  (0.0ms) commit transaction
257
+  (0.0ms) begin transaction
258
+  (0.0ms) rollback transaction
259
+  (0.0ms) begin transaction
260
+  (0.0ms) commit transaction
261
+  (0.0ms) begin transaction
262
+  (0.0ms) rollback transaction
263
+  (0.1ms) begin transaction
264
+  (0.0ms) commit transaction
265
+  (0.0ms) begin transaction
266
+  (0.0ms) rollback transaction
267
+  (0.0ms) begin transaction
268
+  (0.0ms) commit transaction
269
+  (0.0ms) begin transaction
270
+  (0.0ms) rollback transaction
271
+  (0.0ms) begin transaction
272
+  (0.0ms) commit transaction
273
+  (0.0ms) begin transaction
274
+  (0.0ms) rollback transaction