Ticket #7633: allow_requirements_option_in_map_resources.diff
| File allow_requirements_option_in_map_resources.diff, 4.1 kB (added by quixoten, 2 years ago) |
|---|
-
lib/action_controller/resources.rb
old new 11 11 @singular = options[:singular] || plural.to_s.singularize 12 12 13 13 @options = options 14 14 15 15 arrange_actions 16 16 add_default_actions 17 17 set_prefixes … … 21 21 @controller ||= (options[:controller] || plural).to_s 22 22 end 23 23 24 def requirements(with_id = false) 25 @requirements ||= @options[:requirements] || {} 26 @id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ } 27 28 with_id ? @requirements.merge(@id_requirement) : @requirements 29 end 30 24 31 def path 25 32 @path ||= "#{path_prefix}/#{plural}" 26 33 end … … 389 396 390 397 def action_options_for(action, resource, method = nil) 391 398 default_options = { :action => action.to_s } 392 require_id = resource.kind_of?(SingletonResource) ? {} : { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } }399 require_id = !resource.kind_of?(SingletonResource) 393 400 case default_options[:action] 394 when "index", "new" : default_options.merge(conditions_for(method || :get)) 395 when "create" : default_options.merge(conditions_for(method || :post)) 396 when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(re quire_id)397 when "update" : default_options.merge(conditions_for(method || :put)).merge(re quire_id)398 when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(re quire_id)399 else default_options.merge(conditions_for(method)) 401 when "index", "new" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements) 402 when "create" : default_options.merge(conditions_for(method || :post)).merge(resource.requirements) 403 when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements(require_id)) 404 when "update" : default_options.merge(conditions_for(method || :put)).merge(resource.requirements(require_id)) 405 when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(resource.requirements(require_id)) 406 else default_options.merge(conditions_for(method)).merge(resource.requirements) 400 407 end 401 408 end 402 409 end -
test/controller/resources_test.rb
old new 57 57 end 58 58 end 59 59 60 def test_irregular_id_with_no_requirements_should_raise_error 61 expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} 62 63 with_restful_routing :messages do 64 assert_raises(ActionController::RoutingError) do 65 assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) 66 end 67 end 68 end 69 70 def test_irregular_id_with_requirements_should_pass 71 expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} 72 73 with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do 74 assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get) 75 end 76 end 77 78 def test_with_path_prefix_requirements 79 expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'} 80 with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do 81 assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get) 82 end 83 end 84 60 85 def test_with_path_prefix 61 86 with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do 62 87 assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }