active_data 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +13 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +53 -0
  5. data/.rvmrc +1 -1
  6. data/.travis.yml +15 -2
  7. data/Appraisals +1 -1
  8. data/CHANGELOG.md +31 -0
  9. data/Guardfile +8 -8
  10. data/README.md +256 -0
  11. data/Rakefile +2 -4
  12. data/active_data.gemspec +8 -7
  13. data/gemfiles/rails.4.0.gemfile +1 -1
  14. data/gemfiles/rails.4.1.gemfile +1 -1
  15. data/gemfiles/rails.4.2.gemfile +1 -1
  16. data/gemfiles/rails.5.0.gemfile +1 -1
  17. data/gemfiles/rails.5.1.gemfile +14 -0
  18. data/lib/active_data/active_record/associations.rb +18 -13
  19. data/lib/active_data/active_record/nested_attributes.rb +8 -14
  20. data/lib/active_data/base.rb +13 -0
  21. data/lib/active_data/config.rb +4 -4
  22. data/lib/active_data/errors.rb +29 -13
  23. data/lib/active_data/extensions.rb +22 -21
  24. data/lib/active_data/model/associations/base.rb +22 -6
  25. data/lib/active_data/model/associations/embeds_any.rb +17 -0
  26. data/lib/active_data/model/associations/embeds_many.rb +29 -19
  27. data/lib/active_data/model/associations/embeds_one.rb +30 -26
  28. data/lib/active_data/model/associations/nested_attributes.rb +82 -50
  29. data/lib/active_data/model/associations/persistence_adapters/active_record/referenced_proxy.rb +31 -0
  30. data/lib/active_data/model/associations/persistence_adapters/active_record.rb +66 -0
  31. data/lib/active_data/model/associations/persistence_adapters/base.rb +53 -0
  32. data/lib/active_data/model/associations/references_any.rb +41 -0
  33. data/lib/active_data/model/associations/references_many.rb +51 -37
  34. data/lib/active_data/model/associations/references_one.rb +43 -41
  35. data/lib/active_data/model/associations/reflections/base.rb +19 -29
  36. data/lib/active_data/model/associations/reflections/embeds_any.rb +43 -0
  37. data/lib/active_data/model/associations/reflections/embeds_many.rb +3 -13
  38. data/lib/active_data/model/associations/reflections/embeds_one.rb +5 -37
  39. data/lib/active_data/model/associations/reflections/references_any.rb +62 -0
  40. data/lib/active_data/model/associations/reflections/references_many.rb +7 -7
  41. data/lib/active_data/model/associations/reflections/references_one.rb +9 -7
  42. data/lib/active_data/model/associations/reflections/singular.rb +35 -0
  43. data/lib/active_data/model/associations/validations.rb +2 -27
  44. data/lib/active_data/model/associations.rb +12 -10
  45. data/lib/active_data/model/attributes/attribute.rb +10 -10
  46. data/lib/active_data/model/attributes/base.rb +8 -7
  47. data/lib/active_data/model/attributes/localized.rb +4 -4
  48. data/lib/active_data/model/attributes/reference_many.rb +6 -8
  49. data/lib/active_data/model/attributes/reference_one.rb +17 -9
  50. data/lib/active_data/model/attributes/reflections/attribute.rb +2 -2
  51. data/lib/active_data/model/attributes/reflections/base.rb +8 -11
  52. data/lib/active_data/model/attributes/reflections/localized.rb +2 -2
  53. data/lib/active_data/model/attributes/reflections/reference_one.rb +11 -22
  54. data/lib/active_data/model/attributes/reflections/represents.rb +5 -6
  55. data/lib/active_data/model/attributes/represents.rb +6 -5
  56. data/lib/active_data/model/attributes.rb +33 -87
  57. data/lib/active_data/model/callbacks.rb +6 -7
  58. data/lib/active_data/model/conventions.rb +2 -0
  59. data/lib/active_data/model/dirty.rb +4 -4
  60. data/lib/active_data/model/lifecycle.rb +18 -20
  61. data/lib/active_data/model/localization.rb +5 -2
  62. data/lib/active_data/model/persistence.rb +2 -2
  63. data/lib/active_data/model/primary.rb +19 -14
  64. data/lib/active_data/model/representation.rb +81 -0
  65. data/lib/active_data/model/scopes.rb +22 -12
  66. data/lib/active_data/model/validations/associated.rb +3 -2
  67. data/lib/active_data/model/validations/nested.rb +6 -1
  68. data/lib/active_data/model/validations.rb +3 -3
  69. data/lib/active_data/model.rb +2 -1
  70. data/lib/active_data/undefined_class.rb +9 -0
  71. data/lib/active_data/version.rb +1 -1
  72. data/lib/active_data.rb +40 -17
  73. data/spec/lib/active_data/active_record/associations_spec.rb +107 -45
  74. data/spec/lib/active_data/active_record/nested_attributes_spec.rb +1 -2
  75. data/spec/lib/active_data/config_spec.rb +37 -15
  76. data/spec/lib/active_data/model/associations/embeds_many_spec.rb +475 -172
  77. data/spec/lib/active_data/model/associations/embeds_one_spec.rb +353 -96
  78. data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +108 -12
  79. data/spec/lib/active_data/model/associations/persistence_adapters/active_record_spec.rb +58 -0
  80. data/spec/lib/active_data/model/associations/references_many_spec.rb +440 -64
  81. data/spec/lib/active_data/model/associations/references_one_spec.rb +347 -36
  82. data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +8 -7
  83. data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +7 -6
  84. data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +81 -33
  85. data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +116 -37
  86. data/spec/lib/active_data/model/associations/validations_spec.rb +27 -43
  87. data/spec/lib/active_data/model/associations_spec.rb +34 -25
  88. data/spec/lib/active_data/model/attributes/attribute_spec.rb +26 -23
  89. data/spec/lib/active_data/model/attributes/base_spec.rb +5 -6
  90. data/spec/lib/active_data/model/attributes/collection_spec.rb +7 -8
  91. data/spec/lib/active_data/model/attributes/dictionary_spec.rb +40 -33
  92. data/spec/lib/active_data/model/attributes/localized_spec.rb +27 -28
  93. data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +6 -6
  94. data/spec/lib/active_data/model/attributes/represents_spec.rb +10 -78
  95. data/spec/lib/active_data/model/attributes_spec.rb +150 -45
  96. data/spec/lib/active_data/model/callbacks_spec.rb +69 -70
  97. data/spec/lib/active_data/model/conventions_spec.rb +0 -1
  98. data/spec/lib/active_data/model/dirty_spec.rb +22 -13
  99. data/spec/lib/active_data/model/lifecycle_spec.rb +49 -23
  100. data/spec/lib/active_data/model/persistence_spec.rb +5 -6
  101. data/spec/lib/active_data/model/representation_spec.rb +126 -0
  102. data/spec/lib/active_data/model/scopes_spec.rb +1 -3
  103. data/spec/lib/active_data/model/typecasting_spec.rb +6 -5
  104. data/spec/lib/active_data/model/validations/associated_spec.rb +26 -18
  105. data/spec/lib/active_data/model/validations/nested_spec.rb +89 -18
  106. data/spec/lib/active_data/model_spec.rb +1 -2
  107. data/spec/lib/active_data_spec.rb +0 -1
  108. data/spec/shared/nested_attribute_examples.rb +332 -0
  109. data/spec/spec_helper.rb +3 -0
  110. data/spec/support/model_helpers.rb +2 -2
  111. data/spec/support/muffle_helper.rb +7 -0
  112. metadata +52 -18
  113. data/lib/active_data/model/associations/collection/referenced.rb +0 -26
  114. data/lib/active_data/model/associations/reflections/reference_reflection.rb +0 -45
  115. data/spec/lib/active_data/model/nested_attributes.rb +0 -202
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Associations::EmbedsOne do
@@ -19,26 +18,161 @@ describe ActiveData::Model::Associations::EmbedsOne do
19
18
  end
20
19
  end
21
20
 
22
- let(:book) { Book.new }
21
+ let(:book) { Book.new(title: 'Book') }
23
22
  let(:association) { book.association(:author) }
24
23
 
25
24
  let(:existing_book) { Book.instantiate title: 'My Life', author: {'name' => 'Johny'} }
26
25
  let(:existing_association) { existing_book.association(:author) }
27
26
 
27
+ context 'callbacks' do
28
+ before do
29
+ Book.class_eval do
30
+ embeds_one :author, before_add: :before_add, after_add: :after_add
31
+
32
+ def before_add(object)
33
+ callbacks.push([:before_add, object])
34
+ end
35
+
36
+ def after_add(object)
37
+ callbacks.push([:after_add, object])
38
+ end
39
+
40
+ collection :callbacks, Array
41
+ end
42
+ end
43
+ let(:author1) { Author.new(name: 'Author1') }
44
+ let(:author2) { Author.new(name: 'Author2') }
45
+
46
+ specify do
47
+ expect { association.build(name: 'Author1') }
48
+ .to change { book.callbacks }
49
+ .to([[:before_add, author1], [:after_add, author1]])
50
+ end
51
+
52
+ specify do
53
+ expect do
54
+ association.build(name: 'Author1')
55
+ association.build(name: 'Author2')
56
+ end
57
+ .to change { book.callbacks }
58
+ .to([
59
+ [:before_add, author1], [:after_add, author1],
60
+ [:before_add, author2], [:after_add, author2]
61
+ ])
62
+ end
63
+
64
+ specify do
65
+ expect { association.create(name: 'Author1') }
66
+ .to change { book.callbacks }
67
+ .to([[:before_add, author1], [:after_add, author1]])
68
+ end
69
+
70
+ specify do
71
+ expect { association.writer(author1) }
72
+ .to change { book.callbacks }
73
+ .to([[:before_add, author1], [:after_add, author1]])
74
+ end
75
+
76
+ specify do
77
+ expect do
78
+ association.writer(author1)
79
+ association.writer(nil)
80
+ association.writer(author1)
81
+ end
82
+ .to change { book.callbacks }
83
+ .to([
84
+ [:before_add, author1], [:after_add, author1],
85
+ [:before_add, author1], [:after_add, author1]
86
+ ])
87
+ end
88
+
89
+ context 'default' do
90
+ before do
91
+ Book.class_eval do
92
+ embeds_one :author,
93
+ before_add: ->(object) { callbacks.push([:before_add, object]) },
94
+ after_add: ->(object) { callbacks.push([:after_add, object]) },
95
+ default: -> { {name: 'Author1'} }
96
+
97
+ collection :callbacks, Array
98
+ end
99
+ end
100
+
101
+ specify do
102
+ expect { association.writer(author2) }
103
+ .to change { book.callbacks }
104
+ .to([
105
+ [:before_add, author1], [:after_add, author1],
106
+ [:before_add, author2], [:after_add, author2]
107
+ ])
108
+ end
109
+ end
110
+ end
111
+
28
112
  describe 'book#association' do
29
113
  specify { expect(association).to be_a described_class }
30
114
  specify { expect(association).to eq(book.association(:author)) }
31
115
  end
32
116
 
117
+ describe 'author#embedder' do
118
+ let(:author) { Author.new(name: 'Author') }
119
+
120
+ specify { expect(association.build.embedder).to eq(book) }
121
+ specify { expect(association.create.embedder).to eq(book) }
122
+ specify do
123
+ expect { association.writer(author) }
124
+ .to change { author.embedder }.from(nil).to(book)
125
+ end
126
+ specify do
127
+ expect { association.target = author }
128
+ .to change { author.embedder }.from(nil).to(book)
129
+ end
130
+
131
+ context 'default' do
132
+ before do
133
+ Book.class_eval do
134
+ embeds_one :author, default: -> { {name: 'Author1'} }
135
+ end
136
+ end
137
+
138
+ specify { expect(association.target.embedder).to eq(book) }
139
+
140
+ context do
141
+ before do
142
+ Book.class_eval do
143
+ embeds_one :author, default: -> { Author.new(name: 'Author1') }
144
+ end
145
+ end
146
+
147
+ specify { expect(association.target.embedder).to eq(book) }
148
+ end
149
+ end
150
+
151
+ context 'embedding goes before attributes' do
152
+ before do
153
+ Author.class_eval do
154
+ attribute :name, String, normalize: ->(value) { "#{value}#{embedder.title}" }
155
+ end
156
+ end
157
+
158
+ specify { expect(association.build(name: 'Author').name).to eq('AuthorBook') }
159
+ specify { expect(association.create(name: 'Author').name).to eq('AuthorBook') }
160
+ end
161
+ end
162
+
33
163
  describe '#build' do
