active_data 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,9 +1,10 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Associations::ReferencesOne do
5
4
  before do
6
- stub_class(:author, ActiveRecord::Base) { }
5
+ stub_class(:author, ActiveRecord::Base) do
6
+ validates :name, presence: true
7
+ end
7
8
 
8
9
  stub_model(:book) do
9
10
  include ActiveData::Model::Persistence
@@ -27,6 +28,284 @@ describe ActiveData::Model::Associations::ReferencesOne do
27
28
  specify { expect(association).to eq(book.association(:author)) }
28
29
  end
29
30
 
31
+ describe 'book#inspect' do
32
+ specify { expect(existing_book.inspect).to eq('#<Book author: #<ReferencesOne #<Author id: 1, name: "Johny">>, title: "My Life", author_id: 1>') }
33
+ end
34
+
35
+ describe '#build' do
36
+ specify { expect(association.build).to be_a Author }
37
+ specify { expect(association.build).not_to be_persisted }
38
+
39
+ specify do
40
+ expect { association.build(name: 'Morty') }
41
+ .not_to change { book.author_id }
42
+ end
43
+ specify do
44
+ expect { association.build(name: 'Morty') }
45
+ .to change { book.author }.from(nil)
46
+ .to(an_instance_of(Author).and(have_attributes(name: 'Morty')))
47
+ end
48
+
49
+ specify do
50
+ expect { existing_association.build(name: 'Morty') }
51
+ .to change { existing_book.author_id }
52
+ .from(author.id).to(nil)
53
+ end
54
+ specify do
55
+ expect { existing_association.build(name: 'Morty') }
56
+ .to change { existing_book.author }.from(author)
57
+ .to(an_instance_of(Author).and(have_attributes(name: 'Morty')))
58
+ end
59
+
60
+ context 'dirty' do
61
+ before do
62
+ Book.include ActiveData::Model::Dirty
63
+ end
64
+
65
+ specify do
66
+ expect { existing_association.build(name: 'Morty') }
67
+ .to change { existing_book.changes }
68
+ .from({}).to('author_id' => [author.id, nil])
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#create' do
74
+ specify { expect(association.create).to be_a Author }
75
+ specify { expect(association.create).not_to be_persisted }
76
+
77
+ specify { expect(association.create(name: 'Fred')).to be_a Author }
78
+ specify { expect(association.create(name: 'Fred')).to be_persisted }
79
+
80
+ specify do
81
+ expect { association.create }
82
+ .not_to change { book.author_id }
83
+ end
84
+ specify do
85
+ expect { association.create(name: 'Fred') }
86
+ .to change { book.author_id }
87
+ .from(nil).to(be_a(Integer))
88
+ end
89
+
90
+ specify do
91
+ expect { existing_association.create }
92
+ .to change { existing_book.author_id }
93
+ .from(author.id).to(nil)
94
+ end
95
+ specify do
96
+ expect { existing_association.create(name: 'Fred') }
97
+ .to change { existing_book.author_id }
98
+ .from(author.id).to(be_a(Integer))
99
+ end
100
+
101
+ context 'dirty' do
102
+ before do
103
+ Book.include ActiveData::Model::Dirty
104
+ end
105
+
106
+ specify do
107
+ expect { existing_association.create(name: 'Fred') }
108
+ .to change { existing_book.changes }
109
+ .from({}).to('author_id' => [author.id, be_a(Integer)])
110
+ end
111
+ end
112
+ end
113
+
114
+ describe '#create!' do
115
+ specify { expect { association.create! }.to raise_error ActiveRecord::RecordInvalid }
116
+ specify do
117
+ expect { muffle(ActiveRecord::RecordInvalid) { association.create! } }
118
+ .to change { association.target }
119
+ .from(nil).to(an_instance_of(Author))
120
+ end
121
+
122
+ specify { expect(association.create!(name: 'Fred')).to be_a Author }
123
+ specify { expect(association.create!(name: 'Fred')).to be_persisted }
124
+
125
+ specify do
126
+ expect { muffle(ActiveRecord::RecordInvalid) { association.create! } }
127
+ .not_to change { book.author_id }
128
+ end
129
+ specify do
130
+ expect { muffle(ActiveRecord::RecordInvalid) { association.create! } }
131
+ .to change { association.reader.try(:attributes).try(:slice, 'name') }
132
+ .from(nil).to('name' => nil)
133
+ end
134
+ specify do
135
+ expect { association.create(name: 'Fred') }
136
+ .to change { book.author_id }
137
+ .from(nil).to(be_a(Integer))
138
+ end
139
+
140
+ specify do
141
+ expect { muffle(ActiveRecord::RecordInvalid) { existing_association.create! } }
142
+ .to change { existing_book.author_id }
143
+ .from(author.id).to(nil)
144
+ end
145
+ specify do
146
+ expect { muffle(ActiveRecord::RecordInvalid) { existing_association.create! } }
147
+ .to change { existing_association.reader.try(:attributes).try(:slice, 'name') }
148
+ .from('name' => 'Johny').to('name' => nil)
149
+ end
150
+ specify do
151
+ expect { existing_association.create!(name: 'Fred') }
152
+ .to change { existing_book.author_id }
153
+ .from(author.id).to(be_a(Integer))
154
+ end
155
+ end
156
+
157
+ context do
158
+ shared_examples 'apply_changes' do |method|
159
+ specify do
160
+ association.build(name: 'Fred')
161
+ expect(association.send(method)).to eq(true)
162
+ end
163
+ specify do
164
+ association.build(name: 'Fred')
165
+ expect { association.send(method) }
166
+ .to change { association.target.persisted? }.to(true)
167
+ end
168
+ specify do
169
+ association.build(name: 'Fred')
170
+ expect { association.send(method) }
171
+ .to change { book.author_id }
172
+ .from(nil).to(be_a(Integer))
173
+ end
174
+ specify do
175
+ existing_association.target.name = 'Fred'
176
+ expect { existing_association.send(method) }
177
+ .not_to change { author.reload.name }
178
+ end
179
+ specify do
180
+ existing_association.target.mark_for_destruction
181
+ expect { existing_association.send(method) }
182
+ .not_to change { existing_association.target.destroyed? }
183
+ end
184
+ specify do
185
+ existing_association.target.mark_for_destruction
186
+ expect { existing_association.send(method) }
187
+ .not_to change { existing_book.author_id }
188
+ end
189
+ specify do
190
+ existing_association.target.destroy!
191
+ expect { existing_association.send(method) }
192
+ .not_to change { existing_association.target.destroyed? }
193
+ end
194
+ specify do
195
+ existing_association.target.destroy!
196
+ expect { existing_association.send(method) }
197
+ .not_to change { existing_book.author_id }
198
+ end
199
+
200
+ context ':autosave' do
201
+ before do
202
+ Book.references_one :author, autosave: true
203
+ end
204
+
205
+ specify do
206
+ association.build(name: 'Fred')
207
+ expect(association.send(method)).to eq(true)
208
+ end
209
+ specify do
210
+ association.build(name: 'Fred')
211
+ expect { association.send(method) }
212
+ .to change { association.target.persisted? }.to(true)
213
+ end
214
+ specify do
215
+ existing_association.target.name = 'Fred'
216
+ expect { existing_association.send(method) }
217
+ .to change { author.reload.name }.from('Johny').to('Fred')
218
+ end
219
+ specify do
220
+ existing_association.target.mark_for_destruction
221
+ expect { existing_association.send(method) }
222
+ .to change { existing_association.target.destroyed? }
223
+ .from(false).to(true)
224
+ end
225
+ specify do
226
+ existing_association.target.mark_for_destruction
227
+ expect { existing_association.send(method) }
228
+ .not_to change { existing_book.author_id }
229
+ .from(author.id)
230
+ end
231
+ specify do
232
+ existing_association.target.destroy!
233
+ expect { existing_association.send(method) }
234
+ .not_to change { existing_association.target.destroyed? }
235
+ .from(true)
236
+ end
237
+ specify do
238
+ existing_association.target.destroy!
239
+ expect { existing_association.send(method) }
240
+ .not_to change { existing_book.author_id }
241
+ .from(author.id)
242
+ end
243
+ end
244
+ end
245
+
246
+ describe '#apply_changes' do
247
+ include_examples 'apply_changes', :apply_changes
248
+
249
+ specify do
250
+ association.build
251
+ expect(association.apply_changes).to eq(false)
252
+ end
253
+ specify do
254
+ association.build
255
+ expect { association.apply_changes }
256
+ .not_to change { association.target.persisted? }.from(false)
257
+ end
258
+
259
+ context ':autosave' do
260
+ before do
261
+ Book.references_one :author, autosave: true
262
+ end
263
+
264
+ specify do
265
+ association.build
266
+ expect(association.apply_changes).to eq(false)
267
+ end
268
+ specify do
269
+ association.build
270
+ expect { association.apply_changes }
271
+ .not_to change { association.target.persisted? }.from(false)
272
+ end
273
+ end
274
+ end
275
+
276
+ describe '#apply_changes!' do
277
+ include_examples 'apply_changes', :apply_changes!
278
+
279
+ specify do
280
+ association.build
281
+ expect { association.apply_changes! }
282
+ .to raise_error(ActiveData::AssociationChangesNotApplied)
283
+ end
284
+ specify do
285
+ association.build
286
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { association.apply_changes! } }
287
+ .not_to change { association.target.persisted? }.from(false)
288
+ end
289
+
290
+ context ':autosave' do
291
+ before do
292
+ Book.references_one :author, autosave: true
293
+ end
294
+
295
+ specify do
296
+ association.build
297
+ expect { association.apply_changes! }
298
+ .to raise_error(ActiveData::AssociationChangesNotApplied)
299
+ end
300
+ specify do
301
+ association.build
302
+ expect { muffle(ActiveData::AssociationChangesNotApplied) { association.apply_changes! } }
303
+ .not_to change { association.target.persisted? }.from(false)
304
+ end
305
+ end
306
+ end
307
+ end
308
+
30
309
  describe '#target' do
31
310
  specify { expect(association.target).to be_nil }
32
311
  specify { expect(existing_association.target).to eq(existing_book.author) }
@@ -50,10 +329,12 @@ describe ActiveData::Model::Associations::ReferencesOne do
50
329
  specify { expect(existing_association.reload).to be_persisted }
51
330
 
52
331
  context do
53
- before { existing_association.reader.name = "New" }
54
- specify { expect { existing_association.reload }
55
- .to change { existing_association.reader.name }
56
- .from('New').to('Johny') }
332
+ before { existing_association.reader.name = 'New' }
333
+ specify do
334
+ expect { existing_association.reload }
335
+ .to change { existing_association.reader.name }
336
+ .from('New').to('Johny')
337
+ end
57
338
  end
58
339
  end
59
340
 
@@ -65,7 +346,7 @@ describe ActiveData::Model::Associations::ReferencesOne do
65
346
  end
66
347
 
67
348
  describe '#default' do
68
- before { Book.references_one :author, default: ->(book) { author.id } }
349
+ before { Book.references_one :author, default: ->(_book) { author.id } }
69
350
  let(:existing_book) { Book.instantiate title: 'My Life' }
70
351
 
71
352
  specify { expect(association.target).to eq(author) }
@@ -77,7 +358,6 @@ describe ActiveData::Model::Associations::ReferencesOne do
77
358
  specify { expect { existing_association.replace(nil) }.not_to change { existing_association.target } }
78
359
  end
79
360
 
80
-
81
361
  describe '#writer' do
82
362
  context 'new owner' do
83
363
  let(:new_author) { Author.new(name: 'Morty') }
@@ -88,47 +368,78 @@ describe ActiveData::Model::Associations::ReferencesOne do
88
368
  end
89
369
  end
90
370
 
91
- specify { expect { association.writer(nil) }
92
- .not_to change { book.author_id } }
93
- specify { expect { association.writer(new_author) }
94
- .to change { association.reader.name rescue nil }.from(nil).to('Morty') }
95
- specify { expect { association.writer(new_author) }
96
- .not_to change { book.author_id }.from(nil) }
97
-
371
+ specify do
372
+ expect { association.writer(nil) }
373
+ .not_to change { book.author_id }
374
+ end
375
+ specify do
376
+ expect { association.writer(new_author) }
377
+ .to change { muffle(NoMethodError) { association.reader.name } }
378
+ .from(nil).to('Morty')
379
+ end
380
+ specify do
381
+ expect { association.writer(new_author) }
382
+ .not_to change { book.author_id }.from(nil)
383
+ end
98
384
  end
99
385
 
100
386
  context 'persisted owner' do
101
387
  let(:new_author) { Author.create(name: 'Morty') }
102
388
 
