Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    2727    assert_resource_methods [:new, :preview, :draft], resource, :new,        :get 
    2828  end 
    2929 
     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 
    3047  def test_default_restful_routes 
    3148    with_restful_routing :messages do 
    3249      assert_simply_restful_for :messages 
     
    116133    end 
    117134  end 
    118135 
    119  
    120136  def test_with_new_action 
    121137    with_restful_routing :messages, :new => { :preview => :post } do 
    122138      preview_options = {:action => 'preview'} 
     
    280296    end 
    281297  end 
    282298 
     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 
    283315  protected 
    284316    def with_restful_routing(*args) 
    285317      with_routing do |set| 
     
    311343 
    312344      collection_path = "/#{options[:path_prefix]}#{controller_name}" 
    313345      member_path = "#{collection_path}/1" 
     346      edit_member_path = "#{member_path};edit" 
    314347      new_path = "#{collection_path}/new" 
    315348 
    316349      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' 
    323356      end 
    324357 
    325358      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( 
    326367        options[:options].merge(:action => 'create'), 
    327368        :path => collection_path, :method => :post) 
    328369 
    329370      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( 
    330379        options[:options].merge(:action => 'update', :id => '1'), 
    331380        :path => member_path, :method => :put) 
    332381 
     
    365414 
    366415    def assert_singleton_routes_for(singleton_name, options = {}) 
    367416      (options[:options] ||= {})[:controller] ||= singleton_name.to_s 
    368        
     417 
    369418      full_path = "/#{options[:path_prefix]}#{singleton_name}" 
     419      edit_path = "#{full_path};edit" 
     420      new_path  = "#{full_path}/new" 
     421 
    370422      with_options options[:options] do |controller| 
    371423        controller.assert_routing full_path,          :action => 'show' 
    372424        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' 
    375427      end 
    376428 
     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) 
    377432      assert_recognizes(options[:options].merge(:action => 'create'),  :path => full_path, :method => :post) 
    378433      assert_recognizes(options[:options].merge(:action => 'update'),  :path => full_path, :method => :put) 
    379434      assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete) 
  • lib/action_controller/resources.rb

    old new  
    322322 
    323323      def map_collection_actions(map, resource) 
    324324        resource.collection_methods.each do |method, actions| 
    325           route_options = requirements_for(method) 
     325          route_options = conditions_for(method) 
    326326 
    327327          actions.each do |action| 
    328328            map.named_route( 
    329329              "#{resource.name_prefix}#{action}_#{resource.plural}",  
    330330              "#{resource.path};#{action}",  
    331               route_options.merge(:action => action.to_s) 
     331              action_options(action.to_s, resource).merge(route_options) 
    332332            ) 
    333333 
    334334            map.named_route( 
    335335              "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", 
    336336              "#{resource.path}.:format;#{action}", 
    337               route_options.merge(:action => action.to_s) 
     337              action_options(action.to_s, resource).merge(route_options) 
    338338            ) 
    339339          end 
    340340        end 
    341341      end 
    342342 
    343343      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)
    346346 
    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)
    349349      end 
    350350 
    351351      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)
    354354      end 
    355355 
    356356      def map_new_actions(map, resource) 
    357357        resource.new_methods.each do |method, actions| 
    358           route_options = requirements_for(method) 
     358          route_options = conditions_for(method) 
    359359          actions.each do |action| 
    360360            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)) 
    363363            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)) 
    366366            end 
    367367          end 
    368368        end 
     
    370370       
    371371      def map_member_actions(map, resource) 
    372372        resource.member_methods.each do |method, actions| 
    373           route_options = requirements_for(method) 
     373          route_options = conditions_for(method) 
    374374 
    375375          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)) 
    378378          end 
    379379        end 
    380380 
    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)
    383383 
    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)
    386386 
    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)
    389389      end 
    390390 
    391       def requirements_for(method) 
    392         method == :any ? {} : { :conditions => { :method => method } } 
     391      def conditions_for(method) 
     392        { :conditions => method == :any ? {} : { :method => method } } 
    393393      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 
    394407  end 
    395408end 
    396409