Ticket #7229: put_and_delete_requires_id.diff
| File put_and_delete_requires_id.diff, 13.2 kB (added by dkubb, 2 years ago) |
|---|
-
test/controller/resources_test.rb
old new 27 27 assert_resource_methods [:new, :preview, :draft], resource, :new, :get 28 28 end 29 29 30 def test_should_resource_controller_name_equal_resource_name_by_default 31 resource = ActionController::Resources::Resource.new(:messages, {}) 32 assert_equal 'messages', resource.controller 33 end 34 35 def test_should_resource_controller_name_equal_controller_option 36 resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts') 37 assert_equal 'posts', resource.controller 38 end 39 40 def test_should_all_singleton_paths_be_the_same 41 [ :path, :nesting_path_prefix, :member_path ].each do |method| 42 resource = ActionController::Resources::SingletonResource.new(:messages, :path_prefix => 'admin') 43 assert_equal 'admin/messages', resource.send(method) 44 end 45 end 46 30 47 def test_default_restful_routes 31 48 with_restful_routing :messages do 32 49 assert_simply_restful_for :messages … … 116 133 end 117 134 end 118 135 119 120 136 def test_with_new_action 121 137 with_restful_routing :messages, :new => { :preview => :post } do 122 138 preview_options = {:action => 'preview'} … … 280 296 end 281 297 end 282 298 299 def test_should_not_allow_delete_or_put_on_collection_path 300 controller_name = :messages 301 with_restful_routing controller_name do 302 options = { :controller => controller_name.to_s } 303 collection_path = "/#{controller_name}" 304 305 assert_raises(ActionController::RoutingError) do 306 assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put) 307 end 308 309 assert_raises(ActionController::RoutingError) do 310 assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete) 311 end 312 end 313 end 314 283 315 protected 284 316 def with_restful_routing(*args) 285 317 with_routing do |set| … … 311 343 312 344 collection_path = "/#{options[:path_prefix]}#{controller_name}" 313 345 member_path = "#{collection_path}/1" 346 edit_member_path = "#{member_path};edit" 314 347 new_path = "#{collection_path}/new" 315 348 316 349 with_options(options[:options]) do |controller| 317 controller.assert_routing collection_path, :action => 'index'318 controller.assert_routing "#{collection_path}.xml" , :action => 'index', :format => 'xml'319 controller.assert_routing new_path, :action => 'new'320 controller.assert_routing member_path, :action => 'show', :id => '1'321 controller.assert_routing "#{member_path};edit",:action => 'edit', :id => '1'322 controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'350 controller.assert_routing collection_path, :action => 'index' 351 controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml' 352 controller.assert_routing new_path, :action => 'new' 353 controller.assert_routing member_path, :action => 'show', :id => '1' 354 controller.assert_routing edit_member_path, :action => 'edit', :id => '1' 355 controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml' 323 356 end 324 357 325 358 assert_recognizes( 359 options[:options].merge(:action => 'index'), 360 :path => collection_path, :method => :get) 361 362 assert_recognizes( 363 options[:options].merge(:action => 'new'), 364 :path => new_path, :method => :get) 365 366 assert_recognizes( 326 367 options[:options].merge(:action => 'create'), 327 368 :path => collection_path, :method => :post) 328 369 329 370 assert_recognizes( 371 options[:options].merge(:action => 'show', :id => '1'), 372 :path => member_path, :method => :get) 373 374 assert_recognizes( 375 options[:options].merge(:action => 'edit', :id => '1'), 376 :path => edit_member_path, :method => :get) 377 378 assert_recognizes( 330 379 options[:options].merge(:action => 'update', :id => '1'), 331 380 :path => member_path, :method => :put) 332 381 … … 365 414 366 415 def assert_singleton_routes_for(singleton_name, options = {}) 367 416 (options[:options] ||= {})[:controller] ||= singleton_name.to_s 368 417 369 418 full_path = "/#{options[:path_prefix]}#{singleton_name}" 419 edit_path = "#{full_path};edit" 420 new_path = "#{full_path}/new" 421 370 422 with_options options[:options] do |controller| 371 423 controller.assert_routing full_path, :action => 'show' 372 424 controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml' 373 controller.assert_routing "#{full_path}/new",:action => 'new'374 controller.assert_routing "#{full_path};edit",:action => 'edit'425 controller.assert_routing new_path, :action => 'new' 426 controller.assert_routing edit_path, :action => 'edit' 375 427 end 376 428 429 assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get) 430 assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get) 431 assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get) 377 432 assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post) 378 433 assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put) 379 434 assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete) -
lib/action_controller/resources.rb
old new 322 322 323 323 def map_collection_actions(map, resource) 324 324 resource.collection_methods.each do |method, actions| 325 route_options = requirements_for(method)325 route_options = conditions_for(method) 326 326 327 327 actions.each do |action| 328 328 map.named_route( 329 329 "#{resource.name_prefix}#{action}_#{resource.plural}", 330 330 "#{resource.path};#{action}", 331 route_options.merge(:action => action.to_s)331 action_options(action.to_s, resource).merge(route_options) 332 332 ) 333 333 334 334 map.named_route( 335 335 "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", 336 336 "#{resource.path}.:format;#{action}", 337 route_options.merge(:action => action.to_s)337 action_options(action.to_s, resource).merge(route_options) 338 338 ) 339 339 end 340 340 end 341 341 end 342 342 343 343 def map_default_collection_actions(map, resource) 344 map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, :action => "index", :conditions => { :method => :get })345 map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", :action => "index", :conditions => { :method => :get })344 map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, action_options("index", resource)) 345 map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", action_options("index", resource)) 346 346 347 map.connect(resource.path, :action => "create", :conditions => { :method => :post })348 map.connect("#{resource.path}.:format", :action => "create", :conditions => { :method => :post })347 map.connect(resource.path, action_options("create", resource)) 348 map.connect("#{resource.path}.:format", action_options("create", resource)) 349 349 end 350 350 351 351 def map_default_singleton_actions(map, resource) 352 map.connect(resource.path, :action => "create", :conditions => { :method => :post })353 map.connect("#{resource.path}.:format", :action => "create", :conditions => { :method => :post })352 map.connect(resource.path, action_options("create", resource)) 353 map.connect("#{resource.path}.:format", action_options("create", resource)) 354 354 end 355 355 356 356 def map_new_actions(map, resource) 357 357 resource.new_methods.each do |method, actions| 358 route_options = requirements_for(method)358 route_options = conditions_for(method) 359 359 actions.each do |action| 360 360 if action == :new 361 map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, route_options.merge(:action => "new"))362 map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", route_options.merge(:action => "new"))361 map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options("new", resource).merge(route_options)) 362 map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options("new", resource).merge(route_options)) 363 363 else 364 map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", route_options.merge(:action => action.to_s))365 map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", route_options.merge(:action => action.to_s))364 map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options(action.to_s, resource).merge(route_options)) 365 map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options(action.to_s, resource).merge(route_options)) 366 366 end 367 367 end 368 368 end … … 370 370 371 371 def map_member_actions(map, resource) 372 372 resource.member_methods.each do |method, actions| 373 route_options = requirements_for(method)373 route_options = conditions_for(method) 374 374 375 375 actions.each do |action| 376 map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", route_options.merge(:action => action.to_s))377 map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}", route_options.merge(:action => action.to_s))376 map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options(action.to_s, resource).merge(route_options)) 377 map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}", action_options(action.to_s, resource).merge(route_options)) 378 378 end 379 379 end 380 380 381 map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, :action => "show", :conditions => { :method => :get })382 map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", :action => "show", :conditions => { :method => :get })381 map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, action_options("show", resource)) 382 map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", action_options("show", resource)) 383 383 384 map.connect(resource.member_path, :action => "update", :conditions => { :method => :put })385 map.connect("#{resource.member_path}.:format", :action => "update", :conditions => { :method => :put })384 map.connect(resource.member_path, action_options("update", resource)) 385 map.connect("#{resource.member_path}.:format", action_options("update", resource)) 386 386 387 map.connect(resource.member_path, :action => "destroy", :conditions => { :method => :delete })388 map.connect("#{resource.member_path}.:format", :action => "destroy", :conditions => { :method => :delete })387 map.connect(resource.member_path, action_options("destroy", resource)) 388 map.connect("#{resource.member_path}.:format", action_options("destroy", resource)) 389 389 end 390 390 391 def requirements_for(method)392 method == :any ? {} : { :conditions =>{ :method => method } }391 def conditions_for(method) 392 { :conditions => method == :any ? {} : { :method => method } } 393 393 end 394 395 def action_options(action, resource) 396 default_options = { :action => action } 397 require_id = resource.kind_of?(SingletonResource) ? {} : { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } } 398 case action 399 when "index", "new" : default_options.merge(conditions_for(:get)) 400 when "create" : default_options.merge(conditions_for(:post)) 401 when "show", "edit" : default_options.merge(conditions_for(:get)).merge(require_id) 402 when "update" : default_options.merge(conditions_for(:put)).merge(require_id) 403 when "destroy" : default_options.merge(conditions_for(:delete)).merge(require_id) 404 else default_options 405 end 406 end 394 407 end 395 408 end 396 409