JackDanger-inherited_resources 0.9.1

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 (37) hide show
  1. data/CHANGELOG +101 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +523 -0
  4. data/Rakefile +37 -0
  5. data/VERSION +1 -0
  6. data/lib/inherited_resources.rb +23 -0
  7. data/lib/inherited_resources/actions.rb +79 -0
  8. data/lib/inherited_resources/base.rb +42 -0
  9. data/lib/inherited_resources/base_helpers.rb +363 -0
  10. data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  11. data/lib/inherited_resources/class_methods.rb +337 -0
  12. data/lib/inherited_resources/dsl.rb +26 -0
  13. data/lib/inherited_resources/dumb_responder.rb +20 -0
  14. data/lib/inherited_resources/has_scope_helpers.rb +83 -0
  15. data/lib/inherited_resources/legacy/respond_to.rb +156 -0
  16. data/lib/inherited_resources/legacy/responder.rb +200 -0
  17. data/lib/inherited_resources/polymorphic_helpers.rb +155 -0
  18. data/lib/inherited_resources/singleton_helpers.rb +95 -0
  19. data/lib/inherited_resources/url_helpers.rb +179 -0
  20. data/test/aliases_test.rb +139 -0
  21. data/test/association_chain_test.rb +125 -0
  22. data/test/base_test.rb +225 -0
  23. data/test/belongs_to_test.rb +87 -0
  24. data/test/class_methods_test.rb +138 -0
  25. data/test/customized_belongs_to_test.rb +76 -0
  26. data/test/defaults_test.rb +70 -0
  27. data/test/flash_test.rb +88 -0
  28. data/test/has_scope_test.rb +139 -0
  29. data/test/nested_belongs_to_test.rb +108 -0
  30. data/test/optional_belongs_to_test.rb +164 -0
  31. data/test/polymorphic_test.rb +186 -0
  32. data/test/redirect_to_test.rb +51 -0
  33. data/test/respond_to_test.rb +155 -0
  34. data/test/singleton_test.rb +83 -0
  35. data/test/test_helper.rb +31 -0
  36. data/test/url_helpers_test.rb +537 -0
  37. metadata +88 -0
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ # This test file is instead to test the how controller flow and actions
4
+ # using a belongs_to association. This is done using mocks a la rspec.
5
+ #
6
+ class Store
7
+ end
8
+
9
+ class Manager
10
+ def self.human_name; 'Manager'; end
11
+ end
12
+
13
+ class ManagersController < InheritedResources::Base
14
+ belongs_to :store, :singleton => true
15
+ end
16
+
17
+ class SingletonTest < ActionController::TestCase
18
+ tests ManagersController
19
+
20
+ def setup
21
+ @controller.stubs(:resource_url).returns('/')
22
+ @controller.stubs(:collection_url).returns('/')
23
+ end
24
+
25
+ def test_expose_the_resquested_manager_on_show
26
+ Store.expects(:find).with('37').returns(mock_store)
27
+ mock_store.expects(:manager).returns(mock_manager)
28
+ get :show, :store_id => '37'
29
+ assert_equal mock_store, assigns(:store)
30
+ assert_equal mock_manager, assigns(:manager)
31
+ end
32
+
33
+ def test_expose_a_new_manager_on_new
34
+ Store.expects(:find).with('37').returns(mock_store)
35
+ mock_store.expects(:build_manager).returns(mock_manager)
36
+ get :new, :store_id => '37'
37
+ assert_equal mock_store, assigns(:store)
38
+ assert_equal mock_manager, assigns(:manager)
39
+ end
40
+
41
+ def test_expose_the_resquested_manager_on_edit
42
+ Store.expects(:find).with('37').returns(mock_store)
43
+ mock_store.expects(:manager).returns(mock_manager)
44
+ get :edit, :store_id => '37'
45
+ assert_equal mock_store, assigns(:store)
46
+ assert_equal mock_manager, assigns(:manager)
47
+ assert_response :success
48
+ end
49
+
50
+ def test_expose_a_newly_create_manager_on_create
51
+ Store.expects(:find).with('37').returns(mock_store)
52
+ mock_store.expects(:build_manager).with({'these' => 'params'}).returns(mock_manager(:save => true))
53
+ post :create, :store_id => '37', :manager => {:these => 'params'}
54
+ assert_equal mock_store, assigns(:store)
55
+ assert_equal mock_manager, assigns(:manager)
56
+ end
57
+
58
+ def test_update_the_requested_object_on_update
59
+ Store.expects(:find).with('37').returns(mock_store(:manager => mock_manager))
60
+ mock_manager.expects(:update_attributes).with({'these' => 'params'}).returns(true)
61
+ put :update, :store_id => '37', :manager => {:these => 'params'}
62
+ assert_equal mock_store, assigns(:store)
63
+ assert_equal mock_manager, assigns(:manager)
64
+ end
65
+
66
+ def test_the_resquested_manager_is_destroyed_on_destroy
67
+ Store.expects(:find).with('37').returns(mock_store)
68
+ mock_store.expects(:manager).returns(mock_manager)
69
+ mock_manager.expects(:destroy)
70
+ delete :destroy, :store_id => '37'
71
+ assert_equal mock_store, assigns(:store)
72
+ assert_equal mock_manager, assigns(:manager)
73
+ end
74
+
75
+ protected
76
+ def mock_store(stubs={})
77
+ @mock_store ||= mock(stubs)
78
+ end
79
+
80
+ def mock_manager(stubs={})
81
+ @mock_manager ||= mock(stubs)
82
+ end
83
+ end
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ gem "test-unit"
3
+ require 'test/unit'
4
+ begin
5
+ require 'ruby-debug'
6
+ rescue LoadError
7
+ end
8
+ require 'mocha'
9
+
10
+ ENV["RAILS_ENV"] = "test"
11
+ RAILS_ROOT = "anywhere"
12
+
13
+ require 'active_support'
14
+ require 'action_controller'
15
+ require 'action_controller/test_case'
16
+ require 'action_controller/test_process'
17
+
18
+ I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
19
+ I18n.reload!
20
+
21
+ class ApplicationController < ActionController::Base; end
22
+
23
+ # Add IR to load path and load the main file
24
+ ActiveSupport::Dependencies.load_paths << File.expand_path(File.dirname(__FILE__) + '/../lib')
25
+ require_dependency 'inherited_resources'
26
+
27
+ ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
28
+
29
+ ActionController::Routing::Routes.draw do |map|
30
+ map.connect ':controller/:action/:id'
31
+ end
@@ -0,0 +1,537 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Universe; end
4
+ class UniversesController < InheritedResources::Base
5
+ defaults :singleton => true, :route_instance_name => 'universum'
6
+ end
7
+
8
+ class House; end
9
+ class HousesController < InheritedResources::Base
10
+ end
11
+
12
+ class Backpack; end
13
+ module Admin; end
14
+ class Admin::BackpacksController < InheritedResources::Base
15
+ defaults :route_collection_name => 'tour_backpacks'
16
+ end
17
+
18
+ class Table; end
19
+ class TablesController < InheritedResources::Base
20
+ belongs_to :house
21
+ end
22
+
23
+ class RoomsController < InheritedResources::Base
24
+ belongs_to :house, :route_name => 'big_house'
25
+ end
26
+
27
+ class ChairsController < InheritedResources::Base
28
+ belongs_to :house do
29
+ belongs_to :table
30
+ end
31
+ end
32
+
33
+ class OwnersController < InheritedResources::Base
34
+ singleton_belongs_to :house
35
+ end
36
+
37
+ class Bed; end
38
+ class BedsController < InheritedResources::Base
39
+ optional_belongs_to :house, :building
40
+ end
41
+
42
+ class Desk; end
43
+ module Admin
44
+ class DesksController < InheritedResources::Base
45
+ optional_belongs_to :house
46
+ end
47
+ end
48
+
49
+ class Dish; end
50
+ class DishesController < InheritedResources::Base
51
+ belongs_to :house do
52
+ polymorphic_belongs_to :table, :kitchen
53
+ end
54
+ end
55
+
56
+ class Center; end
57
+ class CentersController < InheritedResources::Base
58
+ acts_as_singleton!
59
+
60
+ belongs_to :house do
61
+ belongs_to :table, :kitchen, :polymorphic => true
62
+ end
63
+ end
64
+
65
+ # Create a TestHelper module with some helpers
66
+ class UrlHelpersTest < ActiveSupport::TestCase
67
+
68
+ def test_url_helpers_on_simple_inherited_resource
69
+ controller = HousesController.new
70
+ controller.instance_variable_set('@house', :house)
71
+
72
+ [:url, :path].each do |path_or_url|
73
+ controller.expects("houses_#{path_or_url}").with({}).once
74
+ controller.send("collection_#{path_or_url}")
75
+
76
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
77
+ controller.send("resource_#{path_or_url}")
78
+
79
+ controller.expects("new_house_#{path_or_url}").with({}).once
80
+ controller.send("new_resource_#{path_or_url}")
81
+
82
+ controller.expects("edit_house_#{path_or_url}").with(:house, {}).once
83
+ controller.send("edit_resource_#{path_or_url}")
84
+
85
+ controller.expects("root_#{path_or_url}").with({}).once
86
+ controller.send("parent_#{path_or_url}")
87
+
88
+ # With arg
89
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
90
+ controller.send("resource_#{path_or_url}", :arg)
91
+
92
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
93
+ controller.send("resource_#{path_or_url}", :arg)
94
+
95
+ # With options
96
+ controller.expects("house_#{path_or_url}").with(:arg, :page => 1).once
97
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
98
+ end
99
+ end
100
+
101
+ def test_url_helpers_on_simple_inherited_namespaced_resource
102
+ controller = Admin::BackpacksController.new
103
+ controller.instance_variable_set('@backpack', :backpack)
104
+
105
+ assert_equal 'admin', controller.class.resources_configuration[:self][:route_prefix]
106
+
107
+ [:url, :path].each do |path_or_url|
108
+ controller.expects("admin_tour_backpacks_#{path_or_url}").with({}).once
109
+ controller.send("collection_#{path_or_url}")
110
+
111
+ controller.expects("admin_backpack_#{path_or_url}").with(:backpack, {}).once
112
+ controller.send("resource_#{path_or_url}")
113
+
114
+ controller.expects("new_admin_backpack_#{path_or_url}").with({}).once
115
+ controller.send("new_resource_#{path_or_url}")
116
+
117
+ controller.expects("edit_admin_backpack_#{path_or_url}").with(:backpack, {}).once
118
+ controller.send("edit_resource_#{path_or_url}")
119
+
120
+ controller.expects("admin_#{path_or_url}").with({}).once
121
+ controller.send("parent_#{path_or_url}")
122
+
123
+ # With arg
124
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
125
+ controller.send("resource_#{path_or_url}", :arg)
126
+
127
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, {}).once
128
+ controller.send("resource_#{path_or_url}", :arg)
129
+
130
+ # With options
131
+ controller.expects("admin_backpack_#{path_or_url}").with(:arg, :page => 1).once
132
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
133
+ end
134
+ end
135
+
136
+ def test_url_helpers_on_simple_inherited_singleton_resource
137
+ controller = UniversesController.new
138
+ controller.instance_variable_set('@universe', :universe)
139
+
140
+ [:url, :path].each do |path_or_url|
141
+ controller.expects("root_#{path_or_url}").with({}).once
142
+ controller.send("collection_#{path_or_url}")
143
+
144
+ controller.expects("universum_#{path_or_url}").with({}).once
145
+ controller.send("resource_#{path_or_url}")
146
+
147
+ controller.expects("new_universum_#{path_or_url}").with({}).once
148
+ controller.send("new_resource_#{path_or_url}")
149
+
150
+ controller.expects("edit_universum_#{path_or_url}").with({}).once
151
+ controller.send("edit_resource_#{path_or_url}")
152
+
153
+ controller.expects("root_#{path_or_url}").with({}).once
154
+ controller.send("parent_#{path_or_url}")
155
+
156
+ # With options
157
+ # Also tests that argument sent are not used
158
+ controller.expects("universum_#{path_or_url}").with(:page => 1).once
159
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
160
+ end
161
+ end
162
+
163
+ def test_url_helpers_on_belongs_to
164
+ controller = TablesController.new
165
+ controller.instance_variable_set('@house', :house)
166
+ controller.instance_variable_set('@table', :table)
167
+
168
+ [:url, :path].each do |path_or_url|
169
+ controller.expects("house_tables_#{path_or_url}").with(:house, {}).once
170
+ controller.send("collection_#{path_or_url}")
171
+
172
+ controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
173
+ controller.send("resource_#{path_or_url}")
174
+
175
+ controller.expects("new_house_table_#{path_or_url}").with(:house, {}).once
176
+ controller.send("new_resource_#{path_or_url}")
177
+
178
+ controller.expects("edit_house_table_#{path_or_url}").with(:house, :table, {}).once
179
+ controller.send("edit_resource_#{path_or_url}")
180
+
181
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
182
+ controller.send("parent_#{path_or_url}")
183
+
184
+ # With arg
185
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
186
+ controller.send("resource_#{path_or_url}", :arg)
187
+
188
+ controller.expects("edit_house_table_#{path_or_url}").with(:house, :arg, {}).once
189
+ controller.send("edit_resource_#{path_or_url}", :arg)
190
+
191
+ controller.expects("house_#{path_or_url}").with(:arg, {}).once
192
+ controller.send("parent_#{path_or_url}", :arg)
193
+
194
+ # With options
195
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, :page => 1).once
196
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
197
+ end
198
+ end
199
+
200
+ def test_url_helpers_on_not_default_belongs_to
201
+ controller = RoomsController.new
202
+ controller.instance_variable_set('@house', :house)
203
+ controller.instance_variable_set('@room', :room)
204
+
205
+ [:url, :path].each do |path_or_url|
206
+ controller.expects("big_house_rooms_#{path_or_url}").with(:house, {}).once
207
+ controller.send("collection_#{path_or_url}")
208
+
209
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :room, {}).once
210
+ controller.send("resource_#{path_or_url}")
211
+
212
+ controller.expects("new_big_house_room_#{path_or_url}").with(:house, {}).once
213
+ controller.send("new_resource_#{path_or_url}")
214
+
215
+ controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :room, {}).once
216
+ controller.send("edit_resource_#{path_or_url}")
217
+
218
+ controller.expects("big_house_#{path_or_url}").with(:house, {}).once
219
+ controller.send("parent_#{path_or_url}")
220
+
221
+ # With args
222
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, {}).once
223
+ controller.send("resource_#{path_or_url}", :arg)
224
+
225
+ controller.expects("edit_big_house_room_#{path_or_url}").with(:house, :arg, {}).once
226
+ controller.send("edit_resource_#{path_or_url}", :arg)
227
+
228
+ controller.expects("big_house_#{path_or_url}").with(:arg, {}).once
229
+ controller.send("parent_#{path_or_url}", :arg)
230
+
231
+ # With options
232
+ controller.expects("big_house_room_#{path_or_url}").with(:house, :arg, :page => 1).once
233
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
234
+ end
235
+ end
236
+
237
+ def test_url_helpers_on_nested_belongs_to
238
+ controller = ChairsController.new
239
+ controller.instance_variable_set('@house', :house)
240
+ controller.instance_variable_set('@table', :table)
241
+ controller.instance_variable_set('@chair', :chair)
242
+
243
+ [:url, :path].each do |path_or_url|
244
+ controller.expects("house_table_chairs_#{path_or_url}").with(:house, :table, {}).once
245
+ controller.send("collection_#{path_or_url}")
246
+
247
+ controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
248
+ controller.send("resource_#{path_or_url}")
249
+
250
+ controller.expects("new_house_table_chair_#{path_or_url}").with(:house, :table, {}).once
251
+ controller.send("new_resource_#{path_or_url}")
252
+
253
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :chair, {}).once
254
+ controller.send("edit_resource_#{path_or_url}")
255
+
256
+ controller.expects("house_table_#{path_or_url}").with(:house, :table, {}).once
257
+ controller.send("parent_#{path_or_url}")
258
+
259
+ # With args
260
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
261
+ controller.send("edit_resource_#{path_or_url}", :arg)
262
+
263
+ controller.expects("house_table_chair_#{path_or_url}").with(:house, :table, :arg, {}).once
264
+ controller.send("resource_#{path_or_url}", :arg)
265
+
266
+ controller.expects("house_table_#{path_or_url}").with(:house, :arg, {}).once
267
+ controller.send("parent_#{path_or_url}", :arg)
268
+
269
+ # With options
270
+ controller.expects("edit_house_table_chair_#{path_or_url}").with(:house, :table, :arg, :page => 1).once
271
+ controller.send("edit_resource_#{path_or_url}", :arg, :page => 1)
272
+ end
273
+ end
274
+
275
+ def test_url_helpers_on_singletons_with_belongs_to
276
+ controller = OwnersController.new
277
+ controller.instance_variable_set('@house', :house)
278
+ controller.instance_variable_set('@owner', :owner)
279
+
280
+ [:url, :path].each do |path_or_url|
281
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
282
+ controller.send("collection_#{path_or_url}")
283
+
284
+ controller.expects("house_owner_#{path_or_url}").with(:house, {}).once
285
+ controller.send("resource_#{path_or_url}")
286
+
287
+ controller.expects("new_house_owner_#{path_or_url}").with(:house, {}).once
288
+ controller.send("new_resource_#{path_or_url}")
289
+
290
+ controller.expects("edit_house_owner_#{path_or_url}").with(:house, {}).once
291
+ controller.send("edit_resource_#{path_or_url}")
292
+
293
+ controller.expects("house_#{path_or_url}").with(:house, {}).once
294
+ controller.send("parent_#{path_or_url}")
295
+
296
+ # With options
297
+ # Also tests that argument sent are not used
298
+ controller.expects("house_owner_#{path_or_url}").with(:house, :page => 1).once
299
+ controller.send("resource_#{path_or_url}", :arg, :page => 1)
300
+ end
301
+ end
302
+
303
+ def test_url_helpers_on_polymorphic_belongs_to
304
+ house = House.new
305
+ bed = Bed.new
306
+
307
+ new_bed = Bed.new
308
+ Bed.stubs(:new).returns(new_bed)
309
+ new_bed.stubs(:new_record?).returns(true)
310
+
311
+ controller = BedsController.new
312
+ controller.instance_variable_set('@parent_type', :house)
313
+ controller.instance_variable_set('@house', house)
314
+ controller.instance_variable_set('@bed', bed)
315
+
316
+ [:url, :path].each do |path_or_url|
317
+ controller.expects("house_beds_#{path_or_url}").with(house).once
318
+ controller.send("collection_#{path_or_url}")
319
+
320
+ controller.expects("house_bed_#{path_or_url}").with(house, bed).once
321
+ controller.send("resource_#{path_or_url}")
322
+
323
+ controller.expects("new_house_bed_#{path_or_url}").with(house).once
324
+ controller.send("new_resource_#{path_or_url}")
325
+
326
+ controller.expects("edit_house_bed_#{path_or_url}").with(house, bed).once
327
+ controller.send("edit_resource_#{path_or_url}")
328
+
329
+ controller.expects("house_#{path_or_url}").with(house).once
330
+ controller.send("parent_#{path_or_url}")
331
+ end
332
+
333
+ # With options
334
+ controller.expects("house_bed_url").with(house, bed, :page => 1).once
335
+ controller.send("resource_url", :page => 1)
336
+
337
+ controller.expects("house_url").with(house, :page => 1).once
338
+ controller.send("parent_url", :page => 1)
339
+
340
+ # With args
341
+ controller.expects("polymorphic_url").with([:arg, new_bed], {}).once
342
+ controller.send("collection_url", :arg)
343
+
344
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
345
+ controller.send("resource_url", :arg)
346
+
347
+ controller.expects("edit_polymorphic_url").with([house, :arg], {}).once
348
+ controller.send("edit_resource_url", :arg)
349
+
350
+ controller.expects("polymorphic_url").with([:arg], {}).once
351
+ controller.send("parent_url", :arg)
352
+ end
353
+
354
+ def test_url_helpers_on_namespaced_polymorphic_belongs_to
355
+ house = House.new
356
+ desk = Desk.new
357
+
358
+ new_desk = Desk.new
359
+ Desk.stubs(:new).returns(new_desk)
360
+ new_desk.stubs(:new_record?).returns(true)
361
+
362
+ controller = Admin::DesksController.new
363
+ controller.instance_variable_set('@parent_type', :house)
364
+ controller.instance_variable_set('@house', house)
365
+ controller.instance_variable_set('@desk', desk)
366
+
367
+ [:url, :path].each do |path_or_url|
368
+ controller.expects("admin_house_desks_#{path_or_url}").with(house).once
369
+ controller.send("collection_#{path_or_url}")
370
+
371
+ controller.expects("admin_house_desk_#{path_or_url}").with(house, desk).once
372
+ controller.send("resource_#{path_or_url}")
373
+
374
+ controller.expects("new_admin_house_desk_#{path_or_url}").with(house).once
375
+ controller.send("new_resource_#{path_or_url}")
376
+
377
+ controller.expects("edit_admin_house_desk_#{path_or_url}").with(house, desk).once
378
+ controller.send("edit_resource_#{path_or_url}")
379
+
380
+ controller.expects("admin_house_#{path_or_url}").with(house).once
381
+ controller.send("parent_#{path_or_url}")
382
+ end
383
+
384
+ # With options
385
+ controller.expects("admin_house_desk_url").with(house, desk, :page => 1).once
386
+ controller.send("resource_url", :page => 1)
387
+
388
+ controller.expects("admin_house_url").with(house, :page => 1).once
389
+ controller.send("parent_url", :page => 1)
390
+
391
+ # With args
392
+ controller.expects("polymorphic_url").with(['admin', :arg, new_desk], {}).once
393
+ controller.send("collection_url", :arg)
394
+
395
+ controller.expects("polymorphic_url").with(['admin', house, :arg], {}).once
396
+ controller.send("resource_url", :arg)
397
+
398
+ controller.expects("edit_polymorphic_url").with(['admin', house, :arg], {}).once
399
+ controller.send("edit_resource_url", :arg)
400
+
401
+ controller.expects("polymorphic_url").with(['admin', :arg], {}).once
402
+ controller.send("parent_url", :arg)
403
+ end
404
+
405
+ def test_url_helpers_on_nested_polymorphic_belongs_to
406
+ house = House.new
407
+ table = Table.new
408
+ dish = Dish.new
409
+
410
+ new_dish = Dish.new
411
+ Dish.stubs(:new).returns(new_dish)
412
+ new_dish.stubs(:new_record?).returns(true)
413
+
414
+ controller = DishesController.new
415
+ controller.instance_variable_set('@parent_type', :table)
416
+ controller.instance_variable_set('@house', house)
417
+ controller.instance_variable_set('@table', table)
418
+ controller.instance_variable_set('@dish', dish)
419
+
420
+ [:url, :path].each do |path_or_url|
421
+ controller.expects("house_table_dishes_#{path_or_url}").with(house, table).once
422
+ controller.send("collection_#{path_or_url}")
423
+
424
+ controller.expects("house_table_dish_#{path_or_url}").with(house, table, dish).once
425
+ controller.send("resource_#{path_or_url}")
426
+
427
+ controller.expects("new_house_table_dish_#{path_or_url}").with(house, table).once
428
+ controller.send("new_resource_#{path_or_url}")
429
+
430
+ controller.expects("edit_house_table_dish_#{path_or_url}").with(house, table, dish).once
431
+ controller.send("edit_resource_#{path_or_url}")
432
+
433
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
434
+ controller.send("parent_#{path_or_url}")
435
+ end
436
+
437
+ # With options
438
+ controller.expects("house_table_dish_url").with(house, table, dish, :page => 1).once
439
+ controller.send("resource_url", :page => 1)
440
+
441
+ controller.expects("house_table_url").with(house, table, :page => 1).once
442
+ controller.send("parent_url", :page => 1)
443
+
444
+ # With args
445
+ controller.expects("polymorphic_url").with([house, table, :arg], {}).once
446
+ controller.send("resource_url", :arg)
447
+
448
+ controller.expects("edit_polymorphic_url").with([house, table, :arg], {}).once
449
+ controller.send("edit_resource_url", :arg)
450
+
451
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
452
+ controller.send("parent_url", :arg)
453
+ end
454
+
455
+ def test_url_helpers_on_singleton_nested_polymorphic_belongs_to
456
+ # This must not be usefull in singleton controllers...
457
+ # Center.new
458
+ house = House.new
459
+ table = Table.new
460
+
461
+ controller = CentersController.new
462
+ controller.instance_variable_set('@parent_type', :table)
463
+ controller.instance_variable_set('@house', house)
464
+ controller.instance_variable_set('@table', table)
465
+
466
+ # This must not be useful in singleton controllers...
467
+ # controller.instance_variable_set('@center', :center)
468
+
469
+ [:url, :path].each do |path_or_url|
470
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
471
+ controller.send("collection_#{path_or_url}")
472
+
473
+ controller.expects("house_table_center_#{path_or_url}").with(house, table).once
474
+ controller.send("resource_#{path_or_url}")
475
+
476
+ controller.expects("new_house_table_center_#{path_or_url}").with(house, table).once
477
+ controller.send("new_resource_#{path_or_url}")
478
+
479
+ controller.expects("edit_house_table_center_#{path_or_url}").with(house, table).once
480
+ controller.send("edit_resource_#{path_or_url}")
481
+
482
+ controller.expects("house_table_#{path_or_url}").with(house, table).once
483
+ controller.send("parent_#{path_or_url}")
484
+ end
485
+
486
+ # With options
487
+ controller.expects("house_table_center_url").with(house, table, :page => 1)
488
+ controller.send("resource_url", :page => 1)
489
+
490
+ controller.expects("house_table_url").with(house, table, :page => 1)
491
+ controller.send("parent_url", :page => 1)
492
+
493
+ # With args
494
+ controller.expects("polymorphic_url").with([house, table, :center], {}).once
495
+ controller.send("resource_url", :arg)
496
+
497
+ controller.expects("polymorphic_url").with([house, :arg], {}).once
498
+ controller.send("parent_url", :arg)
499
+ end
500
+
501
+ def test_url_helpers_on_optional_polymorphic_belongs_to
502
+ bed = Bed.new
503
+ new_bed = Bed.new
504
+ Bed.stubs(:new).returns(new_bed)
505
+ new_bed.stubs(:new_record?).returns(true)
506
+
507
+ controller = BedsController.new
508
+ controller.instance_variable_set('@parent_type', nil)
509
+ controller.instance_variable_set('@bed', bed)
510
+
511
+ [:url, :path].each do |path_or_url|
512
+ controller.expects("beds_#{path_or_url}").with().once
513
+ controller.send("collection_#{path_or_url}")
514
+
515
+ controller.expects("bed_#{path_or_url}").with(bed).once
516
+ controller.send("resource_#{path_or_url}")
517
+
518
+ controller.expects("new_bed_#{path_or_url}").with().once
519
+ controller.send("new_resource_#{path_or_url}")
520
+
521
+ controller.expects("edit_bed_#{path_or_url}").with(bed).once
522
+ controller.send("edit_resource_#{path_or_url}")
523
+ end
524
+
525
+ # With options
526
+ controller.expects("bed_url").with(bed, :page => 1).once
527
+ controller.send("resource_url", :page => 1)
528
+
529
+ # With args
530
+ controller.expects("polymorphic_url").with([:arg], {}).once
531
+ controller.send("resource_url", :arg)
532
+
533
+ controller.expects("edit_polymorphic_url").with([:arg], {}).once
534
+ controller.send("edit_resource_url", :arg)
535
+ end
536
+
537
+ end