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

Ticket #7893: resource_route_exclusions.diff

File resource_route_exclusions.diff, 11.9 kB (added by halorgium, 3 years ago)
  • test/controller/resources_test.rb

    old new  
    5757    end 
    5858  end 
    5959 
     60  def test_default_restful_routes_without_collection_routes 
     61    with_restful_routing :messages, :without => :collection do 
     62      assert_simply_restful_for :messages, :without => :collection 
     63    end 
     64  end 
     65 
     66  def test_default_restful_routes_without_member_routes 
     67    with_restful_routing :messages, :without => :member do 
     68      assert_simply_restful_for :messages, :without => :member 
     69    end 
     70  end 
     71 
    6072  def test_irregular_id_with_no_requirements_should_raise_error 
    6173    expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} 
    6274     
     
    365377 
    366378    def assert_restful_routes_for(controller_name, options = {}) 
    367379      (options[:options] ||= {})[:controller] = controller_name.to_s 
     380      options[:without] ||= [] 
     381      options[:without] = [options[:without]].flatten 
    368382 
    369383      collection_path            = "/#{options[:path_prefix]}#{controller_name}" 
    370384      member_path                = "#{collection_path}/1" 
     
    373387      formatted_edit_member_path = "#{member_path}.xml;edit" 
    374388 
    375389      with_options(options[:options]) do |controller| 
    376         controller.assert_routing collection_path,            :action => 'index' 
    377         controller.assert_routing new_path,                   :action => 'new' 
    378         controller.assert_routing member_path,                :action => 'show', :id => '1' 
    379         controller.assert_routing edit_member_path,           :action => 'edit', :id => '1' 
    380         controller.assert_routing "#{collection_path}.xml",   :action => 'index',            :format => 'xml' 
    381         controller.assert_routing "#{new_path}.xml",          :action => 'new',              :format => 'xml' 
    382         controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml' 
    383         controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml' 
     390        unless options[:without].include?(:collection) 
     391          controller.assert_routing collection_path,            :action => 'index' 
     392          controller.assert_routing new_path,                   :action => 'new' 
     393        end 
     394        unless options[:without].include?(:member) 
     395          controller.assert_routing member_path,                :action => 'show', :id => '1' 
     396          controller.assert_routing edit_member_path,           :action => 'edit', :id => '1' 
     397        end 
     398 
     399        unless options[:without].include?(:collection) 
     400          controller.assert_routing "#{collection_path}.xml",   :action => 'index',            :format => 'xml' 
     401          controller.assert_routing "#{new_path}.xml",          :action => 'new',              :format => 'xml' 
     402        end 
     403        unless options[:without].include?(:member) 
     404          controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml' 
     405          controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml' 
     406        end 
    384407      end 
    385408 
    386       assert_recognizes(options[:options].merge(:action => 'index'),               :path => collection_path,  :method => :get) 
    387       assert_recognizes(options[:options].merge(:action => 'new'),                 :path => new_path,         :method => :get) 
    388       assert_recognizes(options[:options].merge(:action => 'create'),              :path => collection_path,  :method => :post) 
    389       assert_recognizes(options[:options].merge(:action => 'show',    :id => '1'), :path => member_path,      :method => :get) 
    390       assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1'), :path => edit_member_path, :method => :get) 
    391       assert_recognizes(options[:options].merge(:action => 'update',  :id => '1'), :path => member_path,      :method => :put) 
    392       assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path,      :method => :delete) 
     409      unless options[:without].include?(:collection) 
     410        assert_recognizes(options[:options].merge(:action => 'index'),               :path => collection_path,  :method => :get) 
     411        assert_recognizes(options[:options].merge(:action => 'new'),                 :path => new_path,         :method => :get) 
     412        assert_recognizes(options[:options].merge(:action => 'create'),              :path => collection_path,  :method => :post) 
     413      end 
     414      unless options[:without].include?(:member) 
     415        assert_recognizes(options[:options].merge(:action => 'show',    :id => '1'), :path => member_path,      :method => :get) 
     416        assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1'), :path => edit_member_path, :method => :get) 
     417        assert_recognizes(options[:options].merge(:action => 'update',  :id => '1'), :path => member_path,      :method => :put) 
     418        assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path,      :method => :delete) 
     419      end 
    393420 
    394       assert_recognizes(options[:options].merge(:action => 'index',               :format => 'xml'), :path => "#{collection_path}.xml",   :method => :get) 
    395       assert_recognizes(options[:options].merge(:action => 'new',                 :format => 'xml'), :path => "#{new_path}.xml",          :method => :get) 
    396       assert_recognizes(options[:options].merge(:action => 'create',              :format => 'xml'), :path => "#{collection_path}.xml",   :method => :post) 
    397       assert_recognizes(options[:options].merge(:action => 'show',    :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :get) 
    398       assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get) 
    399       assert_recognizes(options[:options].merge(:action => 'update',  :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :put) 
    400       assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :delete) 
     421      unless options[:without].include?(:collection) 
     422        assert_recognizes(options[:options].merge(:action => 'index',               :format => 'xml'), :path => "#{collection_path}.xml",   :method => :get) 
     423        assert_recognizes(options[:options].merge(:action => 'new',                 :format => 'xml'), :path => "#{new_path}.xml",          :method => :get) 
     424        assert_recognizes(options[:options].merge(:action => 'create',              :format => 'xml'), :path => "#{collection_path}.xml",   :method => :post) 
     425      end 
     426      unless options[:without].include?(:member) 
     427        assert_recognizes(options[:options].merge(:action => 'show',    :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :get) 
     428        assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get) 
     429        assert_recognizes(options[:options].merge(:action => 'update',  :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :put) 
     430        assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :delete) 
     431      end 
    401432 
    402433      yield options[:options] if block_given? 
    403434    end 
     
    410441      end 
    411442      singular_name ||= controller_name.to_s.singularize 
    412443      (options[:options] ||= {})[:controller] = controller_name.to_s 
     444      options[:without] ||= [] 
     445      options[:without] = [options[:without]].flatten 
     446 
    413447      @controller = "#{controller_name.to_s.camelize}Controller".constantize.new 
    414448      @request    = ActionController::TestRequest.new 
    415449      @response   = ActionController::TestResponse.new 
    416       get :index, options[:options] 
     450      if options[:without].include?(:collection) 
     451        get :show, options[:options].merge(:id => 1) 
     452      else 
     453        get :index, options[:options] 
     454      end 
    417455      options[:options].delete :action 
    418456 
    419457      full_prefix = "/#{options[:path_prefix]}#{controller_name}" 
    420458      name_prefix = options[:name_prefix] 
    421459 
    422       assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
    423       assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options] 
    424       assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
    425       assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
    426       assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
    427       assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
    428       assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
    429       assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
     460      unless options[:without].include?(:collection) 
     461        assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
     462        assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options] 
     463      end 
     464      unless options[:without].include?(:member) 
     465        assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
     466        assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
     467      end 
     468      unless options[:without].include?(:collection) 
     469        assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
     470        assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
     471      end 
     472      unless options[:without].include?(:member) 
     473        assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
     474        assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
     475      end 
    430476      yield options[:options] if block_given? 
    431477    end 
    432478 
  • lib/action_controller/resources.rb

    old new  
    300300      def map_resource(entities, options = {}, &block) 
    301301        resource = Resource.new(entities, options) 
    302302 
     303        options[:without] ||= [] 
     304        without = [options[:without]].flatten 
    303305        with_options :controller => resource.controller do |map| 
    304           map_collection_actions(map, resource) 
    305           map_default_collection_actions(map, resource) 
    306           map_new_actions(map, resource) 
    307           map_member_actions(map, resource) 
     306          unless without.include?(:collection) 
     307            map_collection_actions(map, resource) 
     308            map_default_collection_actions(map, resource) 
     309            map_new_actions(map, resource) 
     310          end 
     311          map_member_actions(map, resource) unless without.include?(:member) 
    308312 
    309313          if block_given? 
    310314            with_options(:path_prefix => resource.nesting_path_prefix, &block)