From 06eb36e308c54b8860254b609189b84eae6b52a1 Mon Sep 17 00:00:00 2001 From: Ruy Asan Date: Thu, 5 Jun 2008 16:52:08 -0700 Subject: [PATCH] Updated patch from http://dev.rubyonrails.org/ticket/7633 (again) for newest rails --- actionpack/lib/action_controller/resources.rb | 15 +++++++-------- actionpack/test/controller/resources_test.rb | 8 +++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index 9fb1f9f..e78e672 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -68,7 +68,7 @@ module ActionController @requirements ||= @options[:requirements] || {} @id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ } - with_id ? @requirements.merge(@id_requirement) : @requirements + (with_id && !self.kind_of?(SingletonResource)) ? @requirements.merge(@id_requirement) : @requirements end def conditions @@ -523,7 +523,7 @@ module ActionController 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) + action_options = action_options_for(action, resource, method, true) action_path = resource.options[:path_names][action] if resource.options[:path_names].is_a?(Hash) action_path ||= Base.resources_path_names[action] || action @@ -533,15 +533,15 @@ module ActionController end end - show_action_options = action_options_for("show", resource) + show_action_options = action_options_for("show", resource, nil, true) map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, show_action_options) map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", show_action_options) - update_action_options = action_options_for("update", resource) + update_action_options = action_options_for("update", resource, nil, true) map.connect(resource.member_path, update_action_options) map.connect("#{resource.member_path}.:format", update_action_options) - destroy_action_options = action_options_for("destroy", resource) + destroy_action_options = action_options_for("destroy", resource, nil, true) map.connect(resource.member_path, destroy_action_options) map.connect("#{resource.member_path}.:format", destroy_action_options) end @@ -552,16 +552,15 @@ module ActionController end end - def action_options_for(action, resource, method = nil) + def action_options_for(action, resource, method = nil, require_id = false) default_options = { :action => action.to_s } - require_id = !resource.kind_of?(SingletonResource) case default_options[:action] when "index", "new"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements) when "create"; default_options.merge(add_conditions_for(resource.conditions, method || :post)).merge(resource.requirements) when "show", "edit"; default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(require_id)) when "update"; default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(require_id)) when "destroy"; default_options.merge(add_conditions_for(resource.conditions, method || :delete)).merge(resource.requirements(require_id)) - else default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements) + else default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements(require_id)) end end end diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 0d089d0..10d2808 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -112,7 +112,13 @@ class ResourcesTest < Test::Unit::TestCase assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) end end - + def test_irregular_id_requirements_should_get_passed_to_member_actions + expected_options = {:controller => 'messages', :action => 'custom', :id => '1.1.1'} + + with_restful_routing(:messages, :member => {:custom => :get}, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do + assert_recognizes(expected_options, :path => 'messages/1.1.1/custom', :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 -- 1.5.5.1