103
- specify { expect { association.writer(stub_model(:dummy).new) }
104
- .to raise_error ActiveData::AssociationTypeMismatch }
389
+ specify do
390
+ expect { association.writer(stub_model(:dummy).new) }
391
+ .to raise_error ActiveData::AssociationTypeMismatch
392
+ end
105
393
 
106
394
  specify { expect(association.writer(nil)).to be_nil }
107
395
  specify { expect(association.writer(new_author)).to eq(new_author) }
108
- specify { expect { association.writer(nil) }
109
- .not_to change { book.read_attribute(:author_id) } }
110
- specify { expect { association.writer(new_author) }
111
- .to change { association.reader.try(:attributes) }.from(nil).to('id' => 1, 'name' => 'Morty') }
112
- specify { expect { association.writer(new_author) }
113
- .to change { book.read_attribute(:author_id) } }
396
+ specify do
397
+ expect { association.writer(nil) }
398
+ .not_to change { book.read_attribute(:author_id) }
399
+ end
400
+ specify do
401
+ expect { association.writer(new_author) }
402
+ .to change { association.reader.try(:attributes) }.from(nil).to('id' => 1, 'name' => 'Morty')
403
+ end
404
+ specify do
405
+ expect { association.writer(new_author) }
406
+ .to change { book.read_attribute(:author_id) }
407
+ end
114
408
 
409
+ context do
410
+ before do
411
+ stub_class(:dummy, ActiveRecord::Base) do
412
+ self.table_name = :authors
413
+ end
414
+ end
115
415
 
116
- specify { expect { existing_association.writer(stub_class(:dummy, ActiveRecord::Base).new) rescue nil }
117
- .not_to change { existing_book.read_attribute(:author_id) } }
118
- specify { expect { existing_association.writer(stub_class(:dummy, ActiveRecord::Base).new) rescue nil }
119
- .not_to change { existing_association.reader } }
416
+ specify do
417
+ expect { muffle(ActiveData::AssociationTypeMismatch) { existing_association.writer(Dummy.new) } }
418
+ .not_to change { existing_book.read_attribute(:author_id) }
419
+ end
420
+ specify do
421
+ expect { muffle(ActiveData::AssociationTypeMismatch) { existing_association.writer(Dummy.new) } }
422
+ .not_to change { existing_association.reader }
423
+ end
424
+ end
120
425
 
121
426
  specify { expect(existing_association.writer(nil)).to be_nil }
122
427
  specify { expect(existing_association.writer(new_author)).to eq(new_author) }
123
- specify { expect { existing_association.writer(nil) }
124
- .to change { existing_book.read_attribute(:author_id) }.from(author.id).to(nil) }
125
- specify { expect { existing_association.writer(new_author) }
126
- .to change { existing_association.reader.try(:attributes) }
127
- .from('id' => 1, 'name' => 'Johny').to('id' => 2, 'name' => 'Morty') }
128
- specify { expect { existing_association.writer(new_author) }
129
- .to change { existing_book.read_attribute(:author_id) }
130
- .from(author.id).to(new_author.id) }
131
-
428
+ specify do
429
+ expect { existing_association.writer(nil) }
430
+ .to change { existing_book.read_attribute(:author_id) }
431
+ .from(author.id).to(nil)
432
+ end
433
+ specify do
434
+ expect { existing_association.writer(new_author) }
435
+ .to change { existing_association.reader.try(:attributes) }
436
+ .from('id' => 1, 'name' => 'Johny').to('id' => 2, 'name' => 'Morty')
437
+ end
438
+ specify do
439
+ expect { existing_association.writer(new_author) }
440
+ .to change { existing_book.read_attribute(:author_id) }
441
+ .from(author.id).to(new_author.id)
442
+ end
132
443
  end
133
444
  end
134
445
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Associations::Reflections::EmbedsMany do
@@ -24,11 +23,11 @@ describe ActiveData::Model::Associations::Reflections::EmbedsMany do
24
23
 
25
24
  attribute :name
26
25
  embeds_many :projects,
27
- read: ->(reflection, object) {
26
+ read: lambda { |reflection, object|
28
27
  value = object.read_attribute(reflection.name)
29
28
  JSON.parse(value) if value.present?
30
29
  },
31
- write: ->(reflection, object, value) {
30
+ write: lambda { |reflection, object, value|
32
31
  object.write_attribute(reflection.name, value.to_json)
33
32
  }
34
33
  end
@@ -38,10 +37,12 @@ describe ActiveData::Model::Associations::Reflections::EmbedsMany do
38
37
  let(:new_project1) { Project.new(title: 'Project 1') }
39
38
  let(:new_project2) { Project.new(title: 'Project 2') }
40
39
 
41
- specify { expect { user.projects.concat([new_project1, new_project2]) }
42
- .to change { user.read_attribute(:projects) }
43
- .from([{title: 'Genesis'}].to_json)
44
- .to([{title: 'Genesis'}, {title: 'Project 1'}, {title: 'Project 2'}].to_json) }
40
+ specify do
41
+ expect { user.projects.concat([new_project1, new_project2]) }
42
+ .to change { user.read_attribute(:projects) }
43
+ .from([{title: 'Genesis'}].to_json)
44
+ .to([{title: 'Genesis'}, {title: 'Project 1'}, {title: 'Project 2'}].to_json)
45
+ end
45
46
  end
46
47
 
47
48
  describe '#projects' do
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Associations::Reflections::EmbedsOne do
@@ -27,11 +26,11 @@ describe ActiveData::Model::Associations::Reflections::EmbedsOne do
27
26
 
28
27
  attribute :title
29
28
  embeds_one :author,
30
- read: ->(reflection, object) {
29
+ read: lambda { |reflection, object|
31
30
  value = object.read_attribute(reflection.name)
32
31
  JSON.parse(value) if value.present?
33
32
  },
34
- write: ->(reflection, object, value) {
33
+ write: lambda { |reflection, object, value|
35
34
  object.write_attribute(reflection.name, value ? value.to_json : nil)
36
35
  }
37
36
  end
@@ -40,9 +39,11 @@ describe ActiveData::Model::Associations::Reflections::EmbedsOne do
40
39
  let(:book) { Book.instantiate author: {name: 'Duke'}.to_json }
41
40
  let(:author) { Author.new(name: 'Rick') }
42
41
 
43
- specify { expect { book.author = author }
44
- .to change { book.read_attribute(:author) }
45
- .from({name: 'Duke'}.to_json).to({name: 'Rick'}.to_json) }
42
+ specify do
43
+ expect { book.author = author }
44
+ .to change { book.read_attribute(:author) }
45
+ .from({name: 'Duke'}.to_json).to({name: 'Rick'}.to_json)
46
+ end
46
47
  end
47
48
 
48
49
  describe '#author=' do