syrup_form_object 0.0.5 → 0.0.6

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.
@@ -37,14 +37,11 @@ class Syrup::FormObject
37
37
  attr_accessor klass
38
38
  end
39
39
 
40
- attr_accessor :wrapped_class
41
40
 
42
41
  def wraps(klass)
43
- wrapped_class = klass
44
42
  has_one(klass)
45
43
  alias_method :wrapped, klass
46
44
  alias_method :wrapped=, "#{klass}="
47
-
48
45
  end
49
46
 
50
47
  def find(params)
@@ -97,9 +94,38 @@ class Syrup::FormObject
97
94
 
98
95
  def build(params); end
99
96
  def after_find(params); end
97
+ def after_create; end
98
+ def after_save; end
99
+ def after_commit; end
100
100
 
101
101
  def persisted?
102
- false
102
+ respond_to?(:wrapped) && wrapped.persisted?
103
+ end
104
+
105
+ def transaction(&block)
106
+ first_related_object.transaction(&block)
107
+ end
108
+
109
+ def first_related_object
110
+ respond_to?(:wrapped) ? wrapped : self.send(self.class.relations.first)
111
+ end
112
+
113
+ def save
114
+ if self.class.relations.empty?
115
+ after_save
116
+ else
117
+ new_object= !persisted?
118
+ transaction do
119
+ self.class.relations.each do |klass|
120
+ self.send(klass).save
121
+ end
122
+ if new_object
123
+ after_create
124
+ end
125
+ after_save
126
+ end
127
+ after_commit
128
+ end
103
129
  end
104
130
 
105
131
  end
data/lib/syrup/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Syrup
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -6,6 +6,17 @@ class TestItem
6
6
 
7
7
  def self.find(id)
8
8
  end
9
+
10
+ def save
11
+ end
12
+
13
+ def transaction
14
+ yield
15
+ end
16
+
17
+ def persisted?
18
+ false
19
+ end
9
20
  end
10
21
 
11
22
  class TestSubclass < Syrup::FormObject
@@ -24,6 +35,31 @@ class TestSubclass < Syrup::FormObject
24
35
  def has_called_after_find?
25
36
  @after_find
26
37
  end
38
+
39
+ def after_save
40
+ @after_save = true
41
+ end
42
+
43
+ def has_called_after_save?
44
+ @after_save
45
+ end
46
+
47
+ def after_create
48
+ @after_create = true
49
+ end
50
+
51
+ def has_called_after_create?
52
+ @after_create
53
+ end
54
+
55
+ def after_commit
56
+ @after_commit = true
57
+ end
58
+
59
+ def has_called_after_commit?
60
+ @after_commit
61
+ end
62
+
27
63
  end
28
64
 
29
65
  shared_examples 'successful create' do
@@ -42,6 +78,7 @@ describe Syrup::FormObject do
42
78
  attribute :test_value, String
43
79
  end
44
80
  }
81
+
45
82
  describe '#new' do
46
83
  subject { test_subclass.new(params) }
47
84
  let(:params) { {} }
@@ -56,7 +93,7 @@ describe Syrup::FormObject do
56
93
  end
57
94
  end
58
95
  end
59
- pending '#save'
96
+
60
97
  describe '::find' do
61
98
  subject { test_subclass.find(params) }
62
99
  let(:params) { {} }
@@ -65,6 +102,24 @@ describe Syrup::FormObject do
65
102
  end
66
103
  end
67
104
 
105
+ describe '#save' do
106
+ let(:params) { {} }
107
+ subject { test_subclass.new(params) }
108
+ it 'calls after_save' do
109
+ subject.save
110
+
111
+ expect(subject).to have_called_after_save
112
+ end
113
+ end
114
+
115
+ describe '#persisted?' do
116
+ let(:params) { {} }
117
+ subject { test_subclass.new(params) }
118
+ it 'returns false' do
119
+ expect(subject.persisted?).to be_false
120
+ end
121
+ end
122
+
68
123
  end
69
124
  context 'when using nested attributes' do
70
125
  let(:test_subclass) {
@@ -94,7 +149,6 @@ describe Syrup::FormObject do
94
149
  end
95
150
  end
96
151
 
97
- pending '#save'
98
152
  describe '::find' do
99
153
  subject { test_subclass.find(params) }
100
154
  let(:params) { {test_item: 2} }
@@ -109,6 +163,30 @@ describe Syrup::FormObject do
109
163
  end
110
164
  end
111
165
 
166
+ describe '#save' do
167
+ let(:params) {{ test_item_attributes: {
168
+ test_item_value: '2'}}}
169
+ subject { test_subclass.new(params) }
170
+ it 'saves the test_item' do
171
+ subject.test_item.should_receive(:save)
172
+
173
+ subject.save
174
+ end
175
+ it 'calls after_save' do
176
+ subject.save
177
+
178
+ expect(subject).to have_called_after_save
179
+ end
180
+ end
181
+
182
+ describe '#persisted?' do
183
+ let(:params) {{ test_item_attributes: {
184
+ test_item_value: '2'}}}
185
+ subject { test_subclass.new(params) }
186
+ it 'returns false' do
187
+ expect(subject.persisted?).to be_false
188
+ end
189
+ end
112
190
  end
113
191
 
114
192
  context 'when using a wrapped object' do
@@ -153,19 +231,64 @@ describe Syrup::FormObject do
153
231
  end
154
232
  end
155
233
 
156
- pending '#save'
157
234
  describe '::find' do
158
235
  subject { test_subclass.find(params) }
159
236
  let(:params) { 2 }
237
+ let(:test_item) { TestItem.new }
160
238
 
161
239
  it 'loads the test_item from the id sent' do
240
+ TestItem.should_receive(:find).with(params) { test_item }
241
+
242
+ expect(subject.wrapped).to be test_item
162
243
  end
163
244
  it 'calls the after_find method' do
164
245
  expect(subject).to have_called_after_find
165
246
  end
166
247
  end
167
248
 
168
- end
249
+ describe '#save' do
250
+ let(:params) {{ test_item_value: '2' }}
251
+
252
+ subject { test_subclass.new(params) }
253
+ it 'saves the test_item' do
254
+ subject.test_item.should_receive(:save)
255
+
256
+ subject.save
257
+ end
258
+ it 'calls after_save' do
259
+ subject.save
169
260
 
261
+ expect(subject).to have_called_after_save
262
+ end
263
+ context 'when the object is new' do
264
+ it 'calls after_create' do
265
+ subject.wrapped.stub(:persisted?) { false }
266
+
267
+ subject.save
268
+
269
+ expect(subject).to have_called_after_create
270
+ end
271
+ end
272
+ context 'when the object is not new' do
273
+ it 'calls after_update' do
274
+ subject.wrapped.stub(:persisted?) { true }
275
+
276
+ subject.save
277
+
278
+ expect(subject).not_to have_called_after_create
279
+ end
280
+ end
281
+ end
282
+
283
+ describe '#persisted?' do
284
+ let(:params) {{ test_item_value: '2' }}
285
+ subject { test_subclass.find(params) }
286
+ it 'forwards the message to the wrapped object' do
287
+ subject.wrapped.stub(:persisted?) {:a_value}
288
+
289
+ expect(subject.persisted?).to be :a_value
290
+ end
291
+ end
292
+ end
170
293
 
171
294
  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.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: