Permalink
Browse files

Added new new_admin_products_url named route in addition to old admin…

…_new_products_url syntax. latter syntax got a deprecation warning [pixeltrix]

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@7414 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 19dff14 commit 3acd2bd7210dd1164a8f07cd4667032d5d5a392a Tobias Lütke committed Sep 6, 2007
@@ -292,6 +292,10 @@ class Base
# Turn on +ignore_missing_templates+ if you want to unit test actions without making the associated templates.
cattr_accessor :ignore_missing_templates
+ # Controls the resource action separator
+ @@resource_action_separator = ";"
+ cattr_accessor :resource_action_separator
+
# Holds the request object that's primarily used to get environment variables through access like
# <tt>request.env["REQUEST_URI"]</tt>.
attr_internal :request
@@ -16,27 +16,31 @@ def initialize(entities, options)
add_default_actions
set_prefixes
end
-
+
def controller
@controller ||= (options[:controller] || plural).to_s
end
-
+
def path
@path ||= "#{path_prefix}/#{plural}"
end
-
+
def new_path
@new_path ||= "#{path}/new"
end
-
+
def member_path
@member_path ||= "#{path}/:id"
end
-
+
def nesting_path_prefix
@nesting_path_prefix ||= "#{path}/:#{singular}_id"
end
-
+
+ def action_separator
+ @action_separator ||= Base.resource_action_separator
+ end
+
protected
def arrange_actions
@collection_methods = arrange_actions_by_methods(options.delete(:collection))
@@ -324,8 +328,14 @@ def map_collection_actions(map, resource)
resource.collection_methods.each do |method, actions|
actions.each do |action|
action_options = action_options_for(action, resource, method)
- map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path};#{action}", action_options)
- map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}.:format;#{action}", action_options)
+
+ unless resource.name_prefix.blank?
+ map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.name_prefix}#{action}_#{resource.plural}")
+ map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.name_prefix}#{action}_#{resource.plural}")
+ end
+
+ map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options)
+ map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options)
end
end
end
@@ -351,11 +361,25 @@ def map_new_actions(map, resource)
actions.each do |action|
action_options = action_options_for(action, resource, method)
if action == :new
- map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options)
- map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options)
+
+ unless resource.name_prefix.blank?
+ map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}new_#{resource.singular}")
+ map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}new_#{resource.singular}")
+ end
+
+ map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options)
+ map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options)
+
else
- map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options)
- map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options)
+
+ unless resource.name_prefix.blank?
+ map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_new_#{resource.singular}")
+ map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}")
+ end
+
+ map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options)
+ map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options)
+
end
end
end
@@ -365,8 +389,15 @@ def map_member_actions(map, resource)
resource.member_methods.each do |method, actions|
actions.each do |action|
action_options = action_options_for(action, resource, method)
- map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options)
- map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}",action_options)
+
+ unless resource.name_prefix.blank?
+ map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.name_prefix}#{action}_#{resource.singular}")
+ map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.name_prefix}#{action}_#{resource.singular}")
+ end
+
+ map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options)
+ map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format", action_options)
+
end
end
@@ -989,6 +989,10 @@ def connect(path, options = {})
def named_route(name, path, options = {})
@set.add_named_route(name, path, options)
end
+
+ def deprecated_named_route(name, deprecated_name, options = {})
+ @set.add_deprecated_named_route(name, deprecated_name)
+ end
# Added deprecation notice for anyone who already added a named route called "root".
# It'll be used as a shortcut for map.connect '' in Rails 2.0.
@@ -1019,7 +1023,7 @@ def initialize
def clear!
@routes = {}
@helpers = []
-
+
@module ||= Module.new
@module.instance_methods.each do |selector|
@module.send :remove_method, selector
@@ -1055,6 +1059,38 @@ def length
def install(destinations = [ActionController::Base, ActionView::Base])
Array(destinations).each { |dest| dest.send :include, @module }
end
+
+ def define_deprecated_named_route_methods(name, deprecated_name)
+
+ [:url, :path].each do |kind|
+ @module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
+
+ def #{url_helper_name(deprecated_name, kind)}(*args)
+
+ ActiveSupport::Deprecation.warn(
+ 'The named route "#{url_helper_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
+ 'You should use "#{url_helper_name(name, kind)}" instead.', caller
+ )
+
+ send :#{url_helper_name(name, kind)}, *args
+
+ end
+
+ def #{hash_access_name(deprecated_name, kind)}(*args)
+
+ ActiveSupport::Deprecation.warn(
+ 'The named route "#{hash_access_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
+ 'You should use "#{hash_access_name(name, kind)}" instead.', caller
+ )
+
+ send :#{hash_access_name(name, kind)}, *args
+
+ end
+
+ end_eval
+ end
+
+ end
private
def url_helper_name(name, kind = :url)
@@ -1177,6 +1213,10 @@ def add_route(path, options = {})
def add_named_route(name, path, options = {})
named_routes[name] = add_route(path, options)
end
+
+ def add_deprecated_named_route(name, deprecated_name)
+ named_routes.define_deprecated_named_route_methods(name, deprecated_name)
+ end
def options_as_params(options)
# If an explicit :controller was given, always make :action explicit
@@ -63,7 +63,7 @@ def test_with_path_prefix
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' }
@@ -312,6 +312,78 @@ def test_should_not_allow_delete_or_put_on_collection_path
end
end
+ def test_resource_action_separator
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
+ end
+
+ action_separator = ActionController::Base.resource_action_separator
+
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
+ assert_named_route "/threads/1/messages#{action_separator}search", "search_thread_messages_path", {}
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
+ assert_named_route "/threads/1/messages/new#{action_separator}preview", "preview_new_thread_message_path", {}
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
+ assert_named_route "/admin/account#{action_separator}login", "login_admin_account_path", {}
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
+ assert_named_route "/admin/account/new#{action_separator}preview", "preview_new_admin_account_path", {}
+ end
+ end
+
+ def test_new_style_named_routes_for_resource
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
+ end
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
+ assert_named_route "/threads/1/messages;search", "search_thread_messages_path", {}
+ assert_named_route "/threads/1/messages/new", "new_thread_message_path", {}
+ assert_named_route "/threads/1/messages/new;preview", "preview_new_thread_message_path", {}
+ end
+ end
+
+ def test_new_style_named_routes_for_singleton_resource
+ with_routing do |set|
+ set.draw do |map|
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
+ end
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
+ assert_named_route "/admin/account;login", "login_admin_account_path", {}
+ assert_named_route "/admin/account/new", "new_admin_account_path", {}
+ assert_named_route "/admin/account/new;preview", "preview_new_admin_account_path", {}
+ end
+ end
+
+ def test_should_add_deprecated_named_routes_for_resource
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :messages, :collection => {:search => :get}, :new => {:preview => :any}, :name_prefix => 'thread_', :path_prefix => '/threads/:thread_id'
+ end
+ assert_simply_restful_for :messages, :name_prefix => 'thread_', :path_prefix => 'threads/1/', :options => { :thread_id => '1' }
+ assert_deprecated do
+ assert_named_route "/threads/1/messages;search", "thread_search_messages_path", {}
+ assert_named_route "/threads/1/messages/new", "thread_new_message_path", {}
+ assert_named_route "/threads/1/messages/new;preview", "thread_preview_new_message_path", {}
+ end
+ end
+ end
+
+ def test_should_add_deprecated_named_routes_for_singleton_resource
+ with_routing do |set|
+ set.draw do |map|
+ map.resource :account, :member => {:login => :get}, :new => {:preview => :any}, :name_prefix => 'admin_', :path_prefix => '/admin'
+ end
+ assert_singleton_restful_for :account, :name_prefix => 'admin_', :path_prefix => 'admin/'
+ assert_deprecated do
+ assert_named_route "/admin/account;login", "admin_login_account_path", {}
+ assert_named_route "/admin/account/new", "admin_new_account_path", {}
+ assert_named_route "/admin/account/new;preview", "admin_preview_new_account_path", {}
+ end
+ end
+ end
+
protected
def with_restful_routing(*args)
with_routing do |set|
@@ -345,7 +417,7 @@ def assert_restful_routes_for(controller_name, options = {})
member_path = "#{collection_path}/1"
new_path = "#{collection_path}/new"
edit_member_path = "#{member_path};edit"
- formatted_edit_member_path = "#{member_path}.xml;edit"
+ formatted_edit_member_path = "#{member_path};edit.xml"
with_options(options[:options]) do |controller|
controller.assert_routing collection_path, :action => 'index'
@@ -395,13 +467,13 @@ def assert_restful_named_routes_for(controller_name, singular_name = nil, option
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}/new", "new_#{name_prefix}#{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}/1;edit", "edit_#{name_prefix}#{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}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml')
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.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+ 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
@@ -411,7 +483,7 @@ def assert_singleton_routes_for(singleton_name, options = {})
full_path = "/#{options[:path_prefix]}#{singleton_name}"
new_path = "#{full_path}/new"
edit_path = "#{full_path};edit"
- formatted_edit_path = "#{full_path}.xml;edit"
+ formatted_edit_path = "#{full_path};edit.xml"
with_options options[:options] do |controller|
controller.assert_routing full_path, :action => 'show'
@@ -448,13 +520,14 @@ def assert_singleton_named_routes_for(singleton_name, options = {})
options[:options].delete :action
full_path = "/#{options[:path_prefix]}#{singleton_name}"
-
- assert_named_route "#{full_path}", "#{singleton_name}_path", options[:options]
- assert_named_route "#{full_path}/new", "new_#{singleton_name}_path", options[:options]
- assert_named_route "#{full_path};edit", "edit_#{singleton_name}_path", options[:options]
- assert_named_route "#{full_path}.xml", "formatted_#{singleton_name}_path", options[:options].merge(:format => 'xml')
- assert_named_route "#{full_path}/new.xml", "formatted_new_#{singleton_name}_path", options[:options].merge(:format => 'xml')
- assert_named_route "#{full_path}.xml;edit", "formatted_edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')
+ full_name = "#{options[:name_prefix]}#{singleton_name}"
+
+ assert_named_route "#{full_path}", "#{full_name}_path", options[:options]
+ assert_named_route "#{full_path}/new", "new_#{full_name}_path", options[:options]
+ assert_named_route "#{full_path};edit", "edit_#{full_name}_path", options[:options]
+ assert_named_route "#{full_path}.xml", "formatted_#{full_name}_path", options[:options].merge(:format => 'xml')
+ assert_named_route "#{full_path}/new.xml", "formatted_new_#{full_name}_path", options[:options].merge(:format => 'xml')
+ assert_named_route "#{full_path};edit.xml", "formatted_edit_#{full_name}_path", options[:options].merge(:format => 'xml')
end
def assert_named_route(expected, route, options)

0 comments on commit 3acd2bd

Please sign in to comment.