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::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 { expect(stub_model {
46
- include ActiveData::Model::Primary
47
- primary :count, Integer
48
- attribute :object, Object
49
- }.inspect).to match(/#<Class:0x\w+>\(\*count: Integer, object: Object\)/) }
50
- specify { expect(stub_model(:user) {
51
- include ActiveData::Model::Primary
52
- primary :count, Integer
53
- attribute :object, Object
54
- }.inspect).to match('User(*count: Integer, object: Object)') }
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 { expect(model.new(name: 'Name').attributes)
115
- .to match({'id' => nil, 'full_name' => 'Name', 't' => {}, 'author' => nil, 'projects' => nil}) }
116
- specify { expect(model.new(name: 'Name').attributes(false))
117
- .to match({'id' => nil, 'full_name' => 'Name', 't' => {}}) }
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) { { id: 42, full_name: 'Name', missed: 'value' } }
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 { expect(stub_model {
132
- include ActiveData::Model::Primary
133
- primary :count, Integer
134
- attribute :object, Object
135
- }.new(object: 'String').inspect).to match(/#<#<Class:0x\w+> \*count: nil, object: "String">/) }
136
- specify { expect(stub_model(:user) {
137
- include ActiveData::Model::Primary
138
- primary :count, Integer
139
- attribute :object, Object
140
- }.new.inspect).to match(/#<User \*count: nil, object: nil>/) }
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 name = nil
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 { subject.enum = 3; expect(subject.enum).to eq(3) }
187
- specify { subject.enum = '3'; expect(subject.enum).to eq(3) }
188
- specify { subject.enum = 10; expect(subject.enum).to eq(nil) }
189
- specify { subject.enum = 'hello'; expect(subject.enum).to eq(nil) }
190
- specify { subject.enum_with_default = 3; expect(subject.enum_with_default).to eq(3) }
191
- specify { subject.enum_with_default = 10; expect(subject.enum_with_default).to be_nil }
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 { subject.array = [2, 4]; expect(subject.array).to eq([2, nil]) }
196
- specify { subject.array = [2, 4]; expect(subject.array?).to eq(true) }
197
- specify { subject.array = [2, 4]; expect(subject.array_values).to eq([1, 2, 3]) }
198
- specify { subject.array = [2, 4]; expect(subject.array_default).to eq(7) }
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(['foo', 'bar']) }
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(['foo', 'baz', 'moo']) }
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 action
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([:after_initialize, :create]) }
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([:before_save, :create, :after_save]) }
36
- specify { expect(User.new.tap(&:save).actions).to eq([:before_save, :create, :after_save]) }
37
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:before_save, :create, :after_save]) }
38
- specify { expect(User.create.tap(&:save).actions).to eq([:before_save, :create, :after_save, :before_save, :update, :after_save]) }
39
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:before_save, :create, :after_save, :before_save, :update, :after_save]) }
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([:before_save, :create, :after_save, :before_save]) }
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 |user, block|
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([:before_around_save, :create, :after_around_save]) }
55
- specify { expect(User.new.tap(&:save).actions).to eq([:before_around_save, :create, :after_around_save]) }
56
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:before_around_save, :create, :after_around_save]) }
57
- specify { expect(User.create.tap(&:save).actions).to eq([:before_around_save, :create, :after_around_save, :before_around_save, :update, :after_around_save]) }
58
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:before_around_save, :create, :after_around_save, :before_around_save, :update, :after_around_save]) }
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([:before_around_save, :after_around_save]) }
61
- specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq([:before_around_save, :create, :after_around_save, :before_around_save, :after_around_save]) }
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([:before_create, :create, :after_create]) }
71
- specify { expect(User.new.tap(&:save).actions).to eq([:before_create, :create, :after_create]) }
72
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:before_create, :create, :after_create]) }
73
- specify { expect(User.create.tap(&:save).actions).to eq([:before_create, :create, :after_create, :update]) }
74
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:before_create, :create, :after_create, :update]) }
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 |user, block|
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([:before_around_create, :create, :after_around_create]) }
89
- specify { expect(User.new.tap(&:save).actions).to eq([:before_around_create, :create, :after_around_create]) }
90
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:before_around_create, :create, :after_around_create]) }
91
- specify { expect(User.create.tap(&:save).actions).to eq([:before_around_create, :create, :after_around_create, :update]) }
92
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:before_around_create, :create, :after_around_create, :update]) }
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([:before_around_create, :after_around_create]) }
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([:create, :before_update, :update, :after_update]) }
107
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:create, :before_update, :update, :after_update]) }
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([:create, :before_update]) }
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 |user, block|
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([:create, :before_around_update, :update, :after_around_update]) }
125
- specify { expect(User.create.tap { |u| u.update({}) }.actions).to eq([:create, :before_around_update, :update, :after_around_update]) }
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([:create, :before_around_update, :after_around_update]) }
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 |user, block|
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 |user, block|
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 |user, block|
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 |user, block|
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 { expect(User.create.tap(&:save).destroy.actions).to eq([
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
-
180
- :before_validation, :after_validation,
181
- :before_save, :before_around_save,
182
- :before_update, :before_around_update,
183
- :update,
184
- :after_around_update, :after_update,
185
- :after_around_save, :after_save,
186
-
187
- :before_destroy, :before_around_destroy,
188
- :destroy,
189
- :after_around_destroy, :after_destroy
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([:before_destroy, :destroy, :after_destroy]) }
200
- specify { expect(User.create.destroy.actions).to eq([:create, :before_destroy, :destroy, :after_destroy]) }
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([:create, :before_destroy]) }
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 |user, block|
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([:before_around_destroy, :destroy, :after_around_destroy]) }
216
- specify { expect(User.create.destroy.actions).to eq([:create, :before_around_destroy, :destroy, :after_around_destroy]) }
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([:before_around_destroy, :after_around_destroy]) }
219
- specify { expect(User.create.destroy { false }.actions).to eq([:create, :before_around_destroy, :after_around_destroy]) }
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 action
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([:before_validation, :after_validation])
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([:before_validation, :after_validation])
301
+ expect(user.actions).to eq(%i[before_validation after_validation])
303
302
  end
304
303
  end
305
304
 
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe ActiveData::Model::Conventions do
@@ -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
- represents :name, of: :author
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 { expect(Model.new(author: author, name: 'Name2').changes)
42
- .to eq('author_id' => [nil, author.id], 'name' => ['Name', 'Name2']) }
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 { expect(Model.instantiate(author_id: other_author.id)
45
+ specify do
46
+ expect(Model.instantiate(author_id: other_author.id)
45
47
  .tap { |m| m.update(author_id: author.id) }.changes)
46
- .to eq('author_id' => [other_author.id, author.id]) }
47
- specify { expect(Model.instantiate(author_id: other_author.id)
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
- .to eq('author_id' => [other_author.id, author.id]) }
50
- specify { expect(Model.instantiate(author_ids: [other_author.id])
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
- .to eq('author_ids' => [[other_author.id], [author.id]]) }
53
- specify { expect(Model.instantiate(author_ids: [other_author.id])
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
- .to eq('author_ids' => [[other_author.id], [author.id]]) }
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]) }