34
164
  specify { expect(association.build).to be_a Author }
35
165
  specify { expect(association.build).not_to be_persisted }
36
166
 
37
- specify { expect { association.build(name: 'Fred') }
38
- .not_to change { book.read_attribute(:author) } }
167
+ specify do
168
+ expect { association.build(name: 'Fred') }
169
+ .not_to change { book.read_attribute(:author) }
170
+ end
39
171
 
40
- specify { expect { existing_association.build(name: 'Fred') }
41
- .not_to change { existing_book.read_attribute(:author) } }
172
+ specify do
173
+ expect { existing_association.build(name: 'Fred') }
174
+ .not_to change { existing_book.read_attribute(:author) }
175
+ end
42
176
  end
43
177
 
44
178
  describe '#create' do
@@ -48,52 +182,123 @@ describe ActiveData::Model::Associations::EmbedsOne do
48
182
  specify { expect(association.create(name: 'Fred')).to be_a Author }
49
183
  specify { expect(association.create(name: 'Fred')).to be_persisted }
50
184
 
51
- specify { expect { association.create }
52
- .not_to change { book.read_attribute(:author) } }
53
- specify { expect { association.create(name: 'Fred') }
54
- .to change { book.read_attribute(:author) }.from(nil).to('name' => 'Fred') }
185
+ specify do
186
+ expect { association.create }
187
+ .not_to change { book.read_attribute(:author) }
188
+ end
189
+ specify do
190
+ expect { association.create(name: 'Fred') }
191
+ .to change { book.read_attribute(:author) }
192
+ .from(nil).to('name' => 'Fred')
193
+ end
55
194
 
56
- specify { expect { existing_association.create }
57
- .not_to change { existing_book.read_attribute(:author) } }
58
- specify { expect { existing_association.create(name: 'Fred') }
59
- .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to('name' => 'Fred') }
195
+ specify do
196
+ expect { existing_association.create }
197
+ .not_to change { existing_book.read_attribute(:author) }
198
+ end
199
+ specify do
200
+ expect { existing_association.create(name: 'Fred') }
201
+ .to change { existing_book.read_attribute(:author) }
202
+ .from('name' => 'Johny').to('name' => 'Fred')
203
+ end
60
204
  end
61
205
 
62
206
  describe '#create!' do
63
207
  specify { expect { association.create! }.to raise_error ActiveData::ValidationError }
208
+ specify do
209
+ expect { muffle(ActiveData::ValidationError) { association.create! } }
210
+ .to change { association.target }
211
+ .from(nil).to(an_instance_of(Author))
212
+ end
64
213
 
65
214
  specify { expect(association.create!(name: 'Fred')).to be_a Author }
66
215
  specify { expect(association.create!(name: 'Fred')).to be_persisted }
67
216
 
68
- specify { expect { association.create! rescue nil }
69
- .not_to change { book.read_attribute(:author) } }
70
- specify { expect { association.create! rescue nil }
71
- .to change { association.reader.try(:attributes) }.from(nil).to('name' => nil) }
72
- specify { expect { association.create(name: 'Fred') }
73
- .to change { book.read_attribute(:author) }.from(nil).to('name' => 'Fred') }
74
-
75
- specify { expect { existing_association.create! rescue nil }
76
- .not_to change { existing_book.read_attribute(:author) } }
77
- specify { expect { existing_association.create! rescue nil }
78
- .to change { existing_association.reader.try(:attributes) }.from('name' => 'Johny').to('name' => nil) }
79
- specify { expect { existing_association.create!(name: 'Fred') }
80
- .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to('name' => 'Fred') }
217
+ specify do
218
+ expect { muffle(ActiveData::ValidationError) { association.create! } }
219
+ .not_to change { book.read_attribute(:author) }
220
+ end
221
+ specify do
222
+ expect { muffle(ActiveData::ValidationError) { association.create! } }
223
+ .to change { association.reader.try(:attributes) }
224
+ .from(nil).to('name' => nil)
225
+ end
226
+ specify do
227
+ expect { association.create(name: 'Fred') }
228
+ .to change { book.read_attribute(:author) }
229
+ .from(nil).to('name' => 'Fred')
230
+ end
231
+
232
+ specify do
233
+ expect { muffle(ActiveData::ValidationError) { existing_association.create! } }
234
+ .not_to change { existing_book.read_attribute(:author) }
235
+ end
236
+ specify do
237
+ expect { muffle(ActiveData::ValidationError) { existing_association.create! } }
238
+ .to change { existing_association.reader.try(:attributes) }
239
+ .from('name' => 'Johny').to('name' => nil)
240
+ end
241
+ specify do
242
+ expect { existing_association.create!(name: 'Fred') }
243
+ .to change { existing_book.read_attribute(:author) }
244
+ .from('name' => 'Johny').to('name' => 'Fred')
245
+ end
81
246
  end
82
247
 
83
248
  describe '#apply_changes' do
84
- specify { expect { association.build; association.apply_changes }.to change { association.target.try(:persisted?) }.to(false) }
85
- specify { expect { association.build(name: 'Fred'); association.apply_changes }.to change { association.target.try(:persisted?) }.to(true) }
86
- specify { expect { existing_association.target.mark_for_destruction; existing_association.apply_changes }.to change { existing_association.target }.to(nil) }
87
- specify { expect { existing_association.target.destroy!; existing_association.apply_changes }.to change { existing_association.target }.to(nil) }
88
- specify { expect { existing_association.target.mark_for_destruction; existing_association.apply_changes }.to change { existing_association.destroyed.try(:name) }.from(nil).to('Johny') }
89
- specify { expect { existing_association.target.destroy!; existing_association.apply_changes }.to change { existing_association.destroyed.try(:name) }.from(nil).to('Johny') }
249
+ specify do
250
+ association.build
251
+ expect { association.apply_changes }
252
+ .not_to change { association.target.persisted? }.from(false)
253
+ end
254
+ specify do
255
+ association.build(name: 'Fred')
256
+ expect { association.apply_changes }
257
+ .to change { association.target.persisted? }.to(true)
258
+ end
259
+ specify do
260
+ existing_association.target.mark_for_destruction
261
+ expect { existing_association.apply_changes }
262
+ .to change { existing_association.target }.to(nil)
263
+ end
264
+ specify do
265
+ existing_association.target.destroy!
266
+ expect { existing_association.apply_changes }
267
+ .to change { existing_association.target }.to(nil)
268
+ end
269
+ specify do
270
+ existing_association.target.mark_for_destruction
271
+ expect { existing_association.apply_changes }
272
+ .to change { existing_association.destroyed.try(:name) }.from(nil).to('Johny')
273
+ end
274
+ specify do
275
+ existing_association.target.destroy!
276
+ expect { existing_association.apply_changes }
277
+ .to change { existing_association.destroyed.try(:name) }.from(nil).to('Johny')
278
+ end
90
279
  end
