1 require 'abstract_unit'
3 class ResourcesController
< ActionController
::Base
4 def index() render
:nothing => true end
5 alias_method
:show, :index
6 def rescue_action(e
) raise e
end
9 class ThreadsController
< ResourcesController
; end
10 class MessagesController
< ResourcesController
; end
11 class CommentsController
< ResourcesController
; end
12 class AuthorsController
< ResourcesController
; end
13 class LogosController
< ResourcesController
; end
15 class AccountsController
< ResourcesController
; end
16 class AdminController
< ResourcesController
; end
17 class ProductsController
< ResourcesController
; end
18 class ImagesController
< ResourcesController
; end
21 class ProductsController
< ResourcesController
; end
22 class TagsController
< ResourcesController
; end
23 class ManufacturersController
< ResourcesController
; end
24 class ImagesController
< ResourcesController
; end
27 class ProductsController
< ResourcesController
; end
28 class ImagesController
< ResourcesController
; end
32 class ResourcesTest
< ActionController
::TestCase
33 # The assertions in these tests are incompatible with the hash method
34 # optimisation. This could indicate user level problems
36 ActionController
::Base.optimise_named_routes
= false
40 ActionController
::Base.optimise_named_routes
= true
43 def test_should_arrange_actions
44 resource
= ActionController
::Resources::Resource.new(:messages,
45 :collection => { :rss => :get, :reorder => :post, :csv => :post },
46 :member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
47 :new => { :preview => :get, :draft => :get })
49 assert_resource_methods
[:rss], resource
, :collection, :get
50 assert_resource_methods
[:csv, :reorder], resource
, :collection, :post
51 assert_resource_methods
[:edit, :rss, :atom], resource
, :member, :get
52 assert_resource_methods
[:upload, :fix], resource
, :member, :post
53 assert_resource_methods
[:new, :preview, :draft], resource
, :new, :get
56 def test_should_resource_controller_name_equal_resource_name_by_default
57 resource
= ActionController
::Resources::Resource.new(:messages, {})
58 assert_equal
'messages', resource
.controller
61 def test_should_resource_controller_name_equal_controller_option
62 resource
= ActionController
::Resources::Resource.new(:messages, :controller => 'posts')
63 assert_equal
'posts', resource
.controller
66 def test_should_all_singleton_paths_be_the_same
67 [ :path, :nesting_path_prefix, :member_path ].each
do |method
|
68 resource
= ActionController
::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
69 assert_equal
'admin/messages', resource
.send(method
)
73 def test_default_restful_routes
74 with_restful_routing
:messages do
75 assert_simply_restful_for
:messages
79 def test_override_paths_for_default_restful_actions
80 resource
= ActionController
::Resources::Resource.new(:messages,
81 :path_names => {:new => 'nuevo', :edit => 'editar'})
82 assert_equal resource
.new_path
, "#{resource.path}/nuevo"
85 def test_multiple_default_restful_routes
86 with_restful_routing
:messages, :comments do
87 assert_simply_restful_for
:messages
88 assert_simply_restful_for
:comments
92 def test_with_custom_conditions
93 with_restful_routing
:messages, :conditions => { :subdomain => 'app' } do
94 assert_equal
'app', ActionController
::Routing::Routes.named_routes
.routes
[:messages].conditions
[:subdomain]
98 def test_irregular_id_with_no_requirements_should_raise_error
99 expected_options
= {:controller => 'messages', :action => 'show', :id => '1.1.1'}
101 with_restful_routing
:messages do
102 assert_raise(ActionController
::RoutingError) do
103 assert_recognizes(expected_options
, :path => 'messages/1.1.1', :method => :get)
108 def test_irregular_id_with_requirements_should_pass
109 expected_options
= {:controller => 'messages', :action => 'show', :id => '1.1.1'}
111 with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
112 assert_recognizes(expected_options
, :path => 'messages/1.1.1', :method => :get)
116 def test_with_path_prefix_requirements
117 expected_options
= {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
118 with_restful_routing
:messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
119 assert_recognizes(expected_options
, :path => 'thread/1.1.1/messages/1', :method => :get)
123 def test_with_path_prefix
124 with_restful_routing
:messages, :path_prefix => '/thread/:thread_id' do
125 assert_simply_restful_for
:messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
129 def test_multiple_with_path_prefix
130 with_restful_routing
:messages, :comments, :path_prefix => '/thread/:thread_id' do
131 assert_simply_restful_for
:messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
132 assert_simply_restful_for
:comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
136 def test_with_name_prefix
137 with_restful_routing
:messages, :name_prefix => 'post_' do
138 assert_simply_restful_for
:messages, :name_prefix => 'post_'
142 def test_with_collection_actions
143 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
145 with_restful_routing
:messages, :collection => actions
do
146 assert_restful_routes_for
:messages do |options
|
147 actions
.each
do |action
, method
|
148 assert_recognizes(options
.merge(:action => action
), :path => "/messages/#{action}", :method => method
)
152 assert_restful_named_routes_for
:messages do |options
|
153 actions
.keys
.each
do |action
|
154 assert_named_route
"/messages/#{action}", "#{action}_messages_path", :action => action
160 def test_with_collection_actions_and_name_prefix
161 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
163 with_restful_routing
:messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions
do
164 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
165 actions
.each
do |action
, method
|
166 assert_recognizes(options
.merge(:action => action
), :path => "/threads/1/messages/#{action}", :method => method
)
170 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
171 actions
.keys
.each
do |action
|
172 assert_named_route
"/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
178 def test_with_collection_actions_and_name_prefix_and_member_action_with_same_name
179 actions
= { 'a' => :get }
181 with_restful_routing
:messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions
, :member => actions
do
182 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
183 actions
.each
do |action
, method
|
184 assert_recognizes(options
.merge(:action => action
), :path => "/threads/1/messages/#{action}", :method => method
)
188 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
189 actions
.keys
.each
do |action
|
190 assert_named_route
"/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
196 def test_with_collection_action_and_name_prefix_and_formatted
197 actions
= { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
199 with_restful_routing
:messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions
do
200 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
201 actions
.each
do |action
, method
|
202 assert_recognizes(options
.merge(:action => action
, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method
)
206 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
207 actions
.keys
.each
do |action
|
208 assert_named_route
"/threads/1/messages/#{action}.xml", "#{action}_thread_messages_path", :action => action
, :format => 'xml'
214 def test_with_member_action
215 [:put, :post].each
do |method
|
216 with_restful_routing
:messages, :member => { :mark => method
} do
217 mark_options
= {:action => 'mark', :id => '1'}
218 mark_path
= "/messages/1/mark"
219 assert_restful_routes_for
:messages do |options
|
220 assert_recognizes(options
.merge(mark_options
), :path => mark_path
, :method => method
)
223 assert_restful_named_routes_for
:messages do |options
|
224 assert_named_route mark_path
, :mark_message_path, mark_options
230 def test_with_member_action_and_requirement
231 expected_options
= {:controller => 'messages', :action => 'mark', :id => '1.1.1'}
233 with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}, :member => { :mark => :get }) do
234 assert_recognizes(expected_options
, :path => 'messages/1.1.1/mark', :method => :get)
238 def test_member_when_override_paths_for_default_restful_actions_with
239 [:put, :post].each
do |method
|
240 with_restful_routing
:messages, :member => { :mark => method
}, :path_names => {:new => 'nuevo'} do
241 mark_options
= {:action => 'mark', :id => '1', :controller => "messages"}
242 mark_path
= "/messages/1/mark"
244 assert_restful_routes_for
:messages, :path_names => {:new => 'nuevo'} do |options
|
245 assert_recognizes(options
.merge(mark_options
), :path => mark_path
, :method => method
)
248 assert_restful_named_routes_for
:messages, :path_names => {:new => 'nuevo'} do |options
|
249 assert_named_route mark_path
, :mark_message_path, mark_options
255 def test_member_when_changed_default_restful_actions_and_path_names_not_specified
256 default_path_names
= ActionController
::Base.resources_path_names
257 ActionController
::Base.resources_path_names
= {:new => 'nuevo', :edit => 'editar'}
259 with_restful_routing
:messages do
260 new_options
= { :action => 'new', :controller => 'messages' }
261 new_path
= "/messages/nuevo"
262 edit_options
= { :action => 'edit', :id => '1', :controller => 'messages' }
263 edit_path
= "/messages/1/editar"
265 assert_restful_routes_for
:messages do |options
|
266 assert_recognizes(options
.merge(new_options
), :path => new_path
, :method => :get)
269 assert_restful_routes_for
:messages do |options
|
270 assert_recognizes(options
.merge(edit_options
), :path => edit_path
, :method => :get)
274 ActionController
::Base.resources_path_names
= default_path_names
277 def test_with_two_member_actions_with_same_method
278 [:put, :post].each
do |method
|
279 with_restful_routing
:messages, :member => { :mark => method
, :unmark => method
} do
280 %w(mark unmark
).each
do |action
|
281 action_options
= {:action => action
, :id => '1'}
282 action_path
= "/messages/1/#{action}"
283 assert_restful_routes_for
:messages do |options
|
284 assert_recognizes(options
.merge(action_options
), :path => action_path
, :method => method
)
287 assert_restful_named_routes_for
:messages do |options
|
288 assert_named_route action_path
, "#{action}_message_path".to_sym
, action_options
295 def test_array_as_collection_or_member_method_value
296 with_restful_routing
:messages, :collection => { :search => [:get, :post] }, :member => { :toggle => [:get, :post] } do
297 assert_restful_routes_for
:messages do |options
|
298 [:get, :post].each
do |method
|
299 assert_recognizes(options
.merge(:action => 'search'), :path => "/messages/search", :method => method
)
301 [:get, :post].each
do |method
|
302 assert_recognizes(options
.merge(:action => 'toggle', :id => '1'), :path => '/messages/1/toggle', :method => method
)
308 def test_with_new_action
309 with_restful_routing
:messages, :new => { :preview => :post } do
310 preview_options
= {:action => 'preview'}
311 preview_path
= "/messages/new/preview"
312 assert_restful_routes_for
:messages do |options
|
313 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
316 assert_restful_named_routes_for
:messages do |options
|
317 assert_named_route preview_path
, :preview_new_message_path, preview_options
322 def test_with_new_action_with_name_prefix
323 with_restful_routing
:messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
324 preview_options
= {:action => 'preview', :thread_id => '1'}
325 preview_path
= "/threads/1/messages/new/preview"
326 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
327 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
330 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
331 assert_named_route preview_path
, :preview_new_thread_message_path, preview_options
336 def test_with_formatted_new_action_with_name_prefix
337 with_restful_routing
:messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
338 preview_options
= {:action => 'preview', :thread_id => '1', :format => 'xml'}
339 preview_path
= "/threads/1/messages/new/preview.xml"
340 assert_restful_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
341 assert_recognizes(options
.merge(preview_options
), :path => preview_path
, :method => :post)
344 assert_restful_named_routes_for
:messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options
|
345 assert_named_route preview_path
, :preview_new_thread_message_path, preview_options
350 def test_override_new_method
351 with_restful_routing
:messages do
352 assert_restful_routes_for
:messages do |options
|
353 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :get)
354 assert_raise(ActionController
::MethodNotAllowed) do
355 ActionController
::Routing::Routes.recognize_path("/messages/new", :method => :post)
360 with_restful_routing
:messages, :new => { :new => :any } do
361 assert_restful_routes_for
:messages do |options
|
362 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :post)
363 assert_recognizes(options
.merge(:action => "new"), :path => "/messages/new", :method => :get)
368 def test_nested_restful_routes
369 with_routing
do |set
|
371 map
.resources
:threads do |map
|
372 map
.resources
:messages do |map
|
373 map
.resources
:comments
378 assert_simply_restful_for
:threads
379 assert_simply_restful_for
:messages,
380 :name_prefix => 'thread_',
381 :path_prefix => 'threads/1/',
382 :options => { :thread_id => '1' }
383 assert_simply_restful_for
:comments,
384 :name_prefix => 'thread_message_',
385 :path_prefix => 'threads/1/messages/2/',
386 :options => { :thread_id => '1', :message_id => '2' }
390 def test_nested_restful_routes_with_overwritten_defaults
391 with_routing
do |set
|
393 map
.resources
:threads do |map
|
394 map
.resources
:messages, :name_prefix => nil do |map
|
395 map
.resources
:comments, :name_prefix => nil
400 assert_simply_restful_for
:threads
401 assert_simply_restful_for
:messages,
402 :path_prefix => 'threads/1/',
403 :options => { :thread_id => '1' }
404 assert_simply_restful_for
:comments,
405 :path_prefix => 'threads/1/messages/2/',
406 :options => { :thread_id => '1', :message_id => '2' }
410 def test_shallow_nested_restful_routes
411 with_routing
do |set
|
413 map
.resources
:threads, :shallow => true do |map
|
414 map
.resources
:messages do |map
|
415 map
.resources
:comments
420 assert_simply_restful_for
:threads,
422 assert_simply_restful_for
:messages,
423 :name_prefix => 'thread_',
424 :path_prefix => 'threads/1/',
426 :options => { :thread_id => '1' }
427 assert_simply_restful_for
:comments,
428 :name_prefix => 'message_',
429 :path_prefix => 'messages/2/',
431 :options => { :message_id => '2' }
435 def test_shallow_nested_restful_routes_with_namespaces
436 with_routing
do |set
|
438 map
.namespace
:backoffice do |map
|
439 map
.namespace
:admin do |map
|
440 map
.resources
:products, :shallow => true do |map
|
441 map
.resources
:images
447 assert_simply_restful_for
:products,
448 :controller => 'backoffice/admin/products',
449 :namespace => 'backoffice/admin/',
450 :name_prefix => 'backoffice_admin_',
451 :path_prefix => 'backoffice/admin/',
453 assert_simply_restful_for
:images,
454 :controller => 'backoffice/admin/images',
455 :namespace => 'backoffice/admin/',
456 :name_prefix => 'backoffice_admin_product_',
457 :path_prefix => 'backoffice/admin/products/1/',
459 :options => { :product_id => '1' }
463 def test_restful_routes_dont_generate_duplicates
464 with_restful_routing
:messages do
465 routes
= ActionController
::Routing::Routes.routes
466 routes
.each
do |route
|
468 next if route
=== r
# skip the comparison instance
469 assert distinct_routes
?(route
, r
), "Duplicate Route: #{route}"
475 def test_should_create_singleton_resource_routes
476 with_singleton_resources
:account do
477 assert_singleton_restful_for
:account
481 def test_should_create_multiple_singleton_resource_routes
482 with_singleton_resources
:account, :logo do
483 assert_singleton_restful_for
:account
484 assert_singleton_restful_for
:logo
488 def test_should_create_nested_singleton_resource_routes
489 with_routing
do |set
|
491 map
.resource
:admin, :controller => 'admin' do |admin
|
492 admin
.resource
:account
496 assert_singleton_restful_for
:admin, :controller => 'admin'
497 assert_singleton_restful_for
:account, :name_prefix => "admin_", :path_prefix => 'admin/'
501 def test_resource_has_many_should_become_nested_resources
502 with_routing
do |set
|
504 map
.resources
:messages, :has_many => [ :comments, :authors ]
507 assert_simply_restful_for
:messages
508 assert_simply_restful_for
:comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
509 assert_simply_restful_for
:authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
513 def test_resources_has_many_hash_should_become_nested_resources
514 with_routing
do |set
|
516 map
.resources
:threads, :has_many => { :messages => [ :comments, { :authors => :threads } ] }
519 assert_simply_restful_for
:threads
520 assert_simply_restful_for
:messages, :name_prefix => "thread_", :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
521 assert_simply_restful_for
:comments, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
522 assert_simply_restful_for
:authors, :name_prefix => "thread_message_", :path_prefix => 'threads/1/messages/1/', :options => { :thread_id => '1', :message_id => '1' }
523 assert_simply_restful_for
:threads, :name_prefix => "thread_message_author_", :path_prefix => 'threads/1/messages/1/authors/1/', :options => { :thread_id => '1', :message_id => '1', :author_id => '1' }
527 def test_shallow_resource_has_many_should_become_shallow_nested_resources
528 with_routing
do |set
|
530 map
.resources
:messages, :has_many => [ :comments, :authors ], :shallow => true
533 assert_simply_restful_for
:messages, :shallow => true
534 assert_simply_restful_for
:comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
535 assert_simply_restful_for
:authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
539 def test_resource_has_one_should_become_nested_resources
540 with_routing
do |set
|
542 map
.resources
:messages, :has_one => :logo
545 assert_simply_restful_for
:messages
546 assert_singleton_restful_for
:logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
550 def test_shallow_resource_has_one_should_become_shallow_nested_resources
551 with_routing
do |set
|
553 map
.resources
:messages, :has_one => :logo, :shallow => true
556 assert_simply_restful_for
:messages, :shallow => true
557 assert_singleton_restful_for
:logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :shallow => true, :options => { :message_id => '1' }
561 def test_singleton_resource_with_member_action
562 [:put, :post].each
do |method
|
563 with_singleton_resources
:account, :member => { :reset => method
} do
564 reset_options
= {:action => 'reset'}
565 reset_path
= "/account/reset"
566 assert_singleton_routes_for
:account do |options
|
567 assert_recognizes(options
.merge(reset_options
), :path => reset_path
, :method => method
)
570 assert_singleton_named_routes_for
:account do |options
|
571 assert_named_route reset_path
, :reset_account_path, reset_options
577 def test_singleton_resource_with_two_member_actions_with_same_method
578 [:put, :post].each
do |method
|
579 with_singleton_resources
:account, :member => { :reset => method
, :disable => method
} do
580 %w(reset disable
).each
do |action
|
581 action_options
= {:action => action
}
582 action_path
= "/account/#{action}"
583 assert_singleton_routes_for
:account do |options
|
584 assert_recognizes(options
.merge(action_options
), :path => action_path
, :method => method
)
587 assert_singleton_named_routes_for
:account do |options
|
588 assert_named_route action_path
, "#{action}_account_path".to_sym
, action_options
595 def test_should_nest_resources_in_singleton_resource
596 with_routing
do |set
|
598 map
.resource
:account do |account
|
599 account
.resources
:messages
603 assert_singleton_restful_for
:account
604 assert_simply_restful_for
:messages, :name_prefix => "account_", :path_prefix => 'account/'
608 def test_should_nest_resources_in_singleton_resource_with_path_prefix
609 with_routing
do |set
|
611 map
.resource(:account, :path_prefix => ':site_id') do |account
|
612 account
.resources
:messages
616 assert_singleton_restful_for
:account, :path_prefix => '7/', :options => { :site_id => '7' }
617 assert_simply_restful_for
:messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
621 def test_should_nest_singleton_resource_in_resources
622 with_routing
do |set
|
624 map
.resources
:threads do |thread
|
625 thread
.resource
:admin, :controller => 'admin'
629 assert_simply_restful_for
:threads
630 assert_singleton_restful_for
:admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
634 def test_should_not_allow_delete_or_put_on_collection_path
635 controller_name
= :messages
636 with_restful_routing controller_name
do
637 options
= { :controller => controller_name
.to_s
}
638 collection_path
= "/#{controller_name}"
640 assert_raise(ActionController
::MethodNotAllowed) do
641 assert_recognizes(options
.merge(:action => 'update'), :path => collection_path
, :method => :put)
644 assert_raise(ActionController
::MethodNotAllowed) do
645 assert_recognizes(options
.merge(:action => 'destroy'), :path => collection_path
, :method => :delete)
650 def test_should_not_allow_invalid_head_method_for_member_routes
651 with_routing
do |set
|
653 assert_raise(ArgumentError
) do
654 map
.resources
:messages, :member => {:something => :head}
660 def test_should_not_allow_invalid_http_methods_for_member_routes
661 with_routing
do |set
|
663 assert_raise(ArgumentError
) do
664 map
.resources
:messages, :member => {:something => :invalid}
670 def test_resource_action_separator
671 with_routing
do |set
|
673 map
.resources
:messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
674 map
.resource
:account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
677 action_separator
= ActionController
::Base.resource_action_separator
679 assert_simply_restful_for
:messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
680 assert_named_route
"/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
681 assert_named_route
"/threads/1/messages/new", "new_thread_message_path", {}
682 assert_named_route
"/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
683 assert_singleton_restful_for
:account, :name_prefix => 'admin_', :path_prefix => 'admin/'
684 assert_named_route
"/admin/account#{action_separator}login", "login_admin_account_path", {}
685 assert_named_route
"/admin/account/new", "new_admin_account_path", {}
686 assert_named_route
"/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
690 def test_new_style_named_routes_for_resource
691 with_routing
do |set
|
693 map
.resources
:messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
695 assert_simply_restful_for
:messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
696 assert_named_route
"/threads/1/messages/search", "search_thread_messages_path", {}
697 assert_named_route
"/threads/1/messages/new", "new_thread_message_path", {}
698 assert_named_route
"/threads/1/messages/new/preview", "preview_new_thread_message_path", {}
702 def test_new_style_named_routes_for_singleton_resource
703 with_routing
do |set
|
705 map
.resource
:account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
707 assert_singleton_restful_for
:account, :name_prefix => 'admin_', :path_prefix => 'admin/'
708 assert_named_route
"/admin/account/login", "login_admin_account_path", {}
709 assert_named_route
"/admin/account/new", "new_admin_account_path", {}
710 assert_named_route
"/admin/account/new/preview", "preview_new_admin_account_path", {}
714 def test_resources_in_namespace
715 with_routing
do |set
|
717 map
.namespace
:backoffice do |backoffice
|
718 backoffice
.resources
:products
722 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
726 def test_resource_has_many_in_namespace
727 with_routing
do |set
|
729 map
.namespace
:backoffice do |backoffice
|
730 backoffice
.resources
:products, :has_many => :tags
734 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
735 assert_simply_restful_for
:tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
739 def test_resource_has_one_in_namespace
740 with_routing
do |set
|
742 map
.namespace
:backoffice do |backoffice
|
743 backoffice
.resources
:products, :has_one => :manufacturer
747 assert_simply_restful_for
:products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
748 assert_singleton_restful_for
:manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
752 def test_resources_in_nested_namespace
753 with_routing
do |set
|
755 map
.namespace
:backoffice do |backoffice
|
756 backoffice
.namespace
:admin do |admin
|
757 admin
.resources
:products
762 assert_simply_restful_for
:products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
766 def test_resources_using_namespace
767 with_routing
do |set
|
769 map
.resources
:products, :namespace => "backoffice/"
772 assert_simply_restful_for
:products, :controller => "backoffice/products"
776 def test_nested_resources_using_namespace
777 with_routing
do |set
|
779 map
.namespace
:backoffice do |backoffice
|
780 backoffice
.resources
:products do |products
|
781 products
.resources
:images
786 assert_simply_restful_for
:images, :controller => "backoffice/images", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => {:product_id => '1'}
790 def test_nested_resources_in_nested_namespace
791 with_routing
do |set
|
793 map
.namespace
:backoffice do |backoffice
|
794 backoffice
.namespace
:admin do |admin
|
795 admin
.resources
:products do |products
|
796 products
.resources
:images
802 assert_simply_restful_for
:images, :controller => "backoffice/admin/images", :name_prefix => 'backoffice_admin_product_', :path_prefix => 'backoffice/admin/products/1/', :options => {:product_id => '1'}
806 def test_with_path_segment
807 with_restful_routing
:messages do
808 assert_simply_restful_for
:messages
809 assert_recognizes({:controller => "messages", :action => "index"}, "/messages")
810 assert_recognizes({:controller => "messages", :action => "index"}, "/messages/")
813 with_restful_routing
:messages, :as => 'reviews' do
814 assert_simply_restful_for
:messages, :as => 'reviews'
815 assert_recognizes({:controller => "messages", :action => "index"}, "/reviews")
816 assert_recognizes({:controller => "messages", :action => "index"}, "/reviews/")
820 def test_multiple_with_path_segment_and_controller
821 with_routing
do |set
|
823 map
.resources
:products do |products
|
824 products
.resources
:product_reviews, :as => 'reviews', :controller => 'messages'
826 map
.resources
:tutors do |tutors
|
827 tutors
.resources
:tutor_reviews, :as => 'reviews', :controller => 'comments'
831 assert_simply_restful_for
:product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'}
832 assert_simply_restful_for
:tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'}
836 def test_with_path_segment_path_prefix_requirements
837 expected_options
= {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
838 with_restful_routing
:messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do
839 assert_recognizes(expected_options
, :path => 'thread/1.1.1/comments/1', :method => :get)
843 def test_resource_has_only_show_action
844 with_routing
do |set
|
846 map
.resources
:products, :only => :show
849 assert_resource_allowed_routes('products', {}, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
850 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :show, [:index, :new, :create, :edit, :update, :destroy])
854 def test_singleton_resource_has_only_show_action
855 with_routing
do |set
|
857 map
.resource
:account, :only => :show
860 assert_singleton_resource_allowed_routes('accounts', {}, :show, [:index, :new, :create, :edit, :update, :destroy])
861 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :show, [:index, :new, :create, :edit, :update, :destroy])
865 def test_resource_does_not_have_destroy_action
866 with_routing
do |set
|
868 map
.resources
:products, :except => :destroy
871 assert_resource_allowed_routes('products', {}, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
872 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [:index, :new, :create, :show, :edit, :update], :destroy)
876 def test_singleton_resource_does_not_have_destroy_action
877 with_routing
do |set
|
879 map
.resource
:account, :except => :destroy
882 assert_singleton_resource_allowed_routes('accounts', {}, [:new, :create, :show, :edit, :update], :destroy)
883 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [:new, :create, :show, :edit, :update], :destroy)
887 def test_resource_has_only_create_action_and_named_route
888 with_routing
do |set
|
890 map
.resources
:products, :only => :create
893 assert_resource_allowed_routes('products', {}, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
894 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
896 assert_not_nil set
.named_routes
[:products]
900 def test_resource_has_only_update_action_and_named_route
901 with_routing
do |set
|
903 map
.resources
:products, :only => :update
906 assert_resource_allowed_routes('products', {}, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
907 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
909 assert_not_nil set
.named_routes
[:product]
913 def test_resource_has_only_destroy_action_and_named_route
914 with_routing
do |set
|
916 map
.resources
:products, :only => :destroy
919 assert_resource_allowed_routes('products', {}, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
920 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
922 assert_not_nil set
.named_routes
[:product]
926 def test_singleton_resource_has_only_create_action_and_named_route
927 with_routing
do |set
|
929 map
.resource
:account, :only => :create
932 assert_singleton_resource_allowed_routes('accounts', {}, :create, [:new, :show, :edit, :update, :destroy])
933 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :create, [:new, :show, :edit, :update, :destroy])
935 assert_not_nil set
.named_routes
[:account]
939 def test_singleton_resource_has_only_update_action_and_named_route
940 with_routing
do |set
|
942 map
.resource
:account, :only => :update
945 assert_singleton_resource_allowed_routes('accounts', {}, :update, [:new, :create, :show, :edit, :destroy])
946 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :update, [:new, :create, :show, :edit, :destroy])
948 assert_not_nil set
.named_routes
[:account]
952 def test_singleton_resource_has_only_destroy_action_and_named_route
953 with_routing
do |set
|
955 map
.resource
:account, :only => :destroy
958 assert_singleton_resource_allowed_routes('accounts', {}, :destroy, [:new, :create, :show, :edit, :update])
959 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, :destroy, [:new, :create, :show, :edit, :update])
961 assert_not_nil set
.named_routes
[:account]
965 def test_resource_has_only_collection_action
966 with_routing
do |set
|
968 map
.resources
:products, :except => :all, :collection => { :sale => :get }
971 assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
972 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
974 assert_recognizes({ :controller => 'products', :action => 'sale' }, :path => 'products/sale', :method => :get)
975 assert_recognizes({ :controller => 'products', :action => 'sale', :format => 'xml' }, :path => 'products/sale.xml', :method => :get)
979 def test_resource_has_only_member_action
980 with_routing
do |set
|
982 map
.resources
:products, :except => :all, :member => { :preview => :get }
985 assert_resource_allowed_routes('products', {}, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
986 assert_resource_allowed_routes('products', { :format => 'xml' }, { :id => '1' }, [], [:index, :new, :create, :show, :edit, :update, :destroy])
988 assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1' }, :path => 'products/1/preview', :method => :get)
989 assert_recognizes({ :controller => 'products', :action => 'preview', :id => '1', :format => 'xml' }, :path => 'products/1/preview.xml', :method => :get)
993 def test_singleton_resource_has_only_member_action
994 with_routing
do |set
|
996 map
.resource
:account, :except => :all, :member => { :signup => :get }
999 assert_singleton_resource_allowed_routes('accounts', {}, [], [:new, :create, :show, :edit, :update, :destroy])
1000 assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' }, [], [:new, :create, :show, :edit, :update, :destroy])
1002 assert_recognizes({ :controller => 'accounts', :action => 'signup' }, :path => 'account/signup', :method => :get)
1003 assert_recognizes({ :controller => 'accounts', :action => 'signup', :format => 'xml' }, :path => 'account/signup.xml', :method => :get)
1007 def test_nested_resource_has_only_show_and_member_action
1008 with_routing
do |set
|
1010 map
.resources
:products, :only => [:index, :show] do |product
|
1011 product
.resources
:images, :member => { :thumbnail => :get }, :only => :show
1015 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1016 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :show, [:index, :new, :create, :edit, :update, :destroy], 'products/1/images')
1018 assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2' }, :path => 'products/1/images/2/thumbnail', :method => :get)
1019 assert_recognizes({ :controller => 'images', :action => 'thumbnail', :product_id => '1', :id => '2', :format => 'jpg' }, :path => 'products/1/images/2/thumbnail.jpg', :method => :get)
1023 def test_nested_resource_does_not_inherit_only_option
1024 with_routing
do |set
|
1026 map
.resources
:products, :only => :show do |product
|
1027 product
.resources
:images, :except => :destroy
1031 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1032 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
1036 def test_nested_resource_does_not_inherit_only_option_by_default
1037 with_routing
do |set
|
1039 map
.resources
:products, :only => :show do |product
|
1040 product
.resources
:images
1044 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destory], [], 'products/1/images')
1045 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1049 def test_nested_resource_does_not_inherit_except_option
1050 with_routing
do |set
|
1052 map
.resources
:products, :except => :show do |product
|
1053 product
.resources
:images, :only => :destroy
1057 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1058 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
1062 def test_nested_resource_does_not_inherit_except_option_by_default
1063 with_routing
do |set
|
1065 map
.resources
:products, :except => :show do |product
|
1066 product
.resources
:images
1070 assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1071 assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update, :destroy], [], 'products/1/images')
1075 def test_default_singleton_restful_route_uses_get
1076 with_routing
do |set
|
1078 map
.resource
:product
1081 assert_equal
:get, set
.named_routes
.routes
[:product].conditions
[:method]
1086 def with_restful_routing(*args
)
1087 with_routing
do |set
|
1088 set
.draw
{ |map
| map
.resources(*args
) }
1093 def with_singleton_resources(*args
)
1094 with_routing
do |set
|
1095 set
.draw
{ |map
| map
.resource(*args
) }
1100 # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
1101 def assert_simply_restful_for(controller_name
, options
= {})
1102 assert_restful_routes_for controller_name
, options
1103 assert_restful_named_routes_for controller_name
, nil, options
1106 def assert_singleton_restful_for(singleton_name
, options
= {})
1107 assert_singleton_routes_for singleton_name
, options
1108 assert_singleton_named_routes_for singleton_name
, options
1111 def assert_restful_routes_for(controller_name
, options
= {})
1112 options
[:options] ||= {}
1113 options
[:options][:controller] = options
[:controller] || controller_name
.to_s
1115 if options
[:shallow]
1116 options
[:shallow_options] ||= {}
1117 options
[:shallow_options][:controller] = options
[:options][:controller]
1119 options
[:shallow_options] = options
[:options]
1122 new_action
= ActionController
::Base.resources_path_names
[:new] || "new"
1123 edit_action
= ActionController
::Base.resources_path_names
[:edit] || "edit"
1124 if options
[:path_names]
1125 new_action
= options
[:path_names][:new] if options
[:path_names][:new]
1126 edit_action
= options
[:path_names][:edit] if options
[:path_names][:edit]
1129 path
= "#{options[:as] || controller_name}"
1130 collection_path
= "/#{options[:path_prefix]}#{path}"
1131 shallow_path
= "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1132 member_path
= "#{shallow_path}/1"
1133 new_path
= "#{collection_path}/#{new_action}"
1134 edit_member_path
= "#{member_path}/#{edit_action}"
1135 formatted_edit_member_path
= "#{member_path}/#{edit_action}.xml"
1137 with_options(options
[:options]) do |controller
|
1138 controller
.assert_routing collection_path
, :action => 'index'
1139 controller
.assert_routing new_path
, :action => 'new'
1140 controller
.assert_routing
"#{collection_path}.xml", :action => 'index', :format => 'xml'
1141 controller
.assert_routing
"#{new_path}.xml", :action => 'new', :format => 'xml'
1144 with_options(options
[:shallow_options]) do |controller
|
1145 controller
.assert_routing member_path
, :action => 'show', :id => '1'
1146 controller
.assert_routing edit_member_path
, :action => 'edit', :id => '1'
1147 controller
.assert_routing
"#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
1148 controller
.assert_routing formatted_edit_member_path
, :action => 'edit', :id => '1', :format => 'xml'
1151 assert_recognizes(options
[:options].merge(:action => 'index'), :path => collection_path
, :method => :get)
1152 assert_recognizes(options
[:options].merge(:action => 'new'), :path => new_path
, :method => :get)
1153 assert_recognizes(options
[:options].merge(:action => 'create'), :path => collection_path
, :method => :post)
1154 assert_recognizes(options
[:shallow_options].merge(:action => 'show', :id => '1'), :path => member_path
, :method => :get)
1155 assert_recognizes(options
[:shallow_options].merge(:action => 'edit', :id => '1'), :path => edit_member_path
, :method => :get)
1156 assert_recognizes(options
[:shallow_options].merge(:action => 'update', :id => '1'), :path => member_path
, :method => :put)
1157 assert_recognizes(options
[:shallow_options].merge(:action => 'destroy', :id => '1'), :path => member_path
, :method => :delete)
1159 assert_recognizes(options
[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
1160 assert_recognizes(options
[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1161 assert_recognizes(options
[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
1162 assert_recognizes(options
[:shallow_options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
1163 assert_recognizes(options
[:shallow_options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path
, :method => :get)
1164 assert_recognizes(options
[:shallow_options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
1165 assert_recognizes(options
[:shallow_options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
1167 yield options
[:options] if block_given
?
1170 # test named routes like foo_path and foos_path map to the correct options.
1171 def assert_restful_named_routes_for(controller_name
, singular_name
= nil, options
= {})
1172 if singular_name
.is_a
?(Hash
)
1173 options
= singular_name
1176 singular_name
||= controller_name
.to_s
.singularize
1178 options
[:options] ||= {}
1179 options
[:options][:controller] = options
[:controller] || controller_name
.to_s
1181 if options
[:shallow]
1182 options
[:shallow_options] ||= {}
1183 options
[:shallow_options][:controller] = options
[:options][:controller]
1185 options
[:shallow_options] = options
[:options]
1188 @controller = "#{options[:options][:controller].camelize}Controller".constantize
.new
1189 @request = ActionController
::TestRequest.new
1190 @response = ActionController
::TestResponse.new
1191 get
:index, options
[:options]
1192 options
[:options].delete
:action
1194 path
= "#{options[:as] || controller_name}"
1195 shallow_path
= "/#{options[:shallow] ? options[:namespace] : options[:path_prefix]}#{path}"
1196 full_path
= "/#{options[:path_prefix]}#{path}"
1197 name_prefix
= options
[:name_prefix]
1198 shallow_prefix
= options
[:shallow] ? options
[:namespace].try(:gsub, /\//, '_') : options
[:name_prefix]
1201 edit_action
= "edit"
1202 if options
[:path_names]
1203 new_action
= options
[:path_names][:new] || "new"
1204 edit_action
= options
[:path_names][:edit] || "edit"
1207 assert_named_route
"#{full_path}", "#{name_prefix}#{controller_name}_path", options
[:options]
1208 assert_named_route
"#{full_path}.xml", "#{name_prefix}#{controller_name}_path", options
[:options].merge(:format => 'xml')
1209 assert_named_route
"#{shallow_path}/1", "#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1')
1210 assert_named_route
"#{shallow_path}/1.xml", "#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1', :format => 'xml')
1212 assert_named_route
"#{full_path}/#{new_action}", "new_#{name_prefix}#{singular_name}_path", options
[:options]
1213 assert_named_route
"#{full_path}/#{new_action}.xml", "new_#{name_prefix}#{singular_name}_path", options
[:options].merge(:format => 'xml')
1214 assert_named_route
"#{shallow_path}/1/#{edit_action}", "edit_#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1')
1215 assert_named_route
"#{shallow_path}/1/#{edit_action}.xml", "edit_#{shallow_prefix}#{singular_name}_path", options
[:shallow_options].merge(:id => '1', :format => 'xml')
1217 yield options
[:options] if block_given
?
1220 def assert_singleton_routes_for(singleton_name
, options
= {})
1221 options
[:options] ||= {}
1222 options
[:options][:controller] = options
[:controller] || singleton_name
.to_s
.pluralize
1224 full_path
= "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1225 new_path
= "#{full_path}/new"
1226 edit_path
= "#{full_path}/edit"
1227 formatted_edit_path
= "#{full_path}/edit.xml"
1229 with_options options
[:options] do |controller
|
1230 controller
.assert_routing full_path
, :action => 'show'
1231 controller
.assert_routing new_path
, :action => 'new'
1232 controller
.assert_routing edit_path
, :action => 'edit'
1233 controller
.assert_routing
"#{full_path}.xml", :action => 'show', :format => 'xml'
1234 controller
.assert_routing
"#{new_path}.xml", :action => 'new', :format => 'xml'
1235 controller
.assert_routing formatted_edit_path
, :action => 'edit', :format => 'xml'
1238 assert_recognizes(options
[:options].merge(:action => 'show'), :path => full_path
, :method => :get)
1239 assert_recognizes(options
[:options].merge(:action => 'new'), :path => new_path
, :method => :get)
1240 assert_recognizes(options
[:options].merge(:action => 'edit'), :path => edit_path
, :method => :get)
1241 assert_recognizes(options
[:options].merge(:action => 'create'), :path => full_path
, :method => :post)
1242 assert_recognizes(options
[:options].merge(:action => 'update'), :path => full_path
, :method => :put)
1243 assert_recognizes(options
[:options].merge(:action => 'destroy'), :path => full_path
, :method => :delete)
1245 assert_recognizes(options
[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
1246 assert_recognizes(options
[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
1247 assert_recognizes(options
[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path
, :method => :get)
1248 assert_recognizes(options
[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
1249 assert_recognizes(options
[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
1250 assert_recognizes(options
[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
1252 yield options
[:options] if block_given
?
1255 def assert_singleton_named_routes_for(singleton_name
, options
= {})
1256 (options
[:options] ||= {})[:controller] ||= singleton_name
.to_s
.pluralize
1257 @controller = "#{options[:options][:controller].camelize}Controller".constantize
.new
1258 @request = ActionController
::TestRequest.new
1259 @response = ActionController
::TestResponse.new
1260 get
:show, options
[:options]
1261 options
[:options].delete
:action
1263 full_path
= "/#{options[:path_prefix]}#{options[:as] || singleton_name}"
1264 name_prefix
= options
[:name_prefix]
1266 assert_named_route
"#{full_path}", "#{name_prefix}#{singleton_name}_path", options
[:options]
1267 assert_named_route
"#{full_path}.xml", "#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1269 assert_named_route
"#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options
[:options]
1270 assert_named_route
"#{full_path}/new.xml", "new_#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1271 assert_named_route
"#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options
[:options]
1272 assert_named_route
"#{full_path}/edit.xml", "edit_#{name_prefix}#{singleton_name}_path", options
[:options].merge(:format => 'xml')
1275 def assert_named_route(expected
, route
, options
)
1276 actual
= @controller.send(route
, options
) rescue $
!.class.name
1277 assert_equal expected
, actual
, "Error on route: #{route}(#{options.inspect})"
1280 def assert_resource_methods(expected
, resource
, action_method
, method
)
1281 assert_equal expected
.length
, resource
.send("#{action_method}_methods")[method
].size
, "#{resource.send("#{action_method}_methods")[method].inspect}"
1282 expected
.each
do |action
|
1283 assert resource
.send("#{action_method}_methods")[method
].include?(action
),
1284 "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
1288 def assert_resource_allowed_routes(controller
, options
, shallow_options
, allowed
, not_allowed
, path
= controller
)
1289 shallow_path
= "#{path}/#{shallow_options[:id]}"
1290 format
= options
[:format] && ".#{options[:format]}"
1291 options
.merge
!(:controller => controller
)
1292 shallow_options
.merge
!(options
)
1294 assert_whether_allowed(allowed
, not_allowed
, options
, 'index', "#{path}#{format}", :get)
1295 assert_whether_allowed(allowed
, not_allowed
, options
, 'new', "#{path}/new#{format}", :get)
1296 assert_whether_allowed(allowed
, not_allowed
, options
, 'create', "#{path}#{format}", :post)
1297 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'show', "#{shallow_path}#{format}", :get)
1298 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'edit', "#{shallow_path}/edit#{format}", :get)
1299 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'update', "#{shallow_path}#{format}", :put)
1300 assert_whether_allowed(allowed
, not_allowed
, shallow_options
, 'destroy', "#{shallow_path}#{format}", :delete)
1303 def assert_singleton_resource_allowed_routes(controller
, options
, allowed
, not_allowed
, path
= controller
.singularize
)
1304 format
= options
[:format] && ".#{options[:format]}"
1305 options
.merge
!(:controller => controller
)
1307 assert_whether_allowed(allowed
, not_allowed
, options
, 'new', "#{path}/new#{format}", :get)
1308 assert_whether_allowed(allowed
, not_allowed
, options
, 'create', "#{path}#{format}", :post)
1309 assert_whether_allowed(allowed
, not_allowed
, options
, 'show', "#{path}#{format}", :get)
1310 assert_whether_allowed(allowed
, not_allowed
, options
, 'edit', "#{path}/edit#{format}", :get)
1311 assert_whether_allowed(allowed
, not_allowed
, options
, 'update', "#{path}#{format}", :put)
1312 assert_whether_allowed(allowed
, not_allowed
, options
, 'destroy', "#{path}#{format}", :delete)
1315 def assert_whether_allowed(allowed
, not_allowed
, options
, action
, path
, method
)
1316 action
= action
.to_sym
1317 options
= options
.merge(:action => action
.to_s
)
1318 path_options
= { :path => path
, :method => method
}
1320 if Array(allowed
).include?(action
)
1321 assert_recognizes options
, path_options
1322 elsif Array(not_allowed
).include?(action
)
1323 assert_not_recognizes options
, path_options
1327 def assert_not_recognizes(expected_options
, path
)
1328 assert_raise ActionController
::RoutingError, ActionController
::MethodNotAllowed, Assertion
do
1329 assert_recognizes(expected_options
, path
)
1333 def distinct_routes
? (r1
, r2
)
1334 if r1
.conditions
== r2
.conditions
and r1
.requirements
== r2
.requirements
then
1335 if r1
.segments
.collect(&:to_s) == r2
.segments
.collect(&:to_s) then