granite-form 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +1 -2
  3. data/.github/workflows/{ci.yml → ruby.yml} +22 -4
  4. data/.rubocop.yml +1 -1
  5. data/.rubocop_todo.yml +3 -3
  6. data/Appraisals +1 -2
  7. data/CHANGELOG.md +7 -0
  8. data/README.md +0 -2
  9. data/docker-compose.yml +14 -0
  10. data/gemfiles/rails.5.0.gemfile +0 -1
  11. data/gemfiles/rails.5.1.gemfile +0 -1
  12. data/gemfiles/rails.5.2.gemfile +0 -1
  13. data/granite-form.gemspec +15 -15
  14. data/lib/granite/form/active_record/associations.rb +1 -1
  15. data/lib/granite/form/base.rb +1 -2
  16. data/lib/granite/form/errors.rb +0 -15
  17. data/lib/granite/form/model/associations/base.rb +0 -4
  18. data/lib/granite/form/model/associations/collection/embedded.rb +2 -1
  19. data/lib/granite/form/model/associations/collection/proxy.rb +1 -1
  20. data/lib/granite/form/model/associations/embeds_any.rb +7 -0
  21. data/lib/granite/form/model/associations/embeds_many.rb +9 -58
  22. data/lib/granite/form/model/associations/embeds_one.rb +7 -36
  23. data/lib/granite/form/model/associations/nested_attributes.rb +5 -5
  24. data/lib/granite/form/model/associations/persistence_adapters/active_record.rb +0 -4
  25. data/lib/granite/form/model/associations/persistence_adapters/base.rb +0 -4
  26. data/lib/granite/form/model/associations/references_many.rb +0 -32
  27. data/lib/granite/form/model/associations/references_one.rb +0 -28
  28. data/lib/granite/form/model/associations/reflections/embeds_any.rb +1 -1
  29. data/lib/granite/form/model/associations/reflections/references_any.rb +0 -4
  30. data/lib/granite/form/model/associations/reflections/references_one.rb +0 -2
  31. data/lib/granite/form/model/associations/reflections/singular.rb +0 -8
  32. data/lib/granite/form/model/associations.rb +0 -6
  33. data/lib/granite/form/model/attributes/base.rb +1 -1
  34. data/lib/granite/form/model/attributes/reflections/attribute.rb +0 -6
  35. data/lib/granite/form/model/attributes/reflections/base.rb +8 -7
  36. data/lib/granite/form/model/attributes/reflections/reference_one.rb +0 -6
  37. data/lib/granite/form/model/persistence.rb +1 -19
  38. data/lib/granite/form/model.rb +0 -2
  39. data/lib/granite/form/version.rb +1 -1
  40. data/spec/granite/form/active_record/associations_spec.rb +16 -18
  41. data/spec/granite/form/model/associations/embeds_many_spec.rb +29 -305
  42. data/spec/granite/form/model/associations/embeds_one_spec.rb +27 -212
  43. data/spec/granite/form/model/associations/nested_attributes_spec.rb +0 -95
  44. data/spec/granite/form/model/associations/references_many_spec.rb +5 -326
  45. data/spec/granite/form/model/associations/references_one_spec.rb +6 -278
  46. data/spec/granite/form/model/associations/reflections/embeds_any_spec.rb +1 -2
  47. data/spec/granite/form/model/associations/reflections/embeds_many_spec.rb +18 -26
  48. data/spec/granite/form/model/associations/reflections/embeds_one_spec.rb +16 -23
  49. data/spec/granite/form/model/associations/reflections/references_many_spec.rb +1 -1
  50. data/spec/granite/form/model/associations/reflections/references_one_spec.rb +1 -22
  51. data/spec/granite/form/model/associations/validations_spec.rb +0 -3
  52. data/spec/granite/form/model/associations_spec.rb +3 -24
  53. data/spec/granite/form/model/dirty_spec.rb +1 -1
  54. data/spec/granite/form/model/persistence_spec.rb +0 -2
  55. data/spec/granite/form/model/validations/associated_spec.rb +2 -4
  56. data/spec/granite/form/model/validations/nested_spec.rb +2 -4
  57. data/spec/spec_helper.rb +0 -15
  58. data/spec/support/active_record.rb +20 -0
  59. data/spec/support/shared/nested_attribute_examples.rb +3 -21
  60. metadata +32 -38
  61. data/.github/workflows/main.yml +0 -29
  62. data/gemfiles/rails.4.2.gemfile +0 -15
  63. data/lib/granite/form/model/callbacks.rb +0 -72
  64. data/lib/granite/form/model/lifecycle.rb +0 -309
  65. data/spec/granite/form/model/callbacks_spec.rb +0 -337
  66. data/spec/granite/form/model/lifecycle_spec.rb +0 -356
@@ -1,337 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Callbacks do
4
- before do
5
- stub_model(:user) do
6
- include Granite::Form::Model::Callbacks
7
- attribute :actions, Array, default: []
8
-
9
- def append(action)
10
- self.actions = actions + [action]
11
- end
12
-
13
- define_create { append :create }
14
- define_update { append :update }
15
- define_destroy { append :destroy }
16
- end
17
- end
18
-
19
- describe '.after_initialize' do
20
- before do
21
- User.after_initialize { append :after_initialize }
22
- end
23
-
24
- specify { expect(User.new.actions).to eq([:after_initialize]) }
25
- specify { expect(User.create.actions).to eq(%i[after_initialize create]) }
26
- end
27
-
28
- describe '.before_save, .after_save' do
29
- before do
30
- User.before_save { append :before_save }
31
- User.after_save { append :after_save }
32
- end
33
-
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]) }
39
-
40
- specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([:before_save]) }
41
- specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[before_save create after_save before_save]) }
42
- end
43
-
44
- describe '.around_save' do
45
- before do
46
- User.around_save do |_, block|
47
- append :before_around_save
48
- block.call
49
- append :after_around_save
50
- end
51
- end
52
-
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]) }
58
-
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]) }
61
- end
62
-
63
- describe '.before_create, .after_create' do
64
- before do
65
- User.before_create { append :before_create }
66
- User.after_create { append :after_create }
67
- end
68
-
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]) }
74
-
75
- specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq([:before_create]) }
76
- end
77
-
78
- describe '.around_create' do
79
- before do
80
- User.around_create do |_, block|
81
- append :before_around_create
82
- block.call
83
- append :after_around_create
84
- end
85
- end
86
-
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]) }
92
-
93
- specify { expect(User.new.tap { |u| u.save { false } }.actions).to eq(%i[before_around_create after_around_create]) }
94
- end
95
-
96
- describe '.before_update, .after_update' do
97
- before do
98
- User.before_update { append :before_update }
99
- User.after_update { append :after_update }
100
- end
101
-
102
- specify { expect(User.create.actions).to eq([:create]) }
103
- specify { expect(User.new.tap(&:save).actions).to eq([:create]) }
104
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:create]) }
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]) }
107
-
108
- specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[create before_update]) }
109
- end
110
-
111
- describe '.around_update' do
112
- before do
113
- User.around_update do |_, block|
114
- append :before_around_update
115
- block.call
116
- append :after_around_update
117
- end
118
- end
119
-
120
- specify { expect(User.create.actions).to eq([:create]) }
121
- specify { expect(User.new.tap(&:save).actions).to eq([:create]) }
122
- specify { expect(User.new.tap { |u| u.update({}) }.actions).to eq([:create]) }
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]) }
125
-
126
- specify { expect(User.create.tap { |u| u.save { false } }.actions).to eq(%i[create before_around_update after_around_update]) }
127
- end
128
-
129
- describe '.before_validation, .after_validation,
130
- .before_save, .after_save, .around_save,
131
- .before_create, .after_create, .around_create,
132
- .before_update, .after_update, .around_update
133
- .before_destroy, .after_destroy, .around_destroy' do
134
- before do
135
- User.before_validation { append :before_validation }
136
- User.after_validation { append :after_validation }
137
-
138
- User.before_save { append :before_save }
139
- User.after_save { append :after_save }
140
- User.around_save do |_, block|
141
- append :before_around_save
142
- block.call
143
- append :after_around_save
144
- end
145
-
146
- User.before_create { append :before_create }
147
- User.after_create { append :after_create }
148
- User.around_create do |_, block|
149
- append :before_around_create
150
- block.call
151
- append :after_around_create
152
- end
153
-
154
- User.before_update { append :before_update }
155
- User.after_update { append :after_update }
156
- User.around_update do |_, block|
157
- append :before_around_update
158
- block.call
159
- append :after_around_update
160
- end
161
-
162
- User.before_destroy { append :before_destroy }
163
- User.after_destroy { append :after_destroy }
164
- User.around_destroy do |_, block|
165
- append :before_around_destroy
166
- block.call
167
- append :after_around_destroy
168
- end
169
- end
170
-
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
190
- end
191
-
192
- describe '.before_destroy, .after_destroy' do
193
- before do
194
- User.before_destroy { append :before_destroy }
195
- User.after_destroy { append :after_destroy }
196
- end
197
-
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]) }
200
-
201
- specify { expect(User.new.destroy { false }.actions).to eq([:before_destroy]) }
202
- specify { expect(User.create.destroy { false }.actions).to eq(%i[create before_destroy]) }
203
- end
204
-
205
- describe '.around_destroy' do
206
- before do
207
- User.around_destroy do |_, block|
208
- append :before_around_destroy
209
- block.call
210
- append :after_around_destroy
211
- end
212
- end
213
-
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]) }
216
-
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]) }
219
- end
220
-
221
- context 'unsavable, undestroyable' do
222
- before do
223
- stub_model(:user) do
224
- include Granite::Form::Model::Callbacks
225
-
226
- attribute :actions, Array, default: []
227
- attribute :validated, Boolean, default: false
228
-
229
- validates :validated, presence: true
230
-
231
- def append(action)
232
- self.actions = actions + [action]
233
- end
234
- end
235
- end
236
-
237
- before do
238
- User.before_validation { append :before_validation }
239
- User.after_validation { append :after_validation }
240
-
241
- User.before_save { append :before_save }
242
- User.after_save { append :after_save }
243
- User.around_save do |&block|
244
- append :before_around_save
245
- block.call
246
- append :after_around_save
247
- end
248
-
249
- User.before_create { append :before_create }
250
- User.after_create { append :after_create }
251
- User.around_create do |&block|
252
- append :before_around_create
253
- block.call
254
- append :after_around_create
255
- end
256
-
257
- User.before_update { append :before_update }
258
- User.after_update { append :after_update }
259
- User.around_update do |&block|
260
- append :before_around_update
261
- block.call
262
- append :after_around_update
263
- end
264
-
265
- User.before_destroy { append :before_destroy }
266
- User.after_destroy { append :after_destroy }
267
- User.around_destroy do |&block|
268
- append :before_around_destroy
269
- block.call
270
- append :after_around_destroy
271
- end
272
- end
273
-
274
- let(:user) { User.new }
275
-
276
- specify do
277
- begin
278
- user.save
279
- rescue Granite::Form::UnsavableObject
280
- expect(user.actions).to eq([])
281
- end
282
- end
283
-
284
- specify do
285
- begin
286
- user.save!
287
- rescue Granite::Form::UnsavableObject
288
- expect(user.actions).to eq([])
289
- end
290
- end
291
-
292
- specify do
293
- user.save { true }
294
- expect(user.actions).to eq(%i[before_validation after_validation])
295
- end
296
-
297
- specify do
298
- begin
299
- user.save! { true }
300
- rescue Granite::Form::ValidationError
301
- expect(user.actions).to eq(%i[before_validation after_validation])
302
- end
303
- end
304
-
305
- specify do
306
- begin
307
- user.update({})
308
- rescue Granite::Form::UnsavableObject
309
- expect(user.actions).to eq([])
310
- end
311
- end
312
-
313
- specify do
314
- begin
315
- user.update!({})
316
- rescue Granite::Form::UnsavableObject
317
- expect(user.actions).to eq([])
318
- end
319
- end
320
-
321
- specify do
322
- begin
323
- user.destroy
324
- rescue Granite::Form::UndestroyableObject
325
- expect(user.actions).to eq([])
326
- end
327
- end
328
-
329
- specify do
330
- begin
331
- user.destroy!
332
- rescue Granite::Form::UndestroyableObject
333
- expect(user.actions).to eq([])
334
- end
335
- end
336
- end
337
- end
@@ -1,356 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Granite::Form::Model::Lifecycle do
4
- context do
5
- before do
6
- stub_model(:user) do
7
- include Granite::Form::Model::Lifecycle
8
- end
9
- end
10
-
11
- subject { User.new }
12
-
13
- %i[save create update destroy].each do |action|
14
- specify { expect { subject.public_send "_#{action}_performer=", '' }.to raise_error NoMethodError }
15
- end
16
-
17
- context 'performer execution' do
18
- let(:foo) { true }
19
-
20
- specify { expect { subject.save { foo } }.to raise_error NameError }
21
- specify { expect { subject.save { |_| attributes } }.to raise_error NameError }
22
- specify { expect(subject.save { attributes }).to eq(true) }
23
- specify { expect(subject.save { |_| foo }).to eq(true) }
24
- end
25
-
26
- context 'save' do
27
- specify { expect { subject.save }.to raise_error Granite::Form::UnsavableObject }
28
- specify { expect { subject.save! }.to raise_error Granite::Form::UnsavableObject }
29
-
30
- specify { expect(subject.save { true }).to eq(true) }
31
- specify { expect(subject.save! { true }).to eq(true) }
32
-
33
- context 'instance performer' do
34
- before { subject.define_save { false } }
35
-
36
- specify { expect(subject.save).to eq(false) }
37
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
38
- end
39
-
40
- context 'create performer' do
41
- before { User.define_create { true } }
42
-
43
- specify { expect(subject.save).to eq(true) }
44
- specify { expect(subject.save!).to eq(true) }
45
-
46
- context do
47
- subject { User.create }
48
-
49
- specify { expect { subject.save }.to raise_error Granite::Form::UnsavableObject }
50
- specify { expect { subject.save! }.to raise_error Granite::Form::UnsavableObject }
51
- end
52
-
53
- context 'instance performer' do
54
- before { subject.define_create { false } }
55
-
56
- specify { expect(subject.save).to eq(false) }
57
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
58
- end
59
- end
60
-
61
- context 'update performer' do
62
- before { User.define_update { true } }
63
-
64
- specify { expect { subject.save }.to raise_error Granite::Form::UnsavableObject }
65
- specify { expect { subject.save! }.to raise_error Granite::Form::UnsavableObject }
66
-
67
- context do
68
- subject { User.new.tap { |u| u.save { true } } }
69
-
70
- specify { expect(subject.save).to eq(true) }
71
- specify { expect(subject.save!).to eq(true) }
72
-
73
- context 'instance performer' do
74
- before { subject.define_update { false } }
75
-
76
- specify { expect(subject.save).to eq(false) }
77
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
78
- end
79
- end
80
- end
81
-
82
- context 'performers execution' do
83
- before do
84
- stub_model(:user) do
85
- include Granite::Form::Model::Lifecycle
86
-
87
- attribute :actions, Array, default: []
88
-
89
- def append(action)
90
- self.actions = actions + [action]
91
- end
92
-
93
- define_create { append :create }
94
- define_update { append :update }
95
- define_destroy { append :destroy }
96
- end
97
- end
98
-
99
- subject { User.new }
100
-
101
- specify do
102
- subject.destroy
103
- subject.save
104
- subject.save
105
- subject.destroy
106
- subject.destroy
107
- subject.save
108
- expect(subject.actions).to eq(%i[destroy create update destroy destroy create])
109
- end
110
- end
111
- end
112
-
113
- context 'destroy' do
114
- specify { expect { subject.destroy }.to raise_error Granite::Form::UndestroyableObject }
115
- specify { expect { subject.destroy! }.to raise_error Granite::Form::UndestroyableObject }
116
-
117
- specify { expect(subject.destroy { true }).to be_a User }
118
- specify { expect(subject.destroy! { true }).to be_a User }
119
-
120
- context 'instance performer' do
121
- before { subject.define_save { false } }
122
-
123
- specify { expect(subject.save).to eq(false) }
124
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
125
- end
126
- end
127
- end
128
-
129
- context do
130
- before do
131
- stub_class(:storage) do
132
- def self.storage
133
- @storage ||= {}
134
- end
135
- end
136
- end
137
-
138
- before do
139
- stub_model(:user) do
140
- include Granite::Form::Model::Lifecycle
141
- delegate :generate_id, to: 'self.class'
142
-
143
- attribute :id, Integer, &:generate_id
144
- attribute :name, String
145
- validates :id, :name, presence: true
146
-
147
- def self.generate_id
148
- @id = @id.to_i.next
149
- end
150
-
151
- define_save do
152
- Storage.storage.merge!(id => attributes.symbolize_keys)
153
- end
154
-
155
- define_destroy do
156
- Storage.storage.delete(id)
157
- end
158
- end
159
- end
160
-
161
- describe '.create' do
162
- subject { User.create(name: 'Jonny') }
163
-
164
- it { is_expected.to be_a User }
165
- it { is_expected.to be_valid }
166
- it { is_expected.to be_persisted }
167
-
168
- context 'invalid' do
169
- subject { User.create }
170
-
171
- it { is_expected.to be_a User }
172
- it { is_expected.to be_invalid }
173
- it { is_expected.not_to be_persisted }
174
- end
175
- end
176
-
177
- describe '.create!' do
178
- subject { User.create!(name: 'Jonny') }
179
-
180
- it { is_expected.to be_a User }
181
- it { is_expected.to be_valid }
182
- it { is_expected.to be_persisted }
183
-
184
- context 'invalid' do
185
- subject { User.create! }
186
-
187
- specify { expect { subject }.to raise_error Granite::Form::ValidationError }
188
- end
189
- end
190
-
191
- describe '#update, #update!' do
192
- subject { User.new }
193
-
194
- specify { expect { subject.update(name: 'Jonny') }.to change { subject.persisted? }.from(false).to(true) }
195
- specify { expect { subject.update!(name: 'Jonny') }.to change { subject.persisted? }.from(false).to(true) }
196
-
197
- specify { expect { subject.update({}) }.not_to change { subject.persisted? } }
198
- specify { expect { subject.update!({}) }.to raise_error Granite::Form::ValidationError }
199
-
200
- specify do
201
- expect { subject.update(name: 'Jonny') }
202
- .to change { Storage.storage.keys }.from([]).to([subject.id])
203
- end
204
- specify do
205
- expect { subject.update!(name: 'Jonny') }
206
- .to change { Storage.storage.keys }.from([]).to([subject.id])
207
- end
208
- end
209
-
210
- describe '#update_attributes, #update_attributes!' do
211
- subject { User.new }
212
-
213
- specify { expect { subject.update_attributes(name: 'Jonny') }.to change { subject.persisted? }.from(false).to(true) }
214
- specify { expect { subject.update_attributes!(name: 'Jonny') }.to change { subject.persisted? }.from(false).to(true) }
215
-
216
- specify { expect { subject.update_attributes({}) }.not_to change { subject.persisted? } }
217
- specify { expect { subject.update_attributes!({}) }.to raise_error Granite::Form::ValidationError }
218
-
219
- specify do
220
- expect { subject.update_attributes(name: 'Jonny') }
221
- .to change { Storage.storage.keys }.from([]).to([subject.id])
222
- end
223
- specify do
224
- expect { subject.update_attributes!(name: 'Jonny') }
225
- .to change { Storage.storage.keys }.from([]).to([subject.id])
226
- end
227
- end
228
-
229
- describe '#save, #save!' do
230
- context 'invalid' do
231
- subject { User.new }
232
-
233
- it { is_expected.to be_invalid }
234
- it { is_expected.not_to be_persisted }
235
-
236
- specify { expect(subject.save).to eq(false) }
237
- specify { expect { subject.save! }.to raise_error Granite::Form::ValidationError }
238
-
239
- specify { expect { subject.save }.not_to change { subject.persisted? } }
240
- specify do
241
- expect { muffle(Granite::Form::ValidationError) { subject.save! } }
242
- .not_to change { subject.persisted? }
243
- end
244
- end
245
-
246
- context 'create' do
247
- subject { User.new(name: 'Jonny') }
248
-
249
- it { is_expected.to be_valid }
250
- it { is_expected.not_to be_persisted }
251
-
252
- specify { expect(subject.save).to eq(true) }
253
- specify { expect(subject.save!).to eq(true) }
254
-
255
- specify { expect { subject.save }.to change { subject.persisted? }.from(false).to(true) }
256
- specify { expect { subject.save! }.to change { subject.persisted? }.from(false).to(true) }
257
-
258
- specify { expect { subject.save }.to change { Storage.storage.keys }.from([]).to([subject.id]) }
259
- specify { expect { subject.save! }.to change { Storage.storage.keys }.from([]).to([subject.id]) }
260
-
261
- context 'save failed' do
262
- before { User.define_save { false } }
263
-
264
- it { is_expected.not_to be_persisted }
265
-
266
- specify { expect(subject.save).to eq(false) }
267
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
268
-
269
- specify { expect { subject.save }.not_to change { subject.persisted? } }
270
- specify do
271
- expect { muffle(Granite::Form::ObjectNotSaved) { subject.save! } }
272
- .not_to change { subject.persisted? }
273
- end
274
- end
275
- end
276
-
277
- context 'update' do
278
- subject! { User.new(name: 'Jonny').tap(&:save).tap { |u| u.name = 'Jimmy' } }
279
-
280
- it { is_expected.to be_valid }
281
- it { is_expected.to be_persisted }
282
-
283
- specify { expect(subject.save).to eq(true) }
284
- specify { expect(subject.save!).to eq(true) }
285
-
286
- specify { expect { subject.save }.not_to change { subject.persisted? } }
287
- specify { expect { subject.save! }.not_to change { subject.persisted? } }
288
-
289
- specify do
290
- expect { subject.save }.to change { Storage.storage[subject.id] }
291
- .from(hash_including(name: 'Jonny')).to(hash_including(name: 'Jimmy'))
292
- end
293
- specify do
294
- expect { subject.save! }.to change { Storage.storage[subject.id] }
295
- .from(hash_including(name: 'Jonny')).to(hash_including(name: 'Jimmy'))
296
- end
297
-
298
- context 'save failed' do
299
- before { User.define_save { false } }
300
-
301
- it { is_expected.to be_persisted }
302
-
303
- specify { expect(subject.save).to eq(false) }
304
- specify { expect { subject.save! }.to raise_error Granite::Form::ObjectNotSaved }
305
-
306
- specify { expect { subject.save }.not_to change { subject.persisted? } }
307
- specify do
308
- expect { muffle(Granite::Form::ObjectNotSaved) { subject.save! } }
309
- .not_to change { subject.persisted? }
310
- end
311
- end
312
- end
313
- end
314
-
315
- describe '#destroy, #destroy!' do
316
- subject { User.create(name: 'Jonny') }
317
-
318
- it { is_expected.to be_valid }
319
- it { is_expected.to be_persisted }
320
- it { is_expected.not_to be_destroyed }
321
-
322
- specify { expect(subject.destroy).to eq(subject) }
323
- specify { expect(subject.destroy!).to eq(subject) }
324
-
325
- specify { expect { subject.destroy }.to change { subject.persisted? }.from(true).to(false) }
326
- specify { expect { subject.destroy! }.to change { subject.persisted? }.from(true).to(false) }
327
-
328
- specify { expect { subject.destroy }.to change { subject.destroyed? }.from(false).to(true) }
329
- specify { expect { subject.destroy! }.to change { subject.destroyed? }.from(false).to(true) }
330
-
331
- specify { expect { subject.destroy }.to change { Storage.storage.keys }.from([subject.id]).to([]) }
332
- specify { expect { subject.destroy! }.to change { Storage.storage.keys }.from([subject.id]).to([]) }
333
-
334
- context 'save failed' do
335
- before { User.define_destroy { false } }
336
-
337
- it { is_expected.to be_persisted }
338
-
339
- specify { expect(subject.destroy).to eq(subject) }
340
- specify { expect { subject.destroy! }.to raise_error Granite::Form::ObjectNotDestroyed }
341
-
342
- specify { expect { subject.destroy }.not_to change { subject.persisted? } }
343
- specify do
344
- expect { muffle(Granite::Form::ObjectNotDestroyed) { subject.destroy! } }
345
- .not_to change { subject.persisted? }
346
- end
347
-
348
- specify { expect { subject.destroy }.not_to change { subject.destroyed? } }
349
- specify do
350
- expect { muffle(Granite::Form::ObjectNotDestroyed) { subject.destroy! } }
351
- .not_to change { subject.destroyed? }
352
- end
353
- end
354
- end
355
- end
356
- end