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.
- checksums.yaml +5 -5
- data/.codeclimate.yml +13 -0
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +53 -0
- data/.rvmrc +1 -1
- data/.travis.yml +15 -2
- data/Appraisals +1 -1
- data/CHANGELOG.md +31 -0
- data/Guardfile +8 -8
- data/README.md +256 -0
- data/Rakefile +2 -4
- data/active_data.gemspec +8 -7
- data/gemfiles/rails.4.0.gemfile +1 -1
- data/gemfiles/rails.4.1.gemfile +1 -1
- data/gemfiles/rails.4.2.gemfile +1 -1
- data/gemfiles/rails.5.0.gemfile +1 -1
- data/gemfiles/rails.5.1.gemfile +14 -0
- data/lib/active_data/active_record/associations.rb +18 -13
- data/lib/active_data/active_record/nested_attributes.rb +8 -14
- data/lib/active_data/base.rb +13 -0
- data/lib/active_data/config.rb +4 -4
- data/lib/active_data/errors.rb +29 -13
- data/lib/active_data/extensions.rb +22 -21
- data/lib/active_data/model/associations/base.rb +22 -6
- data/lib/active_data/model/associations/embeds_any.rb +17 -0
- data/lib/active_data/model/associations/embeds_many.rb +29 -19
- data/lib/active_data/model/associations/embeds_one.rb +30 -26
- data/lib/active_data/model/associations/nested_attributes.rb +82 -50
- data/lib/active_data/model/associations/persistence_adapters/active_record/referenced_proxy.rb +31 -0
- data/lib/active_data/model/associations/persistence_adapters/active_record.rb +66 -0
- data/lib/active_data/model/associations/persistence_adapters/base.rb +53 -0
- data/lib/active_data/model/associations/references_any.rb +41 -0
- data/lib/active_data/model/associations/references_many.rb +51 -37
- data/lib/active_data/model/associations/references_one.rb +43 -41
- data/lib/active_data/model/associations/reflections/base.rb +19 -29
- data/lib/active_data/model/associations/reflections/embeds_any.rb +43 -0
- data/lib/active_data/model/associations/reflections/embeds_many.rb +3 -13
- data/lib/active_data/model/associations/reflections/embeds_one.rb +5 -37
- data/lib/active_data/model/associations/reflections/references_any.rb +62 -0
- data/lib/active_data/model/associations/reflections/references_many.rb +7 -7
- data/lib/active_data/model/associations/reflections/references_one.rb +9 -7
- data/lib/active_data/model/associations/reflections/singular.rb +35 -0
- data/lib/active_data/model/associations/validations.rb +2 -27
- data/lib/active_data/model/associations.rb +12 -10
- data/lib/active_data/model/attributes/attribute.rb +10 -10
- data/lib/active_data/model/attributes/base.rb +8 -7
- data/lib/active_data/model/attributes/localized.rb +4 -4
- data/lib/active_data/model/attributes/reference_many.rb +6 -8
- data/lib/active_data/model/attributes/reference_one.rb +17 -9
- data/lib/active_data/model/attributes/reflections/attribute.rb +2 -2
- data/lib/active_data/model/attributes/reflections/base.rb +8 -11
- data/lib/active_data/model/attributes/reflections/localized.rb +2 -2
- data/lib/active_data/model/attributes/reflections/reference_one.rb +11 -22
- data/lib/active_data/model/attributes/reflections/represents.rb +5 -6
- data/lib/active_data/model/attributes/represents.rb +6 -5
- data/lib/active_data/model/attributes.rb +33 -87
- data/lib/active_data/model/callbacks.rb +6 -7
- data/lib/active_data/model/conventions.rb +2 -0
- data/lib/active_data/model/dirty.rb +4 -4
- data/lib/active_data/model/lifecycle.rb +18 -20
- data/lib/active_data/model/localization.rb +5 -2
- data/lib/active_data/model/persistence.rb +2 -2
- data/lib/active_data/model/primary.rb +19 -14
- data/lib/active_data/model/representation.rb +81 -0
- data/lib/active_data/model/scopes.rb +22 -12
- data/lib/active_data/model/validations/associated.rb +3 -2
- data/lib/active_data/model/validations/nested.rb +6 -1
- data/lib/active_data/model/validations.rb +3 -3
- data/lib/active_data/model.rb +2 -1
- data/lib/active_data/undefined_class.rb +9 -0
- data/lib/active_data/version.rb +1 -1
- data/lib/active_data.rb +40 -17
- data/spec/lib/active_data/active_record/associations_spec.rb +107 -45
- data/spec/lib/active_data/active_record/nested_attributes_spec.rb +1 -2
- data/spec/lib/active_data/config_spec.rb +37 -15
- data/spec/lib/active_data/model/associations/embeds_many_spec.rb +475 -172
- data/spec/lib/active_data/model/associations/embeds_one_spec.rb +353 -96
- data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +108 -12
- data/spec/lib/active_data/model/associations/persistence_adapters/active_record_spec.rb +58 -0
- data/spec/lib/active_data/model/associations/references_many_spec.rb +440 -64
- data/spec/lib/active_data/model/associations/references_one_spec.rb +347 -36
- data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +8 -7
- data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +7 -6
- data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +81 -33
- data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +116 -37
- data/spec/lib/active_data/model/associations/validations_spec.rb +27 -43
- data/spec/lib/active_data/model/associations_spec.rb +34 -25
- data/spec/lib/active_data/model/attributes/attribute_spec.rb +26 -23
- data/spec/lib/active_data/model/attributes/base_spec.rb +5 -6
- data/spec/lib/active_data/model/attributes/collection_spec.rb +7 -8
- data/spec/lib/active_data/model/attributes/dictionary_spec.rb +40 -33
- data/spec/lib/active_data/model/attributes/localized_spec.rb +27 -28
- data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +6 -6
- data/spec/lib/active_data/model/attributes/represents_spec.rb +10 -78
- data/spec/lib/active_data/model/attributes_spec.rb +150 -45
- data/spec/lib/active_data/model/callbacks_spec.rb +69 -70
- data/spec/lib/active_data/model/conventions_spec.rb +0 -1
- data/spec/lib/active_data/model/dirty_spec.rb +22 -13
- data/spec/lib/active_data/model/lifecycle_spec.rb +49 -23
- data/spec/lib/active_data/model/persistence_spec.rb +5 -6
- data/spec/lib/active_data/model/representation_spec.rb +126 -0
- data/spec/lib/active_data/model/scopes_spec.rb +1 -3
- data/spec/lib/active_data/model/typecasting_spec.rb +6 -5
- data/spec/lib/active_data/model/validations/associated_spec.rb +26 -18
- data/spec/lib/active_data/model/validations/nested_spec.rb +89 -18
- data/spec/lib/active_data/model_spec.rb +1 -2
- data/spec/lib/active_data_spec.rb +0 -1
- data/spec/shared/nested_attribute_examples.rb +332 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/support/model_helpers.rb +2 -2
- data/spec/support/muffle_helper.rb +7 -0
- metadata +52 -18
- data/lib/active_data/model/associations/collection/referenced.rb +0 -26
- data/lib/active_data/model/associations/reflections/reference_reflection.rb +0 -45
- 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
|
|
38
|
-
|
|
167
|
+
specify do
|
|
168
|
+
expect { association.build(name: 'Fred') }
|
|
169
|
+
.not_to change { book.read_attribute(:author) }
|
|
170
|
+
end
|
|
39
171
|
|
|
40
|
-
specify
|
|
41
|
-
|
|
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
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
specify
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
specify
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
specify
|
|
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
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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: -> { {
|
|
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
|
|
152
|
-
|
|
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
|
|
158
|
-
|
|
159
|
-
|
|
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
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
specify
|
|
224
|
-
|
|
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
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
specify
|
|
231
|
-
|
|
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
|
|
236
|
-
|
|
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
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
specify
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
specify
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|