inherited_resources 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/CHANGELOG +103 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +524 -0
  4. data/Rakefile +40 -0
  5. data/lib/inherited_resources.rb +23 -0
  6. data/lib/inherited_resources/actions.rb +79 -0
  7. data/lib/inherited_resources/base.rb +42 -0
  8. data/lib/inherited_resources/base_helpers.rb +363 -0
  9. data/lib/inherited_resources/belongs_to_helpers.rb +89 -0
  10. data/lib/inherited_resources/class_methods.rb +338 -0
  11. data/lib/inherited_resources/dsl.rb +26 -0
  12. data/lib/inherited_resources/dumb_responder.rb +20 -0
  13. data/lib/inherited_resources/has_scope_helpers.rb +83 -0
  14. data/lib/inherited_resources/legacy/respond_to.rb +156 -0
  15. data/lib/inherited_resources/legacy/responder.rb +200 -0
  16. data/lib/inherited_resources/polymorphic_helpers.rb +155 -0
  17. data/lib/inherited_resources/singleton_helpers.rb +95 -0
  18. data/lib/inherited_resources/url_helpers.rb +179 -0
  19. data/test/aliases_test.rb +139 -0
  20. data/test/association_chain_test.rb +125 -0
  21. data/test/base_test.rb +225 -0
  22. data/test/belongs_to_test.rb +87 -0
  23. data/test/class_methods_test.rb +138 -0
  24. data/test/customized_base_test.rb +162 -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 +38 -0
  36. data/test/url_helpers_test.rb +537 -0
  37. metadata +89 -0
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Machine;
4
+ def self.human_name; 'Machine'; end
5
+ end
6
+
7
+ class MachinesController < InheritedResources::Base
8
+ def create
9
+ create!{ complex_url(:create, true, true) }
10
+ end
11
+
12
+ def update
13
+ update!{ complex_url(:update, false, false) }
14
+ end
15
+
16
+ def destroy
17
+ destroy!{ complex_url(:destroy, true, false) }
18
+ end
19
+
20
+ protected
21
+ def complex_url(name, arg2, arg3)
22
+ 'http://test.host/' + name.to_s
23
+ end
24
+ end
25
+
26
+ class RedirectToWithBlockTest < ActionController::TestCase
27
+ tests MachinesController
28
+
29
+ def test_redirect_to_the_given_url_on_create
30
+ Machine.stubs(:new).returns(mock_machine(:save => true))
31
+ post :create
32
+ assert_redirected_to 'http://test.host/create'
33
+ end
34
+
35
+ def test_redirect_to_the_given_url_on_update
36
+ Machine.stubs(:find).returns(mock_machine(:update_attributes => true))
37
+ put :update
38
+ assert_redirected_to 'http://test.host/update'
39
+ end
40
+
41
+ def test_redirect_to_the_given_url_on_destroy
42
+ Machine.stubs(:find).returns(mock_machine(:destroy => true))
43
+ delete :destroy
44
+ assert_redirected_to 'http://test.host/destroy'
45
+ end
46
+
47
+ protected
48
+ def mock_machine(stubs={})
49
+ @mock_machine ||= mock(stubs)
50
+ end
51
+ end
@@ -0,0 +1,155 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class Project
4
+ def to_html
5
+ 'Generated HTML'
6
+ end
7
+
8
+ def to_xml
9
+ 'Generated XML'
10
+ end
11
+
12
+ [:to_json, :to_rss, :to_rjs].each do |method|
13
+ undef_method method if respond_to? method
14
+ end
15
+ end
16
+
17
+ class ProjectsController < ActionController::Base
18
+ respond_to :html
19
+ respond_to :xml, :except => :edit
20
+ respond_to :rjs, :only => :edit
21
+ respond_to :rss, :only => :index
22
+ respond_to :json, :except => :index
23
+ respond_to :csv, :except => :index
24
+
25
+ def index
26
+ respond_with(Project.new)
27
+ end
28
+
29
+ def respond_with_resource
30
+ respond_with(Project.new)
31
+ end
32
+
33
+ def respond_with_resource_and_options
34
+ respond_with(Project.new, :location => 'http://test.host/')
35
+ end
36
+
37
+ def respond_with_resource_and_blocks
38
+ respond_with(Project.new) do |format|
39
+ format.json { render :text => 'Render JSON' }
40
+ format.rss { render :text => 'Render RSS' }
41
+ end
42
+ end
43
+
44
+ # If the user request Mime::ALL and we have a template called action.html.erb,
45
+ # the html template should be rendered *unless* html is specified inside the
46
+ # block. This tests exactly this case.
47
+ #
48
+ def respond_to_skip_default_template
49
+ respond_with(Project.new) do |format|
50
+ format.html { render :text => 'Render HTML' }
51
+ end
52
+ end
53
+ end
54
+
55
+ class SuperProjectsController < ProjectsController
56
+ end
57
+
58
+ class RespondToFunctionalTest < ActionController::TestCase
59
+ tests ProjectsController
60
+
61
+ def test_respond_with_layout_rendering
62
+ @request.accept = 'text/html'
63
+ get :index
64
+ assert_equal 'Index HTML', @response.body.strip
65
+ end
66
+
67
+ def test_respond_with_calls_to_format_on_resource
68
+ @request.accept = 'application/xml'
69
+ get :index
70
+ assert_equal 'Generated XML', @response.body.strip
71
+ end
72
+
73
+ def test_respond_with_inherits_format
74
+ @request.accept = 'application/xml'
75
+ get :index
76
+ assert_equal 'Generated XML', @response.body.strip
77
+ end
78
+
79
+ def test_respond_with_renders_status_not_acceptable_if_mime_type_is_not_registered
80
+ @request.accept = 'text/csv'
81
+ get :index
82
+ assert_equal '406 Not Acceptable', @response.status
83
+ end
84
+
85
+ def test_respond_with_raises_error_if_could_not_respond
86
+ @request.accept = 'application/rss+xml'
87
+ assert_raise ActionView::MissingTemplate do
88
+ get :index
89
+ end
90
+ end
91
+
92
+ def test_respond_to_all
93
+ @request.accept = '*/*'
94
+ get :index
95
+ assert_equal 'Index HTML', @response.body.strip
96
+ end
97
+
98
+ def test_respond_with_sets_content_type_properly
99
+ @request.accept = 'text/html'
100
+ get :index
101
+ assert_equal 'text/html', @response.content_type
102
+ assert_equal :html, @response.template.template_format
103
+
104
+ @request.accept = 'application/xml'
105
+ get :index
106
+ assert_equal 'application/xml', @response.content_type
107
+ assert_equal :xml, @response.template.template_format
108
+ end
109
+
110
+ def test_respond_with_forwads_extra_options_to_render
111
+ @request.accept = 'application/xml'
112
+ get :respond_with_resource_and_options
113
+ assert_equal 'Generated XML', @response.body.strip
114
+ assert_equal 'http://test.host/', @response.headers['Location']
115
+ end
116
+
117
+ def test_respond_to_when_a_resource_is_given_as_option
118
+ @request.accept = 'text/html'
119
+ get :respond_with_resource
120
+ assert_equal 'RespondTo HTML', @response.body.strip
121
+
122
+ @request.accept = 'application/xml'
123
+ get :respond_with_resource
124
+ assert_equal 'Generated XML', @response.body.strip
125
+
126
+ @request.accept = 'application/rss+xml'
127
+ get :respond_with_resource
128
+ assert_equal '406 Not Acceptable', @response.status
129
+
130
+ @request.accept = 'application/json'
131
+ assert_raise ActionView::MissingTemplate do
132
+ get :respond_with_resource
133
+ end
134
+ end
135
+
136
+ def test_respond_to_overwrite_class_method_definition
137
+ @request.accept = 'application/rss+xml'
138
+ get :respond_with_resource_and_blocks
139
+ assert_equal 'Render RSS', @response.body.strip
140
+ end
141
+
142
+ def test_respond_to_first_configured_mime_in_respond_to_when_mime_type_is_all
143
+ @request.accept = '*/*'
144
+ assert_raise ActionView::MissingTemplate do
145
+ get :respond_with_resource_and_blocks
146
+ end
147
+ assert_equal 'text/html', @response.content_type
148
+ end
149
+
150
+ def test_respond_to_skip_default_template_when_it_is_in_block
151
+ @request.accept = '*/*'
152
+ get :respond_to_skip_default_template
153
+ assert_equal 'Render HTML', @response.body.strip
154
+ end
155
+ end
@@ -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,38 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ gem "test-unit"
5
+ rescue LoadError
6
+ end
7
+
8
+ begin
9
+ gem "ruby-debug"
10
+ require 'ruby-debug'
11
+ rescue LoadError
12
+ end
13
+
14
+ require 'test/unit'
15
+ require 'mocha'
16
+
17
+ ENV["RAILS_ENV"] = "test"
18
+ RAILS_ROOT = "anywhere"
19
+
20
+ require 'active_support'
21
+ require 'action_controller'
22
+ require 'action_controller/test_case'
23
+ require 'action_controller/test_process'
24
+
25
+ I18n.load_path << File.join(File.dirname(__FILE__), 'locales', 'en.yml')
26
+ I18n.reload!
27
+
28
+ class ApplicationController < ActionController::Base; end
29
+
30
+ # Add IR to load path and load the main file
31
+ ActiveSupport::Dependencies.load_paths << File.expand_path(File.dirname(__FILE__) + '/../lib')
32
+ require_dependency 'inherited_resources'
33
+
34
+ ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
35
+
36
+ ActionController::Routing::Routes.draw do |map|
37
+ map.connect ':controller/:action/:id'
38
+ 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