make_resourceful 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +31 -0
- data/Readme.rdoc +229 -0
- data/VERSION +1 -0
- data/lib/make_resourceful.rb +11 -0
- data/lib/resourceful/base.rb +63 -0
- data/lib/resourceful/builder.rb +405 -0
- data/lib/resourceful/default/accessors.rb +418 -0
- data/lib/resourceful/default/actions.rb +101 -0
- data/lib/resourceful/default/callbacks.rb +51 -0
- data/lib/resourceful/default/responses.rb +118 -0
- data/lib/resourceful/default/urls.rb +136 -0
- data/lib/resourceful/generators/resourceful_scaffold/resourceful_scaffold_generator.rb +87 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/controller.rb +5 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/fixtures.yml +10 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/functional_test.rb +50 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/helper.rb +2 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/migration.rb +13 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/model.rb +2 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/unit_test.rb +7 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view__form.haml +5 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view_edit.haml +11 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view_index.haml +5 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view_new.haml +9 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view_partial.haml +12 -0
- data/lib/resourceful/generators/resourceful_scaffold/templates/view_show.haml +14 -0
- data/lib/resourceful/maker.rb +92 -0
- data/lib/resourceful/response.rb +33 -0
- data/lib/resourceful/serialize.rb +185 -0
- data/spec/accessors_spec.rb +474 -0
- data/spec/actions_spec.rb +310 -0
- data/spec/base_spec.rb +12 -0
- data/spec/builder_spec.rb +332 -0
- data/spec/callbacks_spec.rb +71 -0
- data/spec/integration_spec.rb +394 -0
- data/spec/maker_spec.rb +91 -0
- data/spec/response_spec.rb +37 -0
- data/spec/responses_spec.rb +314 -0
- data/spec/serialize_spec.rb +133 -0
- data/spec/urls_spec.rb +282 -0
- metadata +97 -0
@@ -0,0 +1,310 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Resourceful::Default::Actions, " index action" do
|
4
|
+
include ControllerMocks
|
5
|
+
before :each do
|
6
|
+
mock_controller Resourceful::Default::Actions
|
7
|
+
[:load_objects, :before, :response_for].each(&@controller.method(:stubs))
|
8
|
+
end
|
9
|
+
|
10
|
+
after(:each) { @controller.index }
|
11
|
+
|
12
|
+
it "should load the object collection" do
|
13
|
+
#@controller.expects(:load_objects)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should call the before :index callback" do
|
17
|
+
@controller.expects(:before).with(:index)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should run the response for index" do
|
21
|
+
@controller.expects(:response_for).with(:index)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Resourceful::Default::Actions, " show action" do
|
26
|
+
include ControllerMocks
|
27
|
+
before :each do
|
28
|
+
mock_controller Resourceful::Default::Actions
|
29
|
+
[:load_object, :before, :response_for].each(&@controller.method(:stubs))
|
30
|
+
end
|
31
|
+
|
32
|
+
after(:each) { @controller.show }
|
33
|
+
|
34
|
+
it "should load the instance object" do
|
35
|
+
#@controller.expects(:load_object)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should call the before :show callback" do
|
39
|
+
@controller.expects(:before).with(:show)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should run the response for show" do
|
43
|
+
@controller.expects(:response_for).with(:show)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should run the response for show failing if an exception is raised" do
|
47
|
+
@controller.stubs(:response_for).with(:show).raises("Oh no!")
|
48
|
+
@controller.expects(:response_for).with(:show_fails)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe Resourceful::Default::Actions, " successful create action" do
|
53
|
+
include ControllerMocks
|
54
|
+
before :each do
|
55
|
+
mock_controller Resourceful::Default::Actions
|
56
|
+
[:build_object, :load_object, :before, :after,
|
57
|
+
:save_succeeded!, :response_for].each(&@controller.method(:stubs))
|
58
|
+
@object = stub :save => true
|
59
|
+
@controller.stubs(:current_object).returns(@object)
|
60
|
+
end
|
61
|
+
|
62
|
+
after(:each) { @controller.create }
|
63
|
+
|
64
|
+
it "should build the object from the POSTed parameters" do
|
65
|
+
@controller.expects(:build_object)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should load the instance object" do
|
69
|
+
@controller.expects(:load_object)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should call the before :create callback" do
|
73
|
+
@controller.expects(:before).with(:create)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should try to save the object" do
|
77
|
+
@object.expects(:save).returns(true)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should record the successful save" do
|
81
|
+
@controller.expects(:save_succeeded!)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should call the after :create callback" do
|
85
|
+
@controller.expects(:after).with(:create)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should run the response for create" do
|
89
|
+
@controller.expects(:response_for).with(:create)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe Resourceful::Default::Actions, " unsuccessful create action" do
|
94
|
+
include ControllerMocks
|
95
|
+
before :each do
|
96
|
+
mock_controller Resourceful::Default::Actions
|
97
|
+
[:build_object, :load_object, :before, :after,
|
98
|
+
:save_failed!, :response_for].each(&@controller.method(:stubs))
|
99
|
+
@object = stub :save => false
|
100
|
+
@controller.stubs(:current_object).returns(@object)
|
101
|
+
end
|
102
|
+
|
103
|
+
after(:each) { @controller.create }
|
104
|
+
|
105
|
+
it "should record the unsuccessful save" do
|
106
|
+
@controller.expects(:save_failed!)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should call the after :create_fails callback" do
|
110
|
+
@controller.expects(:after).with(:create_fails)
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should run the response for create failing" do
|
114
|
+
@controller.expects(:response_for).with(:create_fails)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe Resourceful::Default::Actions, " successful update action" do
|
119
|
+
include ControllerMocks
|
120
|
+
before :each do
|
121
|
+
mock_controller Resourceful::Default::Actions
|
122
|
+
[:load_object, :before, :after, :object_parameters,
|
123
|
+
:save_succeeded!, :response_for].each(&@controller.method(:stubs))
|
124
|
+
@object = stub :update_attributes => true
|
125
|
+
@controller.stubs(:current_object).returns(@object)
|
126
|
+
end
|
127
|
+
|
128
|
+
after(:each) { @controller.update }
|
129
|
+
|
130
|
+
it "should load the instance object" do
|
131
|
+
#@controller.expects(:load_object)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should call the before :update callback" do
|
135
|
+
@controller.expects(:before).with(:update)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should try to update the object with the POSTed attributes" do
|
139
|
+
@controller.expects(:object_parameters).returns(:params => "stuff")
|
140
|
+
@object.expects(:update_attributes).with(:params => "stuff").returns(true)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should record the successful save" do
|
144
|
+
@controller.expects(:save_succeeded!)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should call the after :update callback" do
|
148
|
+
@controller.expects(:after).with(:update)
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should run the response for update" do
|
152
|
+
@controller.expects(:response_for).with(:update)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe Resourceful::Default::Actions, " unsuccessful update action" do
|
157
|
+
include ControllerMocks
|
158
|
+
before :each do
|
159
|
+
mock_controller Resourceful::Default::Actions
|
160
|
+
[:load_object, :before, :after, :object_parameters,
|
161
|
+
:save_failed!, :response_for].each(&@controller.method(:stubs))
|
162
|
+
@object = stub :update_attributes => false
|
163
|
+
@controller.stubs(:current_object).returns(@object)
|
164
|
+
end
|
165
|
+
|
166
|
+
after(:each) { @controller.update }
|
167
|
+
|
168
|
+
it "should record the unsuccessful save" do
|
169
|
+
@controller.expects(:save_failed!)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should call the after :update_fails callback" do
|
173
|
+
@controller.expects(:after).with(:update_fails)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should run the response for update failing" do
|
177
|
+
@controller.expects(:response_for).with(:update_fails)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
describe Resourceful::Default::Actions, " unsuccessful update action because of StaleObjectError" do
|
182
|
+
include ControllerMocks
|
183
|
+
before :each do
|
184
|
+
mock_controller Resourceful::Default::Actions
|
185
|
+
[:load_object, :before, :after, :object_parameters,
|
186
|
+
:save_failed!, :response_for].each(&@controller.method(:stubs))
|
187
|
+
@object = stub_model("Thing")
|
188
|
+
@object.stubs(:update_attributes).raises(ActiveRecord::StaleObjectError)
|
189
|
+
@object.expects(:reload)
|
190
|
+
@controller.stubs(:current_object).returns(@object)
|
191
|
+
end
|
192
|
+
|
193
|
+
after(:each) { @controller.update }
|
194
|
+
|
195
|
+
it "should record the unsuccessful save" do
|
196
|
+
@controller.expects(:save_failed!)
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should call the after :update_fails callback" do
|
200
|
+
@controller.expects(:after).with(:update_fails)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should run the response for update failing" do
|
204
|
+
@controller.expects(:response_for).with(:update_fails)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
describe Resourceful::Default::Actions, " new action" do
|
209
|
+
include ControllerMocks
|
210
|
+
before :each do
|
211
|
+
mock_controller Resourceful::Default::Actions
|
212
|
+
[:build_object, :load_object,
|
213
|
+
:before, :response_for].each(&@controller.method(:stubs))
|
214
|
+
end
|
215
|
+
|
216
|
+
after(:each) { @controller.new }
|
217
|
+
|
218
|
+
it "should build the object from the POSTed parameters" do
|
219
|
+
@controller.expects(:build_object)
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should load the instance object" do
|
223
|
+
@controller.expects(:load_object)
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should call the before :new callback" do
|
227
|
+
@controller.expects(:before).with(:new)
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should run the response for new" do
|
231
|
+
@controller.expects(:response_for).with(:new)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
describe Resourceful::Default::Actions, " edit action" do
|
236
|
+
include ControllerMocks
|
237
|
+
before :each do
|
238
|
+
mock_controller Resourceful::Default::Actions
|
239
|
+
[:load_object, :before, :response_for].each(&@controller.method(:stubs))
|
240
|
+
end
|
241
|
+
|
242
|
+
after(:each) { @controller.edit }
|
243
|
+
|
244
|
+
it "should load the instance object" do
|
245
|
+
#@controller.expects(:load_object)
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should call the before :edit callback" do
|
249
|
+
@controller.expects(:before).with(:edit)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should run the response for edit" do
|
253
|
+
@controller.expects(:response_for).with(:edit)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
describe Resourceful::Default::Actions, " successful destroy action" do
|
258
|
+
include ControllerMocks
|
259
|
+
before :each do
|
260
|
+
mock_controller Resourceful::Default::Actions
|
261
|
+
[:load_object, :before,
|
262
|
+
:after, :response_for].each(&@controller.method(:stubs))
|
263
|
+
@object = stub :destroy => true
|
264
|
+
@controller.stubs(:current_object).returns(@object)
|
265
|
+
end
|
266
|
+
|
267
|
+
after(:each) { @controller.destroy }
|
268
|
+
|
269
|
+
it "should load the instance object" do
|
270
|
+
#@controller.expects(:load_object)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should call the before :destroy callback" do
|
274
|
+
@controller.expects(:before).with(:destroy)
|
275
|
+
end
|
276
|
+
|
277
|
+
it "should try to destroy the object" do
|
278
|
+
@object.expects(:destroy).returns(true)
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should call the after :destroy callback" do
|
282
|
+
@controller.expects(:after).with(:destroy)
|
283
|
+
end
|
284
|
+
|
285
|
+
it "should run the response for destroy" do
|
286
|
+
@controller.expects(:response_for).with(:destroy)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe Resourceful::Default::Actions, " unsuccessful destroy action" do
|
291
|
+
include ControllerMocks
|
292
|
+
before :each do
|
293
|
+
mock_controller Resourceful::Default::Actions
|
294
|
+
[:load_object, :before,
|
295
|
+
:after, :response_for].each(&@controller.method(:stubs))
|
296
|
+
@object = stub :destroy => false
|
297
|
+
@controller.stubs(:current_object).returns(@object)
|
298
|
+
end
|
299
|
+
|
300
|
+
after(:each) { @controller.destroy }
|
301
|
+
|
302
|
+
it "should call the after :destroy_fails callback" do
|
303
|
+
@controller.expects(:after).with(:destroy_fails)
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should run the response for destroy failing" do
|
307
|
+
@controller.expects(:response_for).with(:destroy_fails)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Resourceful::Base, ".made_resourceful" do
|
4
|
+
before(:all) { @original_blocks = Resourceful::Base.made_resourceful.dup }
|
5
|
+
before(:each) { Resourceful::Base.made_resourceful.replace [] }
|
6
|
+
after(:all) { Resourceful::Base.made_resourceful.replace @original_blocks }
|
7
|
+
|
8
|
+
it "should store blocks when called with blocks and return them when called without a block" do
|
9
|
+
5.times { Resourceful::Base.made_resourceful(&should_be_called) }
|
10
|
+
Resourceful::Base.made_resourceful.each(&:call)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,332 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Resourceful::Builder, " applied without any modification" do
|
4
|
+
include ControllerMocks
|
5
|
+
before :each do
|
6
|
+
mock_kontroller
|
7
|
+
create_builder
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should remove all resourceful actions" do
|
11
|
+
@kontroller.expects(:send).with do |name, action_module|
|
12
|
+
name == :include && (action_module.instance_methods & Resourceful::ACTIONS.map(&:to_s)).empty?
|
13
|
+
end
|
14
|
+
@builder.apply
|
15
|
+
end
|
16
|
+
|
17
|
+
it "shouldn't un-hide any actions" do
|
18
|
+
@builder.apply
|
19
|
+
@kontroller.hidden_actions.should == Resourceful::ACTIONS
|
20
|
+
end
|
21
|
+
|
22
|
+
it "shouldn't set any callbacks" do
|
23
|
+
@builder.apply
|
24
|
+
callbacks.should == {:before => {}, :after => {}}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "shouldn't set any responses" do
|
28
|
+
@builder.apply
|
29
|
+
responses.should be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it "shouldn't set any parents" do
|
33
|
+
@builder.apply
|
34
|
+
parents.should be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should set the controller as made_resourceful" do
|
38
|
+
@builder.apply
|
39
|
+
@kontroller.made_resourceful.should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should set load_parent_object as a before_filter for no actions" do
|
43
|
+
@kontroller.expects(:before_filter).with(:load_parent_object, :only => [])
|
44
|
+
@builder.apply
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe Resourceful::Builder, " with some actions set" do
|
49
|
+
include ControllerMocks
|
50
|
+
before :each do
|
51
|
+
mock_kontroller
|
52
|
+
create_builder
|
53
|
+
@actions = [:show, :index, :new, :create]
|
54
|
+
@builder.actions *@actions
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should include the given actions" do
|
58
|
+
@kontroller.expects(:send).with do |name, action_module|
|
59
|
+
name == :include && (action_module.instance_methods & Resourceful::ACTIONS.map(&:to_s)).sort ==
|
60
|
+
@actions.map(&:to_s).sort
|
61
|
+
end
|
62
|
+
@builder.apply
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should un-hide the given actions" do
|
66
|
+
@builder.apply
|
67
|
+
(@kontroller.hidden_actions & @actions).should be_empty
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should set load_parent_object as a before_filter for the given actions" do
|
71
|
+
@kontroller.expects(:before_filter).with(:load_parent_object, :only => [:show, :index, :new, :create])
|
72
|
+
@builder.apply
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe Resourceful::Builder, " with all actions set for a plural controller" do
|
77
|
+
include ControllerMocks
|
78
|
+
before :each do
|
79
|
+
mock_kontroller
|
80
|
+
@kontroller.class_eval { def plural?; true; end }
|
81
|
+
create_builder
|
82
|
+
@builder.actions :all
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should include all actions" do
|
86
|
+
@kontroller.expects(:send).with do |name, action_module|
|
87
|
+
name == :include && (action_module.instance_methods & Resourceful::ACTIONS.map(&:to_s)).sort ==
|
88
|
+
Resourceful::ACTIONS.map(&:to_s).sort
|
89
|
+
end
|
90
|
+
@builder.apply
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe Resourceful::Builder, " with all actions set for a singular controller" do
|
95
|
+
include ControllerMocks
|
96
|
+
before :each do
|
97
|
+
mock_kontroller
|
98
|
+
@kontroller.class_eval { def plural?; false; end }
|
99
|
+
create_builder
|
100
|
+
@builder.actions :all
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should include all singular actions" do
|
104
|
+
@kontroller.expects(:send).with do |name, action_module|
|
105
|
+
name == :include && (action_module.instance_methods & Resourceful::ACTIONS.map(&:to_s)).sort ==
|
106
|
+
Resourceful::SINGULAR_ACTIONS.map(&:to_s).sort
|
107
|
+
end
|
108
|
+
@builder.apply
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe Resourceful::Builder, " with several before and after callbacks set" do
|
113
|
+
include ControllerMocks
|
114
|
+
before :each do
|
115
|
+
mock_kontroller
|
116
|
+
create_builder
|
117
|
+
@builder.before(:create, :update, 'destroy', &(should_be_called { times(3) }))
|
118
|
+
@builder.after('index', &should_be_called)
|
119
|
+
@builder.after(:update, &should_be_called)
|
120
|
+
@builder.apply
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should save the callbacks as the :resourceful_callbacks inheritable_attribute" do
|
124
|
+
callbacks[:before][:create].each(&:call)
|
125
|
+
callbacks[:before][:update].each(&:call)
|
126
|
+
callbacks[:before][:destroy].each(&:call)
|
127
|
+
callbacks[:after][:index].each(&:call)
|
128
|
+
callbacks[:after][:update].each(&:call)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe Resourceful::Builder, " with chained before and after callbacks" do
|
133
|
+
include ControllerMocks
|
134
|
+
before :each do
|
135
|
+
mock_kontroller
|
136
|
+
create_builder
|
137
|
+
@before_value = ''
|
138
|
+
@builder.before(:index, &lambda { @before_value += 'A' })
|
139
|
+
@builder.before(:index, &lambda { @before_value += 'B' })
|
140
|
+
|
141
|
+
@after_value = ''
|
142
|
+
@builder.after(:index, &lambda { @after_value += 'A' })
|
143
|
+
@builder.after(:index, &lambda { @after_value += 'B' })
|
144
|
+
@builder.apply
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should save as array in the :resourceful_callbacks inheritable_attribute and execute in order" do
|
148
|
+
callbacks[:before][:index].each { |callback| callback.call }
|
149
|
+
@before_value.should == 'AB'
|
150
|
+
callbacks[:after][:index].each { |callback| callback.call }
|
151
|
+
@after_value.should == 'AB'
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe Resourceful::Builder, " with responses set for several formats" do
|
156
|
+
include ControllerMocks
|
157
|
+
before :each do
|
158
|
+
mock_kontroller
|
159
|
+
create_builder
|
160
|
+
@builder.response_for('create') do |f|
|
161
|
+
f.html(&should_be_called)
|
162
|
+
f.js(&should_be_called)
|
163
|
+
f.yaml(&should_be_called)
|
164
|
+
f.xml(&should_be_called)
|
165
|
+
f.txt(&should_be_called)
|
166
|
+
end
|
167
|
+
@builder.response_for(:remove_failed, 'update') do |f|
|
168
|
+
f.yaml(&(should_be_called { times(2) }))
|
169
|
+
f.png(&(should_be_called { times(2) }))
|
170
|
+
end
|
171
|
+
@builder.apply
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should save the responses as the :resourceful_responses inheritable_attribute" do
|
175
|
+
responses[:create].map(&:first).should == [:html, :js, :yaml, :xml, :txt]
|
176
|
+
responses[:create].map(&:last).each(&:call)
|
177
|
+
|
178
|
+
responses[:remove_failed].map(&:first).should == [:yaml, :png]
|
179
|
+
responses[:remove_failed].map(&:last).each(&:call)
|
180
|
+
|
181
|
+
responses[:update].map(&:first).should == [:yaml, :png]
|
182
|
+
responses[:update].map(&:last).each(&:call)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe Resourceful::Builder, " with a response set for the default format" do
|
187
|
+
include ControllerMocks
|
188
|
+
before :each do
|
189
|
+
mock_kontroller
|
190
|
+
create_builder
|
191
|
+
@builder.response_for('index', &should_be_called)
|
192
|
+
@builder.apply
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should save the response as a response for HTML in the :resourceful_responses inheritable_attribute" do
|
196
|
+
responses[:index].map(&:first).should == [:html]
|
197
|
+
responses[:index].map(&:last).each(&:call)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe Resourceful::Builder, " with a response set for no actions" do
|
202
|
+
include ControllerMocks
|
203
|
+
before :each do
|
204
|
+
mock_kontroller
|
205
|
+
create_builder
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should raise an error" do
|
209
|
+
lambda { @builder.response_for {} }.should raise_error("Must specify one or more actions for response_for.")
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe Resourceful::Builder, " publishing without an attributes hash" do
|
214
|
+
include ControllerMocks
|
215
|
+
before :each do
|
216
|
+
mock_kontroller
|
217
|
+
create_builder
|
218
|
+
end
|
219
|
+
|
220
|
+
it "should raise an error" do
|
221
|
+
proc { @builder.publish :xml, :yaml }.should raise_error("Must specify :attributes option")
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
describe Resourceful::Builder, " publishing several formats" do
|
226
|
+
include ControllerMocks
|
227
|
+
before :each do
|
228
|
+
mock_kontroller
|
229
|
+
create_builder
|
230
|
+
|
231
|
+
@model = stub_model("Thing")
|
232
|
+
@kontroller.stubs(:current_object).returns(@model)
|
233
|
+
|
234
|
+
@models = (1..5).map { stub_model("Thing") }
|
235
|
+
@kontroller.stubs(:current_objects).returns(@models)
|
236
|
+
|
237
|
+
@builder.publish :yaml, :json, 'xml', :additional => 'option', :attributes => [:name, :stuff]
|
238
|
+
@builder.apply
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should add a list of types as responses for index and show" do
|
242
|
+
responses[:index].map(&:first).should == [:yaml, :json, :xml]
|
243
|
+
responses[:show].map(&:first).should == [:yaml, :json, :xml]
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should respond by rendering the serialized model with the proper type, passing along un-recognized options" do
|
247
|
+
@model.expects(:serialize).with(:yaml, :additional => 'option', :attributes => [:name, :stuff]).returns('serialized')
|
248
|
+
@kontroller.expects(:render).with(:text => 'serialized')
|
249
|
+
@kontroller.instance_eval(&responses[:index].find { |type, _| type == :yaml }[1])
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should respond render XML and JSON with the proper action" do
|
253
|
+
@model.expects(:serialize).with(:xml, :additional => 'option', :attributes => [:name, :stuff]).returns('XML serialized')
|
254
|
+
@model.expects(:serialize).with(:json, :additional => 'option', :attributes => [:name, :stuff]).returns('JSON serialized')
|
255
|
+
@kontroller.expects(:render).with(:xml => 'XML serialized')
|
256
|
+
@kontroller.expects(:render).with(:json => 'JSON serialized')
|
257
|
+
|
258
|
+
@kontroller.instance_eval(&responses[:index].find { |type, _| type == :xml }[1])
|
259
|
+
@kontroller.instance_eval(&responses[:index].find { |type, _| type == :json }[1])
|
260
|
+
end
|
261
|
+
|
262
|
+
it "should render current_objects if the action is plural" do
|
263
|
+
@kontroller.stubs(:plural_action?).returns(true)
|
264
|
+
@models.expects(:serialize).with(:yaml, :additional => 'option', :attributes => [:name, :stuff]).returns('serialized')
|
265
|
+
@kontroller.expects(:render).with(:text => 'serialized')
|
266
|
+
@kontroller.instance_eval(&responses[:index].find { |type, _| type == :yaml }[1])
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe Resourceful::Builder, " publishing only to #show" do
|
271
|
+
include ControllerMocks
|
272
|
+
before :each do
|
273
|
+
mock_kontroller
|
274
|
+
create_builder
|
275
|
+
|
276
|
+
@model = stub_model("Thing")
|
277
|
+
@kontroller.stubs(:current_object).returns(@model)
|
278
|
+
|
279
|
+
@builder.publish :json, :yaml, :only => :show, :attributes => [:name, :stuff]
|
280
|
+
@builder.apply
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should add responses for show" do
|
284
|
+
responses[:show].map(&:first).should == [:json, :yaml]
|
285
|
+
end
|
286
|
+
|
287
|
+
it "shouldn't add responses for index" do
|
288
|
+
responses[:index].should be_nil
|
289
|
+
end
|
290
|
+
|
291
|
+
it "shouldn't pass the :only option to the serialize call" do
|
292
|
+
@model.expects(:serialize).with(:yaml, :attributes => [:name, :stuff])
|
293
|
+
@kontroller.stubs(:render)
|
294
|
+
@kontroller.instance_eval(&responses[:show].find { |type, _| type == :yaml }[1])
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
describe Resourceful::Builder, " publishing in addition to other responses" do
|
299
|
+
include ControllerMocks
|
300
|
+
before :each do
|
301
|
+
mock_kontroller
|
302
|
+
create_builder
|
303
|
+
|
304
|
+
@builder.response_for(:index) {}
|
305
|
+
@builder.publish :json, :yaml, :attributes => [:name, :stuff]
|
306
|
+
@builder.response_for :show do |f|
|
307
|
+
f.html {}
|
308
|
+
f.js {}
|
309
|
+
end
|
310
|
+
@builder.apply
|
311
|
+
end
|
312
|
+
|
313
|
+
it "should add published responses in addition to pre-existing ones" do
|
314
|
+
responses[:show].map(&:first).should == [:html, :js, :json, :yaml]
|
315
|
+
responses[:index].map(&:first).should == [:html, :json, :yaml]
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
describe Resourceful::Builder, " belonging to several parents" do
|
320
|
+
include ControllerMocks
|
321
|
+
before :each do
|
322
|
+
mock_kontroller
|
323
|
+
create_builder
|
324
|
+
|
325
|
+
@builder.belongs_to :post, :blat, :stang
|
326
|
+
@builder.apply
|
327
|
+
end
|
328
|
+
|
329
|
+
it "should save the parents as the :parents inheritable_attribute" do
|
330
|
+
parents.should == ['post', 'blat', 'stang']
|
331
|
+
end
|
332
|
+
end
|