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.
- 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::Attributes do
|
@@ -34,24 +33,26 @@ describe ActiveData::Model::Attributes do
|
|
34
33
|
end
|
35
34
|
|
36
35
|
describe '.attribute_names' do
|
37
|
-
specify { expect(stub_model.attribute_names).to eq([])
|
36
|
+
specify { expect(stub_model.attribute_names).to eq([]) }
|
38
37
|
specify { expect(model.attribute_names).to eq(%w[id full_name t author projects]) }
|
39
|
-
specify { expect(model.attribute_names(false)).to eq(%w[id full_name t])
|
38
|
+
specify { expect(model.attribute_names(false)).to eq(%w[id full_name t]) }
|
40
39
|
end
|
41
40
|
|
42
41
|
describe '.inspect' do
|
43
42
|
specify { expect(stub_model.inspect).to match(/#<Class:0x\w+>\(no attributes\)/) }
|
44
43
|
specify { expect(stub_model(:user).inspect).to eq('User(no attributes)') }
|
45
|
-
specify
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
44
|
+
specify do
|
45
|
+
expect(stub_model do
|
46
|
+
include ActiveData::Model::Primary
|
47
|
+
primary :count, Integer
|
48
|
+
attribute :object, Object
|
49
|
+
end.inspect).to match(/#<Class:0x\w+>\(\*count: Integer, object: Object\)/) end
|
50
|
+
specify do
|
51
|
+
expect(stub_model(:user) do
|
52
|
+
include ActiveData::Model::Primary
|
53
|
+
primary :count, Integer
|
54
|
+
attribute :object, Object
|
55
|
+
end.inspect).to match('User(*count: Integer, object: Object)') end
|
55
56
|
end
|
56
57
|
|
57
58
|
describe '#==' do
|
@@ -98,9 +99,9 @@ describe ActiveData::Model::Attributes do
|
|
98
99
|
end
|
99
100
|
|
100
101
|
describe '#attribute_names' do
|
101
|
-
specify { expect(stub_model.new.attribute_names).to eq([])
|
102
|
+
specify { expect(stub_model.new.attribute_names).to eq([]) }
|
102
103
|
specify { expect(model.new.attribute_names).to eq(%w[id full_name t author projects]) }
|
103
|
-
specify { expect(model.new.attribute_names(false)).to eq(%w[id full_name t])
|
104
|
+
specify { expect(model.new.attribute_names(false)).to eq(%w[id full_name t]) }
|
104
105
|
end
|
105
106
|
|
106
107
|
describe '#attribute_present?' do
|
@@ -110,34 +111,108 @@ describe ActiveData::Model::Attributes do
|
|
110
111
|
end
|
111
112
|
|
112
113
|
describe '#attributes' do
|
113
|
-
specify { expect(stub_model.new.attributes).to eq({})
|
114
|
-
specify
|
115
|
-
.
|
116
|
-
|
117
|
-
|
114
|
+
specify { expect(stub_model.new.attributes).to eq({}) }
|
115
|
+
specify do
|
116
|
+
expect(model.new(name: 'Name').attributes)
|
117
|
+
.to match('id' => nil, 'full_name' => 'Name', 't' => {}, 'author' => nil, 'projects' => nil)
|
118
|
+
end
|
119
|
+
specify do
|
120
|
+
expect(model.new(name: 'Name').attributes(false))
|
121
|
+
.to match('id' => nil, 'full_name' => 'Name', 't' => {})
|
122
|
+
end
|
118
123
|
end
|
119
124
|
|
120
125
|
describe '#assign_attributes' do
|
121
|
-
let(:attributes) { {
|
126
|
+
let(:attributes) { {id: 42, full_name: 'Name', missed: 'value'} }
|
122
127
|
subject { model.new }
|
123
128
|
|
124
129
|
specify { expect { subject.assign_attributes(attributes) }.to change { subject.id }.to(42) }
|
125
130
|
specify { expect { subject.assign_attributes(attributes) }.to change { subject.full_name }.to('Name') }
|
131
|
+
|
132
|
+
context 'features stack and assign order' do
|
133
|
+
let(:model) do
|
134
|
+
stub_model do
|
135
|
+
attr_reader :logger
|
136
|
+
|
137
|
+
def self.log(a)
|
138
|
+
define_method("#{a}=") do |*args|
|
139
|
+
log(a)
|
140
|
+
super(*args)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def log(o)
|
145
|
+
(@logger ||= []).push(o)
|
146
|
+
end
|
147
|
+
|
148
|
+
attribute :plain1, String
|
149
|
+
attribute :plain2, String
|
150
|
+
log(:plain1)
|
151
|
+
log(:plain2)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
subject { model.new }
|
155
|
+
|
156
|
+
specify do
|
157
|
+
expect { subject.assign_attributes(plain1: 'value', plain2: 'value') }
|
158
|
+
.to change { subject.logger }.to(%i[plain1 plain2])
|
159
|
+
end
|
160
|
+
|
161
|
+
specify do
|
162
|
+
expect { subject.assign_attributes(plain2: 'value', plain1: 'value') }
|
163
|
+
.to change { subject.logger }.to(%i[plain2 plain1])
|
164
|
+
end
|
165
|
+
|
166
|
+
context do
|
167
|
+
before do
|
168
|
+
model.class_eval do
|
169
|
+
include ActiveData::Model::Representation
|
170
|
+
include ActiveData::Model::Associations
|
171
|
+
|
172
|
+
embeds_one :assoc do
|
173
|
+
attribute :assoc_plain, String
|
174
|
+
end
|
175
|
+
accepts_nested_attributes_for :assoc
|
176
|
+
|
177
|
+
represents :assoc_plain, of: :assoc
|
178
|
+
|
179
|
+
log(:assoc_attributes)
|
180
|
+
log(:assoc_plain)
|
181
|
+
|
182
|
+
def assign_attributes(attrs)
|
183
|
+
super attrs.merge(attrs.extract!('plain2'))
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
specify do
|
189
|
+
expect { subject.assign_attributes(assoc_plain: 'value', assoc_attributes: {}, plain1: 'value', plain2: 'value') }
|
190
|
+
.to change { subject.logger }.to(%i[plain1 assoc_attributes assoc_plain plain2])
|
191
|
+
end
|
192
|
+
|
193
|
+
specify do
|
194
|
+
expect { subject.assign_attributes(plain1: 'value', plain2: 'value', assoc_plain: 'value', assoc_attributes: {}) }
|
195
|
+
.to change { subject.logger }.to(%i[plain1 assoc_attributes assoc_plain plain2])
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
126
199
|
end
|
127
200
|
|
128
201
|
describe '#inspect' do
|
129
202
|
specify { expect(stub_model.new.inspect).to match(/#<#<Class:0x\w+> \(no attributes\)>/) }
|
130
203
|
specify { expect(stub_model(:user).new.inspect).to match(/#<User \(no attributes\)>/) }
|
131
|
-
specify
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
204
|
+
specify do
|
205
|
+
expect(stub_model do
|
206
|
+
include ActiveData::Model::Primary
|
207
|
+
primary :count, Integer
|
208
|
+
attribute :object, Object
|
209
|
+
end.new(object: 'String').inspect).to match(/#<#<Class:0x\w+> \*count: nil, object: "String">/) end
|
210
|
+
specify do
|
211
|
+
expect(stub_model(:user) do
|
212
|
+
include ActiveData::Model::Primary
|
213
|
+
primary :count, Integer
|
214
|
+
attribute :object, Object
|
215
|
+
end.new.inspect).to match(/#<User \*count: nil, object: nil>/) end
|
141
216
|
end
|
142
217
|
|
143
218
|
context 'attributes integration' do
|
@@ -149,7 +224,7 @@ describe ActiveData::Model::Attributes do
|
|
149
224
|
|
150
225
|
attribute :id, Integer
|
151
226
|
attribute :hello, Object
|
152
|
-
attribute :string, String, default: ->(record){ record.name }
|
227
|
+
attribute :string, String, default: ->(record) { record.name }
|
153
228
|
attribute :count, Integer, default: '10'
|
154
229
|
attribute(:calc, Integer) { 2 + 3 }
|
155
230
|
attribute :enum, Integer, enum: [1, 2, 3]
|
@@ -157,7 +232,7 @@ describe ActiveData::Model::Attributes do
|
|
157
232
|
attribute :foo, Boolean, default: false
|
158
233
|
collection :array, Integer, enum: [1, 2, 3], default: 7
|
159
234
|
|
160
|
-
def initialize
|
235
|
+
def initialize(name = nil)
|
161
236
|
super()
|
162
237
|
@name = name
|
163
238
|
end
|
@@ -183,19 +258,49 @@ describe ActiveData::Model::Attributes do
|
|
183
258
|
specify { expect { subject.calc = 15 }.to change { subject.calc }.from(5).to(15) }
|
184
259
|
|
185
260
|
context 'enums' do
|
186
|
-
specify
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
specify
|
191
|
-
|
261
|
+
specify do
|
262
|
+
subject.enum = 3
|
263
|
+
expect(subject.enum).to eq(3)
|
264
|
+
end
|
265
|
+
specify do
|
266
|
+
subject.enum = '3'
|
267
|
+
expect(subject.enum).to eq(3)
|
268
|
+
end
|
269
|
+
specify do
|
270
|
+
subject.enum = 10
|
271
|
+
expect(subject.enum).to eq(nil)
|
272
|
+
end
|
273
|
+
specify do
|
274
|
+
subject.enum = 'hello'
|
275
|
+
expect(subject.enum).to eq(nil)
|
276
|
+
end
|
277
|
+
specify do
|
278
|
+
subject.enum_with_default = 3
|
279
|
+
expect(subject.enum_with_default).to eq(3)
|
280
|
+
end
|
281
|
+
specify do
|
282
|
+
subject.enum_with_default = 10
|
283
|
+
expect(subject.enum_with_default).to be_nil
|
284
|
+
end
|
192
285
|
end
|
193
286
|
|
194
287
|
context 'array' do
|
195
|
-
specify
|
196
|
-
|
197
|
-
|
198
|
-
|
288
|
+
specify do
|
289
|
+
subject.array = [2, 4]
|
290
|
+
expect(subject.array).to eq([2, nil])
|
291
|
+
end
|
292
|
+
specify do
|
293
|
+
subject.array = [2, 4]
|
294
|
+
expect(subject.array?).to eq(true)
|
295
|
+
end
|
296
|
+
specify do
|
297
|
+
subject.array = [2, 4]
|
298
|
+
expect(subject.array_values).to eq([1, 2, 3])
|
299
|
+
end
|
300
|
+
specify do
|
301
|
+
subject.array = [2, 4]
|
302
|
+
expect(subject.array_default).to eq(7)
|
303
|
+
end
|
199
304
|
end
|
200
305
|
|
201
306
|
context 'attribute caching' do
|
@@ -233,10 +338,10 @@ describe ActiveData::Model::Attributes do
|
|
233
338
|
specify { expect(ancestor._attributes.keys).to eq(['foo']) }
|
234
339
|
specify { expect(ancestor.instance_methods).to include :foo, :foo= }
|
235
340
|
specify { expect(ancestor.instance_methods).not_to include :bar, :bar=, :baz, :baz= }
|
236
|
-
specify { expect(descendant1._attributes.keys).to eq([
|
341
|
+
specify { expect(descendant1._attributes.keys).to eq(%w[foo bar]) }
|
237
342
|
specify { expect(descendant1.instance_methods).to include :foo, :foo=, :bar, :bar= }
|
238
343
|
specify { expect(descendant1.instance_methods).not_to include :baz, :baz= }
|
239
|
-
specify { expect(descendant2._attributes.keys).to eq([
|
344
|
+
specify { expect(descendant2._attributes.keys).to eq(%w[foo baz moo]) }
|
240
345
|
specify { expect(descendant2.instance_methods).to include :foo, :foo=, :baz, :baz=, :moo, :moo= }
|
241
346
|
specify { expect(descendant2.instance_methods).not_to include :bar, :bar= }
|
242
347
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
require 'spec_helper'
|
3
2
|
|
4
3
|
describe ActiveData::Model::Callbacks do
|
@@ -7,7 +6,7 @@ describe ActiveData::Model::Callbacks do
|
|
7
6
|
include ActiveData::Model::Callbacks
|
8
7
|
attribute :actions, Array, default: []
|
9
8
|
|
10
|
-
def append
|
9
|
+
def append(action)
|
11
10
|
self.actions = actions + [action]
|
12
11
|
end
|
13
12
|
|
@@ -23,7 +22,7 @@ describe ActiveData::Model::Callbacks do
|
|
23
22
|
end
|
24
23
|
|
25
24
|
specify { expect(User.new.actions).to eq([:after_initialize]) }
|
26
|
-
specify { expect(User.create.actions).to eq([
|
25
|
+
specify { expect(User.create.actions).to eq(%i[after_initialize create]) }
|
27
26
|
end
|
28
27
|
|
29
28
|
describe '.before_save, .after_save' do
|
@@ -32,33 +31,33 @@ describe ActiveData::Model::Callbacks do
|
|
32
31
|
User.after_save { append :after_save }
|
33
32
|
end
|
34
33
|
|
35
|
-
specify { expect(User.create.actions).to eq([
|
36
|
-
specify { expect(User.new.tap(&:save).actions).to eq([
|
37
|
-
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([
|
38
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
39
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
34
|
+
specify { expect(User.create.actions).to eq(%i[before_save create after_save]) }
|
35
|
+
specify { expect(User.new.tap(&:save).actions).to eq(%i[before_save create after_save]) }
|
36
|
+
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq(%i[before_save create after_save]) }
|
37
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[before_save create after_save before_save update after_save]) }
|
38
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[before_save create after_save before_save update after_save]) }
|
40
39
|
|
41
40
|
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([:before_save]) }
|
42
|
-
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq([
|
41
|
+
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[before_save create after_save before_save]) }
|
43
42
|
end
|
44
43
|
|
45
44
|
describe '.around_save' do
|
46
45
|
before do
|
47
|
-
User.around_save do |
|
46
|
+
User.around_save do |_, block|
|
48
47
|
append :before_around_save
|
49
48
|
block.call
|
50
49
|
append :after_around_save
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
54
|
-
specify { expect(User.create.actions).to eq([
|
55
|
-
specify { expect(User.new.tap(&:save).actions).to eq([
|
56
|
-
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([
|
57
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
58
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
53
|
+
specify { expect(User.create.actions).to eq(%i[before_around_save create after_around_save]) }
|
54
|
+
specify { expect(User.new.tap(&:save).actions).to eq(%i[before_around_save create after_around_save]) }
|
55
|
+
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq(%i[before_around_save create after_around_save]) }
|
56
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[before_around_save create after_around_save before_around_save update after_around_save]) }
|
57
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[before_around_save create after_around_save before_around_save update after_around_save]) }
|
59
58
|
|
60
|
-
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([
|
61
|
-
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq([
|
59
|
+
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq(%i[before_around_save after_around_save]) }
|
60
|
+
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[before_around_save create after_around_save before_around_save after_around_save]) }
|
62
61
|
end
|
63
62
|
|
64
63
|
describe '.before_create, .after_create' do
|
@@ -67,31 +66,31 @@ describe ActiveData::Model::Callbacks do
|
|
67
66
|
User.after_create { append :after_create }
|
68
67
|
end
|
69
68
|
|
70
|
-
specify { expect(User.create.actions).to eq([
|
71
|
-
specify { expect(User.new.tap(&:save).actions).to eq([
|
72
|
-
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([
|
73
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
74
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
69
|
+
specify { expect(User.create.actions).to eq(%i[before_create create after_create]) }
|
70
|
+
specify { expect(User.new.tap(&:save).actions).to eq(%i[before_create create after_create]) }
|
71
|
+
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq(%i[before_create create after_create]) }
|
72
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[before_create create after_create update]) }
|
73
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[before_create create after_create update]) }
|
75
74
|
|
76
75
|
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([:before_create]) }
|
77
76
|
end
|
78
77
|
|
79
78
|
describe '.around_create' do
|
80
79
|
before do
|
81
|
-
User.around_create do |
|
80
|
+
User.around_create do |_, block|
|
82
81
|
append :before_around_create
|
83
82
|
block.call
|
84
83
|
append :after_around_create
|
85
84
|
end
|
86
85
|
end
|
87
86
|
|
88
|
-
specify { expect(User.create.actions).to eq([
|
89
|
-
specify { expect(User.new.tap(&:save).actions).to eq([
|
90
|
-
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([
|
91
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
92
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
87
|
+
specify { expect(User.create.actions).to eq(%i[before_around_create create after_around_create]) }
|
88
|
+
specify { expect(User.new.tap(&:save).actions).to eq(%i[before_around_create create after_around_create]) }
|
89
|
+
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq(%i[before_around_create create after_around_create]) }
|
90
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[before_around_create create after_around_create update]) }
|
91
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[before_around_create create after_around_create update]) }
|
93
92
|
|
94
|
-
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([
|
93
|
+
specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq(%i[before_around_create after_around_create]) }
|
95
94
|
end
|
96
95
|
|
97
96
|
describe '.before_update, .after_update' do
|
@@ -103,15 +102,15 @@ describe ActiveData::Model::Callbacks do
|
|
103
102
|
specify { expect(User.create.actions).to eq([:create]) }
|
104
103
|
specify { expect(User.new.tap(&:save).actions).to eq([:create]) }
|
105
104
|
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:create]) }
|
106
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
107
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
105
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[create before_update update after_update]) }
|
106
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[create before_update update after_update]) }
|
108
107
|
|
109
|
-
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq([
|
108
|
+
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[create before_update]) }
|
110
109
|
end
|
111
110
|
|
112
111
|
describe '.around_update' do
|
113
112
|
before do
|
114
|
-
User.around_update do |
|
113
|
+
User.around_update do |_, block|
|
115
114
|
append :before_around_update
|
116
115
|
block.call
|
117
116
|
append :after_around_update
|
@@ -121,10 +120,10 @@ describe ActiveData::Model::Callbacks do
|
|
121
120
|
specify { expect(User.create.actions).to eq([:create]) }
|
122
121
|
specify { expect(User.new.tap(&:save).actions).to eq([:create]) }
|
123
122
|
specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:create]) }
|
124
|
-
specify { expect(User.create.tap(&:save).actions).to eq([
|
125
|
-
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([
|
123
|
+
specify { expect(User.create.tap(&:save).actions).to eq(%i[create before_around_update update after_around_update]) }
|
124
|
+
specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq(%i[create before_around_update update after_around_update]) }
|
126
125
|
|
127
|
-
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq([
|
126
|
+
specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[create before_around_update after_around_update]) }
|
128
127
|
end
|
129
128
|
|
130
129
|
describe '.before_validation, .after_validation,
|
@@ -138,7 +137,7 @@ describe ActiveData::Model::Callbacks do
|
|
138
137
|
|
139
138
|
User.before_save { append :before_save }
|
140
139
|
User.after_save { append :after_save }
|
141
|
-
User.around_save do |
|
140
|
+
User.around_save do |_, block|
|
142
141
|
append :before_around_save
|
143
142
|
block.call
|
144
143
|
append :after_around_save
|
@@ -146,7 +145,7 @@ describe ActiveData::Model::Callbacks do
|
|
146
145
|
|
147
146
|
User.before_create { append :before_create }
|
148
147
|
User.after_create { append :after_create }
|
149
|
-
User.around_create do |
|
148
|
+
User.around_create do |_, block|
|
150
149
|
append :before_around_create
|
151
150
|
block.call
|
152
151
|
append :after_around_create
|
@@ -154,7 +153,7 @@ describe ActiveData::Model::Callbacks do
|
|
154
153
|
|
155
154
|
User.before_update { append :before_update }
|
156
155
|
User.after_update { append :after_update }
|
157
|
-
User.around_update do |
|
156
|
+
User.around_update do |_, block|
|
158
157
|
append :before_around_update
|
159
158
|
block.call
|
160
159
|
append :after_around_update
|
@@ -162,32 +161,32 @@ describe ActiveData::Model::Callbacks do
|
|
162
161
|
|
163
162
|
User.before_destroy { append :before_destroy }
|
164
163
|
User.after_destroy { append :after_destroy }
|
165
|
-
User.around_destroy do |
|
164
|
+
User.around_destroy do |_, block|
|
166
165
|
append :before_around_destroy
|
167
166
|
block.call
|
168
167
|
append :after_around_destroy
|
169
168
|
end
|
170
169
|
end
|
171
170
|
|
172
|
-
specify
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
171
|
+
specify do
|
172
|
+
expect(User.create.tap(&:save).destroy.actions).to eq(%i[
|
173
|
+
before_validation after_validation
|
174
|
+
before_save before_around_save
|
175
|
+
before_create before_around_create
|
176
|
+
create
|
177
|
+
after_around_create after_create
|
178
|
+
after_around_save after_save
|
179
|
+
before_validation after_validation
|
180
|
+
before_save before_around_save
|
181
|
+
before_update before_around_update
|
182
|
+
update
|
183
|
+
after_around_update after_update
|
184
|
+
after_around_save after_save
|
185
|
+
before_destroy before_around_destroy
|
186
|
+
destroy
|
187
|
+
after_around_destroy after_destroy
|
188
|
+
])
|
189
|
+
end
|
191
190
|
end
|
192
191
|
|
193
192
|
describe '.before_destroy, .after_destroy' do
|
@@ -196,27 +195,27 @@ describe ActiveData::Model::Callbacks do
|
|
196
195
|
User.after_destroy { append :after_destroy }
|
197
196
|
end
|
198
197
|
|
199
|
-
specify { expect(User.new.destroy.actions).to eq([
|
200
|
-
specify { expect(User.create.destroy.actions).to eq([
|
198
|
+
specify { expect(User.new.destroy.actions).to eq(%i[before_destroy destroy after_destroy]) }
|
199
|
+
specify { expect(User.create.destroy.actions).to eq(%i[create before_destroy destroy after_destroy]) }
|
201
200
|
|
202
201
|
specify { expect(User.new.destroy { false }.actions).to eq([:before_destroy]) }
|
203
|
-
specify { expect(User.create.destroy { false }.actions).to eq([
|
202
|
+
specify { expect(User.create.destroy { false }.actions).to eq(%i[create before_destroy]) }
|
204
203
|
end
|
205
204
|
|
206
205
|
describe '.around_destroy' do
|
207
206
|
before do
|
208
|
-
User.around_destroy do |
|
207
|
+
User.around_destroy do |_, block|
|
209
208
|
append :before_around_destroy
|
210
209
|
block.call
|
211
210
|
append :after_around_destroy
|
212
211
|
end
|
213
212
|
end
|
214
213
|
|
215
|
-
specify { expect(User.new.destroy.actions).to eq([
|
216
|
-
specify { expect(User.create.destroy.actions).to eq([
|
214
|
+
specify { expect(User.new.destroy.actions).to eq(%i[before_around_destroy destroy after_around_destroy]) }
|
215
|
+
specify { expect(User.create.destroy.actions).to eq(%i[create before_around_destroy destroy after_around_destroy]) }
|
217
216
|
|
218
|
-
specify { expect(User.new.destroy { false }.actions).to eq([
|
219
|
-
specify { expect(User.create.destroy { false }.actions).to eq([
|
217
|
+
specify { expect(User.new.destroy { false }.actions).to eq(%i[before_around_destroy after_around_destroy]) }
|
218
|
+
specify { expect(User.create.destroy { false }.actions).to eq(%i[create before_around_destroy after_around_destroy]) }
|
220
219
|
end
|
221
220
|
|
222
221
|
context 'unsavable, undestroyable' do
|
@@ -229,7 +228,7 @@ describe ActiveData::Model::Callbacks do
|
|
229
228
|
|
230
229
|
validates :validated, presence: true
|
231
230
|
|
232
|
-
def append
|
231
|
+
def append(action)
|
233
232
|
self.actions = actions + [action]
|
234
233
|
end
|
235
234
|
end
|
@@ -292,14 +291,14 @@ describe ActiveData::Model::Callbacks do
|
|
292
291
|
|
293
292
|
specify do
|
294
293
|
user.save { true }
|
295
|
-
expect(user.actions).to eq([
|
294
|
+
expect(user.actions).to eq(%i[before_validation after_validation])
|
296
295
|
end
|
297
296
|
|
298
297
|
specify do
|
299
298
|
begin
|
300
299
|
user.save! { true }
|
301
300
|
rescue ActiveData::ValidationError
|
302
|
-
expect(user.actions).to eq([
|
301
|
+
expect(user.actions).to eq(%i[before_validation after_validation])
|
303
302
|
end
|
304
303
|
end
|
305
304
|
|
@@ -1,9 +1,8 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
require 'spec_helper'
|
3
2
|
|
4
3
|
describe ActiveData::Model::Dirty do
|
5
4
|
before do
|
6
|
-
stub_class(:author, ActiveRecord::Base) {
|
5
|
+
stub_class(:author, ActiveRecord::Base) {}
|
7
6
|
stub_model :premodel do
|
8
7
|
include ActiveData::Model::Persistence
|
9
8
|
include ActiveData::Model::Localization
|
@@ -20,7 +19,7 @@ describe ActiveData::Model::Dirty do
|
|
20
19
|
embeds_one :something do
|
21
20
|
attribute :value, String
|
22
21
|
end
|
23
|
-
|
22
|
+
attribute :name, String
|
24
23
|
alias_attribute :n, :name
|
25
24
|
collection :numbers, Integer
|
26
25
|
localized :title, String
|
@@ -38,21 +37,31 @@ describe ActiveData::Model::Dirty do
|
|
38
37
|
specify { expect(Model.new(authors: [author]).changes).to eq('author_ids' => [[], [author.id]]) }
|
39
38
|
specify { expect(Model.new(author_ids: [author.id]).changes).to eq('author_ids' => [[], [author.id]]) }
|
40
39
|
|
41
|
-
specify
|
42
|
-
.
|
40
|
+
specify do
|
41
|
+
expect(Model.new(author: author, name: 'Name2').changes)
|
42
|
+
.to eq('author_id' => [nil, author.id], 'name' => [nil, 'Name2'])
|
43
|
+
end
|
43
44
|
|
44
|
-
specify
|
45
|
+
specify do
|
46
|
+
expect(Model.instantiate(author_id: other_author.id)
|
45
47
|
.tap { |m| m.update(author_id: author.id) }.changes)
|
46
|
-
|
47
|
-
|
48
|
+
.to eq('author_id' => [other_author.id, author.id])
|
49
|
+
end
|
50
|
+
specify do
|
51
|
+
expect(Model.instantiate(author_id: other_author.id)
|
48
52
|
.tap { |m| m.update(author: author) }.changes)
|
49
|
-
|
50
|
-
|
53
|
+
.to eq('author_id' => [other_author.id, author.id])
|
54
|
+
end
|
55
|
+
specify do
|
56
|
+
expect(Model.instantiate(author_ids: [other_author.id])
|
51
57
|
.tap { |m| m.update(author_ids: [author.id]) }.changes)
|
52
|
-
|
53
|
-
|
58
|
+
.to eq('author_ids' => [[other_author.id], [author.id]])
|
59
|
+
end
|
60
|
+
specify do
|
61
|
+
expect(Model.instantiate(author_ids: [other_author.id])
|
54
62
|
.tap { |m| m.update(authors: [author]) }.changes)
|
55
|
-
|
63
|
+
.to eq('author_ids' => [[other_author.id], [author.id]])
|
64
|
+
end
|
56
65
|
|
57
66
|
specify { expect(Model.new(a: 'blabla').changes).to eq('age' => [33, nil]) }
|
58
67
|
specify { expect(Model.new(a: '42').changes).to eq('age' => [33, 42]) }
|