Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add the :path option to match routes when given as symbols. This is s…

…pecially useful in http helpers for generating routes in scenarios like:

resources :users, :path => 'usuarios' do
    get :search, :on => :collection, :path => 'pesquisar'
  end

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 9651ca751c71cb132c47a32291b9647365b617f5 1 parent 61317b6
@carlosantoniodasilva carlosantoniodasilva authored josevalim committed
View
17 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -736,10 +736,10 @@ def match(*args)
return member { match(*args) }
end
- path_names = options.delete(:path_names)
+ path = options.delete(:path)
if args.first.is_a?(Symbol)
- path = path_for_action(args.first, path_names)
+ path = path_for_action(args.first, path)
options = options_for_action(args.first, options)
with_exclusive_scope do
@@ -860,7 +860,7 @@ def member_scope
end
end
- def path_for_action(action, path_names)
+ def path_for_action(action, path)
case action
when :index, :create
"#{@scope[:path]}(.:format)"
@@ -881,12 +881,12 @@ def path_for_action(action, path_names)
else
case @scope[:scope_level]
when :collection, :new
- "#{@scope[:path]}/#{action_path(action)}(.:format)"
+ "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
else
if parent_resource.shallow?
- "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action)}(.:format)"
+ "#{@scope[:shallow_path]}/#{parent_resource.path}/:id/#{action_path(action, path)}(.:format)"
else
- "#{@scope[:path]}/#{action_path(action)}(.:format)"
+ "#{@scope[:path]}/#{action_path(action, path)}(.:format)"
end
end
end
@@ -905,9 +905,8 @@ def path_for_custom_action
end
end
- def action_path(name, path_names = nil)
- path_names ||= @scope[:path_names]
- path_names[name.to_sym] || name.to_s
+ def action_path(name, path = nil)
+ path || @scope[:path_names][name.to_sym] || name.to_s
end
def options_for_action(action, options)
View
21 actionpack/test/dispatch/routing_test.rb
@@ -74,9 +74,12 @@ def self.matches?(request)
scope 'pt', :as => 'pt' do
resources :projects, :path_names => { :edit => 'editar', :new => 'novo' }, :path => 'projetos' do
post :preview, :on => :new
+ put :close, :on => :member, :path => 'fechar'
+ get :open, :on => :new, :path => 'abrir'
end
- resource :admin, :path_names => { :new => 'novo' }, :path => 'administrador' do
+ resource :admin, :path_names => { :new => 'novo', :activate => 'ativar' }, :path => 'administrador' do
post :preview, :on => :new
+ put :activate, :on => :member
end
resources :products, :path_names => { :new => 'novo' } do
new do
@@ -854,6 +857,22 @@ def test_path_names
get '/pt/administrador/novo'
assert_equal 'admins#new', @response.body
assert_equal '/pt/administrador/novo', new_pt_admin_path
+
+ put '/pt/administrador/ativar'
+ assert_equal 'admins#activate', @response.body
+ assert_equal '/pt/administrador/ativar', activate_pt_admin_path
+ end
+ end
+
+ def test_path_option_override
+ with_test_routes do
+ get '/pt/projetos/novo/abrir'
+ assert_equal 'projects#open', @response.body
+ assert_equal '/pt/projetos/novo/abrir', open_new_pt_project_path
+
+ put '/pt/projetos/1/fechar'
+ assert_equal 'projects#close', @response.body
+ assert_equal '/pt/projetos/1/fechar', close_pt_project_path(1)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.