syrup_form_object 0.0.6 → 0.0.7

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.
@@ -64,7 +64,12 @@ class Syrup::FormObject
64
64
  def initialize(params={})
65
65
  build_relations
66
66
  build(params)
67
- assign_parameters(params)
67
+ self.attributes=params
68
+ end
69
+
70
+ def update_attributes(params)
71
+ self.attributes=params
72
+ self.save
68
73
  end
69
74
 
70
75
  def build_relations
@@ -85,7 +90,7 @@ class Syrup::FormObject
85
90
  end
86
91
  end
87
92
 
88
- def assign_parameters(params)
93
+ def attributes=(params)
89
94
  @params = params
90
95
  params.each do |key, value|
91
96
  self.send "#{key}=", value
@@ -94,6 +99,9 @@ class Syrup::FormObject
94
99
 
95
100
  def build(params); end
96
101
  def after_find(params); end
102
+
103
+ def before_save; end
104
+ def before_create; end
97
105
  def after_create; end
98
106
  def after_save; end
99
107
  def after_commit; end
@@ -111,20 +119,32 @@ class Syrup::FormObject
111
119
  end
112
120
 
113
121
  def save
114
- if self.class.relations.empty?
122
+ if self.class.relations.empty? && !respond_to?(:wrapped)
115
123
  after_save
124
+ true
116
125
  else
117
126
  new_object= !persisted?
127
+ saved = false
118
128
  transaction do
119
- self.class.relations.each do |klass|
129
+ before_save
130
+ if new_object
131
+ before_create
132
+ end
133
+ saved = self.class.relations.all? do |klass|
120
134
  self.send(klass).save
121
135
  end
136
+ if !saved
137
+ raise ActiveRecord::Rollback
138
+ end
122
139
  if new_object
123
140
  after_create
124
141
  end
125
142
  after_save
126
143
  end
127
- after_commit
144
+ if saved
145
+ after_commit
146
+ end
147
+ saved
128
148
  end
129
149
  end
130
150
 
data/lib/syrup/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Syrup
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -11,9 +11,15 @@ class TestItem
11
11
  end
12
12
 
13
13
  def transaction
14
- yield
14
+ begin
15
+ yield
16
+ rescue ActiveRecord::Rollback
17
+ @rollback = true
18
+ end
15
19
  end
16
20
 
21
+ attr_reader :rollback
22
+
17
23
  def persisted?
18
24
  false
19
25
  end
@@ -52,6 +58,11 @@ class TestSubclass < Syrup::FormObject
52
58
  @after_create
53
59
  end
54
60
 
61
+ def has_called_rollback?
62
+ @test_item.rollback
63
+ end
64
+
65
+
55
66
  def after_commit
56
67
  @after_commit = true
57
68
  end
@@ -173,10 +184,35 @@ describe Syrup::FormObject do
173
184
  subject.save
174
185
  end
175
186
  it 'calls after_save' do
187
+ subject.test_item.stub(:save) { true }
188
+
176
189
  subject.save
177
190
 
178
191
  expect(subject).to have_called_after_save
179
192
  end
193
+
194
+
195
+ context 'when the object saves' do
196
+ it 'returns true' do
197
+ subject.test_item.stub(:save) { true }
198
+ expect(subject.save).to be_true
199
+ end
200
+ end
201
+
202
+ context 'when the object does not save' do
203
+ it 'returns false' do
204
+ subject.test_item.stub(:save) {false}
205
+
206
+ expect(subject.save).to be_false
207
+ end
208
+ it 'raises a rollback exception' do
209
+ subject.test_item.stub(:save) {false}
210
+
211
+ subject.save
212
+
213
+ expect(subject).to have_called_rollback
214
+ end
215
+ end
180
216
  end
181
217
 
182
218
  describe '#persisted?' do
@@ -256,6 +292,8 @@ describe Syrup::FormObject do
256
292
  subject.save
257
293
  end
258
294
  it 'calls after_save' do
295
+ subject.test_item.stub(:save) { true }
296
+
259
297
  subject.save
260
298
 
261
299
  expect(subject).to have_called_after_save
@@ -263,6 +301,7 @@ describe Syrup::FormObject do
263
301
  context 'when the object is new' do
264
302
  it 'calls after_create' do
265
303
  subject.wrapped.stub(:persisted?) { false }
304
+ subject.wrapped.stub(:save) { true }
266
305
 
267
306
  subject.save
268
307
 
@@ -270,14 +309,42 @@ describe Syrup::FormObject do
270
309
  end
271
310
  end
272
311
  context 'when the object is not new' do
273
- it 'calls after_update' do
312
+ it 'does not call after create' do
274
313
  subject.wrapped.stub(:persisted?) { true }
314
+ subject.wrapped.stub(:save) { true }
275
315
 
276
316
  subject.save
277
317
 
278
318
  expect(subject).not_to have_called_after_create
279
319
  end
280
320
  end
321
+
322
+ context 'when the object saves' do
323
+ it 'returns true' do
324
+ subject.test_item.stub(:save) { true }
325
+ expect(subject.save).to be_true
326
+ end
327
+ end
328
+
329
+ context 'when the object does not save' do
330
+ it 'returns false' do
331
+ subject.test_item.stub(:save) {false}
332
+
333
+ expect(subject.save).to be_false
334
+ end
335
+ it 'raises a rollback exception' do
336
+ subject.test_item.stub(:save) {false}
337
+
338
+ subject.save
339
+
340
+ expect(subject).to have_called_rollback
341
+ end
342
+ end
343
+ end
344
+
345
+ describe '#update_attributes' do
346
+ it 'updates the attributes in the objects'
347
+ it 'behaves like save'
281
348
  end
282
349
 
283
350
  describe '#persisted?' do
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,10 @@ class String
11
11
  end
12
12
  end
13
13
 
14
+ module ActiveRecord
15
+ class Rollback < StandardError; end
16
+ end
17
+
14
18
  module ActiveModel
15
19
  module Naming; end
16
20
  module Conversion; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syrup_form_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-11 00:00:00.000000000 Z
12
+ date: 2013-11-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus