Skip to content

Commit

Permalink
When custom resource actions are specified using strings add the defa…
Browse files Browse the repository at this point in the history
…ult name and action if the string is a valid ruby method name.

[#5431 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
pixeltrix authored and josevalim committed Aug 24, 2010
1 parent a028479 commit 2fb0cbe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -736,8 +736,12 @@ def match(*args)
end
elsif resource_method_scope?
path = path_for_custom_action
options[:action] ||= action
options[:as] = name_for_action(options[:as]) if options[:as]
if action =~ /^[a-zA-Z][_a-zA-Z0-9]*$/
options[:action] ||= action
options[:as] = name_for_action(action, options[:as])
else
options[:as] = name_for_action(options[:as]) if options[:as]
end
args.push(options)

with_exclusive_scope do
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -212,11 +212,15 @@ def self.matches?(request)
get "overdue", :to => :overdue, :on => :collection
get "print" => "invoices#print", :as => :print, :on => :member
post "preview" => "invoices#preview", :as => :preview, :on => :new
get "aged/:months", :on => :collection, :action => :aged, :as => :aged
end
resources :notes, :shallow => true do
get "preview" => "notes#preview", :as => :preview, :on => :new
get "print" => "notes#print", :as => :print, :on => :member
end
get "inactive", :on => :collection
post "deactivate", :on => :member
get "old", :on => :collection, :as => :stale
end

namespace :api do
Expand Down Expand Up @@ -2034,6 +2038,24 @@ def test_symbolized_path_parameters_is_not_stale
verify_redirect 'http://www.example.com/countries/all/cities'
end

def test_custom_resource_actions_defined_using_string
get '/customers/inactive'
assert_equal 'customers#inactive', @response.body
assert_equal '/customers/inactive', inactive_customers_path

post '/customers/1/deactivate'
assert_equal 'customers#deactivate', @response.body
assert_equal '/customers/1/deactivate', deactivate_customer_path(:id => '1')

get '/customers/old'
assert_equal 'customers#old', @response.body
assert_equal '/customers/old', stale_customers_path

get '/customers/1/invoices/aged/3'
assert_equal 'invoices#aged', @response.body
assert_equal '/customers/1/invoices/aged/3', aged_customer_invoices_path(:customer_id => '1', :months => '3')
end

private
def with_test_routes
yield
Expand Down

0 comments on commit 2fb0cbe

Please sign in to comment.