1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow routing requirements on map.resource(s) (closes #7633) [quixoten]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6232 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2007-02-25 20:13:19 +00:00
parent b203b9b55b
commit 784298008b
3 changed files with 44 additions and 8 deletions

View file

@ -1,5 +1,9 @@
*SVN* *SVN*
* Allow routing requirements on map.resource(s) #7633 [quixoten]. Example:
map.resources :network_interfaces, :requirements => { :id => /^\d+\.\d+\.\d+\.\d+$/ }
* Cookie session store: empty and unchanged sessions don't write a cookie. [Jeremy Kemper] * Cookie session store: empty and unchanged sessions don't write a cookie. [Jeremy Kemper]
* Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH] * Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH]

View file

@ -21,6 +21,13 @@ module ActionController
@controller ||= (options[:controller] || plural).to_s @controller ||= (options[:controller] || plural).to_s
end end
def requirements(with_id = false)
@requirements ||= @options[:requirements] || {}
@id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ }
with_id ? @requirements.merge(@id_requirement) : @requirements
end
def path def path
@path ||= "#{path_prefix}/#{plural}" @path ||= "#{path_prefix}/#{plural}"
end end
@ -389,14 +396,14 @@ module ActionController
def action_options_for(action, resource, method = nil) def action_options_for(action, resource, method = nil)
default_options = { :action => action.to_s } default_options = { :action => action.to_s }
require_id = resource.kind_of?(SingletonResource) ? {} : { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } } require_id = !resource.kind_of?(SingletonResource)
case default_options[:action] case default_options[:action]
when "index", "new" : default_options.merge(conditions_for(method || :get)) when "index", "new" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements)
when "create" : default_options.merge(conditions_for(method || :post)) when "create" : default_options.merge(conditions_for(method || :post)).merge(resource.requirements)
when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(require_id) when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements(require_id))
when "update" : default_options.merge(conditions_for(method || :put)).merge(require_id) when "update" : default_options.merge(conditions_for(method || :put)).merge(resource.requirements(require_id))
when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(require_id) when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(resource.requirements(require_id))
else default_options.merge(conditions_for(method)) else default_options.merge(conditions_for(method)).merge(resource.requirements)
end end
end end
end end

View file

@ -57,6 +57,31 @@ class ResourcesTest < Test::Unit::TestCase
end end
end end
def test_irregular_id_with_no_requirements_should_raise_error
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
with_restful_routing :messages do
assert_raises(ActionController::RoutingError) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end
end
end
def test_irregular_id_with_requirements_should_pass
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end
end
def test_with_path_prefix_requirements
expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
end
end
def test_with_path_prefix def test_with_path_prefix
with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }