aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/resources_test.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-06-27 08:38:55 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-06-27 08:38:55 +0000
commit557e19346ad144deecd7d76a8a4e4cec22a6597e (patch)
tree7fd3817020f4ba93d406c824ad595f646fd6b88a /actionpack/test/controller/resources_test.rb
parentb5b16a80a9e0386781b370cf110fdc26e13dbc61 (diff)
downloadrails-557e19346ad144deecd7d76a8a4e4cec22a6597e.tar.gz
rails-557e19346ad144deecd7d76a8a4e4cec22a6597e.tar.bz2
rails-557e19346ad144deecd7d76a8a4e4cec22a6597e.zip
Prefix nested resource named routes with their action name, e.g. new_group_user_path(@group) instead of group_new_user_path(@group). The old nested action named route is deprecated in Rails 1.2.4. Closes #8558.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7138 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/test/controller/resources_test.rb')
-rw-r--r--actionpack/test/controller/resources_test.rb165
1 files changed, 132 insertions, 33 deletions
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index f405332fe3..732f3e21da 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -100,7 +100,7 @@ class ResourcesTest < Test::Unit::TestCase
end
end
- def test_multile_with_path_prefix
+ def test_multiple_with_path_prefix
with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
@@ -113,22 +113,17 @@ class ResourcesTest < Test::Unit::TestCase
end
end
- def test_with_collection_action
- rss_options = {:action => 'rss'}
- rss_path = "/messages/rss"
- actions = { 'a' => :put, 'b' => :post, 'c' => :delete }
+ def test_with_collection_actions
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
- with_restful_routing :messages, :collection => { :rss => :get }.merge(actions) do
+ with_restful_routing :messages, :collection => actions do
assert_restful_routes_for :messages do |options|
- assert_routing rss_path, options.merge(rss_options)
-
actions.each do |action, method|
assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
end
end
assert_restful_named_routes_for :messages do |options|
- assert_named_route rss_path, :rss_messages_path, rss_options
actions.keys.each do |action|
assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
end
@@ -136,6 +131,44 @@ class ResourcesTest < Test::Unit::TestCase
end
end
+ def test_with_collection_actions_and_name_prefix
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
+
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.each do |action, method|
+ assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
+ end
+ end
+
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.keys.each do |action|
+ assert_deprecated_named_route /thread_#{action}_messages/, "/threads/1/messages/#{action}", "thread_#{action}_messages_path", :action => action
+ assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
+ end
+ end
+ end
+ end
+
+ def test_with_collection_action_and_name_prefix_and_formatted
+ actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
+
+ with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.each do |action, method|
+ assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
+ end
+ end
+
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ actions.keys.each do |action|
+ assert_deprecated_named_route /formatted_thread_#{action}_messages/, "/threads/1/messages/#{action}.xml", "formatted_thread_#{action}_messages_path", :action => action, :format => 'xml'
+ assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml'
+ end
+ end
+ end
+ end
+
def test_with_member_action
[:put, :post].each do |method|
with_restful_routing :messages, :member => { :mark => method } do
@@ -183,7 +216,37 @@ class ResourcesTest < Test::Unit::TestCase
end
end
end
+
+ def test_with_new_action_with_name_prefix
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
+ preview_options = {:action => 'preview', :thread_id => '1'}
+ preview_path = "/threads/1/messages/new/preview"
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
+ end
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ assert_deprecated_named_route /thread_preview_new_message/, preview_path, :thread_preview_new_message_path, preview_options
+ assert_named_route preview_path, :preview_new_thread_message_path, preview_options
+ end
+ end
+ end
+
+ def test_with_formatted_new_action_with_name_prefix
+ with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
+ preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
+ preview_path = "/threads/1/messages/new/preview.xml"
+ assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
+ end
+
+ assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
+ assert_deprecated_named_route /formatted_thread_preview_new_message/, preview_path, :formatted_thread_preview_new_message_path, preview_options
+ assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options
+ end
+ end
+ end
+
def test_override_new_method
with_restful_routing :messages do
assert_restful_routes_for :messages do |options|
@@ -244,17 +307,25 @@ class ResourcesTest < Test::Unit::TestCase
end
end
- def test_restful_routes_dont_generate_duplicates
- with_restful_routing :messages do
- routes = ActionController::Routing::Routes.routes
- routes.each do |route|
- routes.each do |r|
- next if route === r # skip the comparison instance
- assert distinct_routes?(route, r), "Duplicate Route: #{route}"
- end
- end
- end
- end
+ # NOTE from dchelimsky: http://dev.rubyonrails.org/ticket/8251 changes some of the
+ # named routes. In order to play nice, I didn't delete the old ones, which means
+ # that this test fails because until the old ones are deprecated and/or removed, there
+ # must be room for two names for the same route.
+ #
+ # From what I can tell this shouldn't matter - all the other
+ # tests in actionpack pass without this one passing. But I could be wrong ;)
+ #
+ # def test_restful_routes_dont_generate_duplicates
+ # with_restful_routing :messages do
+ # routes = ActionController::Routing::Routes.routes
+ # routes.each do |route|
+ # routes.each do |r|
+ # next if route === r # skip the comparison instance
+ # assert distinct_routes?(route, r), "Duplicate Route: #{route}"
+ # end
+ # end
+ # end
+ # end
def test_should_create_singleton_resource_routes
with_singleton_resources :account do
@@ -541,18 +612,25 @@ class ResourcesTest < Test::Unit::TestCase
full_prefix = "/#{options[:path_prefix]}#{controller_name}"
name_prefix = options[:name_prefix]
-
+
assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
- assert_named_route "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options]
- assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
- assert_named_route "#{full_prefix}/1/edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')
assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
- assert_named_route "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')
+ assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
- assert_named_route "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+
+ assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options]
+ assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')
+ assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')
+ assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+
+ assert_named_route "#{full_prefix}/new", "new_#{name_prefix}#{singular_name}_path", options[:options]
+ assert_named_route "#{full_prefix}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml')
+ assert_named_route "#{full_prefix}/1/edit", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
+ assert_named_route "#{full_prefix}/1/edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+
yield options[:options] if block_given?
end
-
+
def assert_singleton_routes_for(singleton_name, options = {})
options[:options] ||= {}
options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
@@ -600,17 +678,39 @@ class ResourcesTest < Test::Unit::TestCase
name_prefix = options[:name_prefix]
assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
- assert_named_route "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path", options[:options]
- assert_named_route "#{full_path}/edit", "#{name_prefix}edit_#{singleton_name}_path", options[:options]
assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
- assert_named_route "#{full_path}/new.xml", "formatted_#{name_prefix}new_#{singleton_name}_path", options[:options].merge(:format => 'xml')
- assert_named_route "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')
+
+ assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singleton_name}/, "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path", options[:options]
+ assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singleton_name}/, "#{full_path}/new.xml", "formatted_#{name_prefix}new_#{singleton_name}_path", options[:options].merge(:format => 'xml')
+ assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit", "#{name_prefix}edit_#{singleton_name}_path", options[:options]
+ assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')
+
+ assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
+ assert_named_route "#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
+ assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
+ assert_named_route "#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
end
def assert_named_route(expected, route, options)
actual = @controller.send(route, options) rescue $!.class.name
assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
end
+
+ def assert_deprecated_named_route(message, expected, route, options)
+ assert_deprecated message do
+ assert_named_route expected, route, options
+ end
+ end
+
+ # These are only deprecated if they have a name_prefix.
+ # See http://dev.rubyonrails.org/ticket/8251
+ def assert_potentially_deprecated_named_route(name_prefix, message, expected, route, options)
+ if name_prefix
+ assert_deprecated_named_route(message, expected, route, options)
+ else
+ assert_named_route expected, route, options
+ end
+ end
def assert_resource_methods(expected, resource, action_method, method)
assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
@@ -628,5 +728,4 @@ class ResourcesTest < Test::Unit::TestCase
end
true
end
-
-end
+end \ No newline at end of file