91
280
 
92
281
  describe '#apply_changes!' do
93
- specify { expect { association.build; association.apply_changes! }.to raise_error ActiveData::AssociationChangesNotApplied }
94
- specify { expect { association.build(name: 'Fred'); association.apply_changes! }.to change { association.target.try(:persisted?) }.to(true) }
95
- specify { expect { existing_association.target.mark_for_destruction; existing_association.apply_changes! }.to change { existing_association.target }.to(nil) }
96
- specify { expect { existing_association.target.destroy!; existing_association.apply_changes! }.to change { existing_association.target }.to(nil) }
282
+ specify do
283
+ association.build
284
+ expect { association.apply_changes! }
285
+ .to raise_error ActiveData::AssociationChangesNotApplied
286
+ end
287
+ specify do
288
+ association.build(name: 'Fred')
289
+ expect { association.apply_changes! }
290
+ .to change { association.target.persisted? }.to(true)
291
+ end
292
+ specify do
293
+ existing_association.target.mark_for_destruction
294
+ expect { existing_association.apply_changes! }
295
+ .to change { existing_association.target }.to(nil)
296
+ end
297
+ specify do
298
+ existing_association.target.destroy!
299
+ expect { existing_association.apply_changes! }
300
+ .to change { existing_association.target }.to(nil)
301
+ end
97
302
  end
98
303
 
99
304
  describe '#target' do
@@ -103,7 +308,7 @@ describe ActiveData::Model::Associations::EmbedsOne do
103
308
  end
104
309
 
105
310
  describe '#default' do
106
- before { Book.embeds_one :author, default: -> { { name: 'Default' } } }
311
+ before { Book.embeds_one :author, default: -> { {name: 'Default'} } }
107
312
  before do
108
313
  Author.class_eval do
109
314
  include ActiveData::Model::Primary
@@ -148,15 +353,19 @@ describe ActiveData::Model::Associations::EmbedsOne do
148
353
 
149
354
  context do
150
355
  before { association.build(name: 'Fred') }
151
- specify { expect { association.reload }
152
- .to change { association.reader.try(:attributes) }.from('name' => 'Fred').to(nil) }
356
+ specify do
357
+ expect { association.reload }
358
+ .to change { association.reader.try(:attributes) }.from('name' => 'Fred').to(nil)
359
+ end
153
360
  end
154
361
 
155
362
  context do
156
363
  before { existing_association.build(name: 'Fred') }
157
- specify { expect { existing_association.reload }
158
- .to change { existing_association.reader.try(:attributes) }
159
- .from('name' => 'Fred').to('name' => 'Johny') }
364
+ specify do
365
+ expect { existing_association.reload }
366
+ .to change { existing_association.reader.try(:attributes) }
367
+ .from('name' => 'Fred').to('name' => 'Johny')
368
+ end
160
369
  end
161
370
  end
162
371
 
@@ -165,10 +374,14 @@ describe ActiveData::Model::Associations::EmbedsOne do
165
374
  specify { expect { association.clear }.not_to change { association.reader } }
166
375
 
167
376
  specify { expect(existing_association.clear).to eq(true) }
168
- specify { expect { existing_association.clear }
169
- .to change { existing_association.reader.try(:attributes) }.from('name' => 'Johny').to(nil) }
170
- specify { expect { existing_association.clear }
171
- .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to(nil) }
377
+ specify do
378
+ expect { existing_association.clear }
379
+ .to change { existing_association.reader.try(:attributes) }.from('name' => 'Johny').to(nil)
380
+ end
381
+ specify do
382
+ expect { existing_association.clear }
383
+ .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to(nil)
384
+ end
172
385
 
173
386
  context do
174
387
  before { Author.send(:include, ActiveData::Model::Callbacks) }
@@ -178,10 +391,14 @@ describe ActiveData::Model::Associations::EmbedsOne do
178
391
  before { Author.before_destroy { false } }
179
392
  end
180
393
  specify { expect(existing_association.clear).to eq(false) }
181
- specify { expect { existing_association.clear }
182
- .not_to change { existing_association.reader } }
183
- specify { expect { existing_association.clear }
184
- .not_to change { existing_book.read_attribute(:author).symbolize_keys } }
394
+ specify do
395
+ expect { existing_association.clear }
396
+ .not_to change { existing_association.reader }
397
+ end
398
+ specify do
399
+ expect { existing_association.clear }
400
+ .not_to change { existing_book.read_attribute(:author).symbolize_keys }
401
+ end
185
402
  end
186
403
  end
187
404
 
@@ -216,61 +433,101 @@ describe ActiveData::Model::Associations::EmbedsOne do
216
433
  end
217
434
  end
218
435
 
219
- specify { expect { association.writer(nil) }
220
- .not_to change { book.read_attribute(:author) } }
221
- specify { expect { association.writer(new_author) }
222
- .to change { association.reader.try(:attributes) }.from(nil).to('name' => 'Morty') }
223
- specify { expect { association.writer(new_author) }
224
- .to change { book.read_attribute(:author) }.from(nil).to('name' => 'Morty') }
436
+ specify do
437
+ expect { association.writer(nil) }
438
+ .not_to change { book.read_attribute(:author) }
439
+ end
440
+ specify do
441
+ expect { association.writer(new_author) }
442
+ .to change { association.reader.try(:attributes) }.from(nil).to('name' => 'Morty')
443
+ end
444
+ specify do
445
+ expect { association.writer(new_author) }
446
+ .to change { book.read_attribute(:author) }.from(nil).to('name' => 'Morty')
447
+ end
225
448
 
226
- specify { expect { association.writer(invalid_author) }
227
- .to raise_error ActiveData::AssociationChangesNotApplied }
228
- specify { expect { association.writer(invalid_author) rescue nil }
229
- .not_to change { association.reader } }
230
- specify { expect { association.writer(invalid_author) rescue nil }
231
- .not_to change { book.read_attribute(:author) } }
449
+ specify do
450
+ expect { association.writer(invalid_author) }
451
+ .to raise_error ActiveData::AssociationChangesNotApplied
452
+ end
453
+ specify do
454
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { association.writer(invalid_author) } }
455
+ .not_to change { association.reader }
456
+ end
457
+ specify do
458
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { association.writer(invalid_author) } }
459
+ .not_to change { book.read_attribute(:author) }
460
+ end
232
461
  end
233
462
 
234
463
  context 'persisted owner' do
235
- specify { expect { association.writer(stub_model(:dummy).new) }
236
- .to raise_error ActiveData::AssociationTypeMismatch }
464
+ specify do
465
+ expect { association.writer(stub_model(:dummy).new) }
466
+ .to raise_error ActiveData::AssociationTypeMismatch
467
+ end
237
468
 
238
469
  specify { expect(association.writer(nil)).to be_nil }
239
470
  specify { expect(association.writer(new_author)).to eq(new_author) }
240
- specify { expect { association.writer(nil) }
241
- .not_to change { book.read_attribute(:author) } }
242
- specify { expect { association.writer(new_author) }
243
- .to change { association.reader.try(:attributes) }.from(nil).to('name' => 'Morty') }
244
- specify { expect { association.writer(new_author) }
245
- .not_to change { book.read_attribute(:author) } }
246
-
247
- specify { expect { association.writer(invalid_author) }
248
- .to change { association.reader.try(:attributes) }.from(nil).to('name' => nil) }
249
- specify { expect { association.writer(invalid_author) }
250
- .not_to change { book.read_attribute(:author) } }
251
-
252
- specify { expect { existing_association.writer(stub_model(:dummy).new) rescue nil }
253
- .not_to change { existing_book.read_attribute(:author) } }
254
- specify { expect { existing_association.writer(stub_model(:dummy).new) rescue nil }
255
- .not_to change { existing_association.reader } }
471
+ specify do
472
+ expect { association.writer(nil) }
473
+ .not_to change { book.read_attribute(:author) }
474
+ end
475
+ specify do
476
+ expect { association.writer(new_author) }
477
+ .to change { association.reader.try(:attributes) }.from(nil).to('name' => 'Morty')
478
+ end
479
+ specify do
480
+ expect { association.writer(new_author) }
481
+ .not_to change { book.read_attribute(:author) }
482
+ end
483
+
484
+ specify do
485
+ expect { association.writer(invalid_author) }
486
+ .to change { association.reader.try(:attributes) }.from(nil).to('name' => nil)
487
+ end
488
+ specify do
489
+ expect { association.writer(invalid_author) }
490
+ .not_to change { book.read_attribute(:author) }
491
+ end
492
+
493
+ specify do
494
+ expect { muffle(ActiveData::AssociationTypeMismatch) { existing_association.writer(stub_model(:dummy).new) } }
495
+ .not_to change { existing_book.read_attribute(:author) }
496
+ end
497
+ specify do
498
+ expect { muffle(ActiveData::AssociationTypeMismatch) { existing_association.writer(stub_model(:dummy).new) } }
499
+ .not_to change { existing_association.reader }
500
+ end
256
501
 
257
502
  specify { expect(existing_association.writer(nil)).to be_nil }
258
503
  specify { expect(existing_association.writer(new_author)).to eq(new_author) }
259
- specify { expect { existing_association.writer(nil) }
260
- .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to(nil) }
261
- specify { expect { existing_association.writer(new_author) }
262
- .to change { existing_association.reader.try(:attributes) }
263
- .from('name' => 'Johny').to('name' => 'Morty') }
264
- specify { expect { existing_association.writer(new_author) }
265
- .to change { existing_book.read_attribute(:author) }
266
- .from('name' => 'Johny').to('name' => 'Morty') }
267
-
268
- specify { expect { existing_association.writer(invalid_author) }
269
- .to raise_error ActiveData::AssociationChangesNotApplied }
270
- specify { expect { existing_association.writer(invalid_author) rescue nil }
271
- .not_to change { existing_association.reader } }
272
- specify { expect { existing_association.writer(invalid_author) rescue nil }
273
- .not_to change { existing_book.read_attribute(:author) } }
504
+ specify do
505
+ expect { existing_association.writer(nil) }
506
+ .to change { existing_book.read_attribute(:author) }.from('name' => 'Johny').to(nil)
507
+ end
508
+ specify do
509
+ expect { existing_association.writer(new_author) }
510
+ .to change { existing_association.reader.try(:attributes) }
511
+ .from('name' => 'Johny').to('name' => 'Morty')
512
+ end
513
+ specify do
514
+ expect { existing_association.writer(new_author) }
515
+ .to change { existing_book.read_attribute(:author) }
516
+ .from('name' => 'Johny').to('name' => 'Morty')
517
+ end
518
+
519
+ specify do
520
+ expect { existing_association.writer(invalid_author) }
521
+ .to raise_error ActiveData::AssociationChangesNotApplied
522
+ end
523
+ specify do
524
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { existing_association.writer(invalid_author) } }
525
+ .not_to change { existing_association.reader }
526
+ end
527
+ specify do
528
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { existing_association.writer(invalid_author) } }
529
+ .not_to change { existing_book.read_attribute(:author) }
530
+ end
274
531
  end
275
532
  end
276
533
  end