Report abuse

config/routes.rb

map.resources :players do |player|
  player.resources :events, :name_prefix => 'player_', :controller => 'player_events', :without => :member
end

map.resources :player_events, :without => :collection

Generated routes for the PlayerEvent resource

http://halorgium.net/resource_route_exclusions_routes.html

The diff (diff)

Index: test/controller/resources_test.rb
===================================================================
--- test/controller/resources_test.rb	(revision 8)
+++ test/controller/resources_test.rb	(working copy)
@@ -57,6 +57,18 @@
     end
   end

+  def test_default_restful_routes_without_collection_routes
+    with_restful_routing :messages, :without => :collection do
+      assert_simply_restful_for :messages, :without => :collection
+    end
+  end
+
+  def test_default_restful_routes_without_member_routes
+    with_restful_routing :messages, :without => :member do
+      assert_simply_restful_for :messages, :without => :member
+    end
+  end
+
   def test_irregular_id_with_no_requirements_should_raise_error
     expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}

@@ -365,6 +377,8 @@

     def assert_restful_routes_for(controller_name, options = {})
       (options[:options] ||= {})[:controller] = controller_name.to_s
+      options[:without] ||= []
+      options[:without] = [options[:without]].flatten

       collection_path            = "/#{options[:path_prefix]}#{controller_name}"
       member_path                = "#{collection_path}/1"
@@ -373,31 +387,48 @@
       formatted_edit_member_path = "#{member_path}.xml;edit"

       with_options(options[:options]) do |controller|
-        controller.assert_routing collection_path,            :action => 'index'
-        controller.assert_routing new_path,                   :action => 'new'
-        controller.assert_routing member_path,                :action => 'show', :id => '1'
-        controller.assert_routing edit_member_path,           :action => 'edit', :id => '1'
-        controller.assert_routing "#{collection_path}.xml",   :action => 'index',            :format => 'xml'
-        controller.assert_routing "#{new_path}.xml",          :action => 'new',              :format => 'xml'
-        controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml'
-        controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
+        unless options[:without].include?(:collection)
+          controller.assert_routing collection_path,            :action => 'index'
+          controller.assert_routing new_path,                   :action => 'new'
+        end
+        unless options[:without].include?(:member)
+          controller.assert_routing member_path,                :action => 'show', :id => '1'
+          controller.assert_routing edit_member_path,           :action => 'edit', :id => '1'
+        end
+
+        unless options[:without].include?(:collection)
+          controller.assert_routing "#{collection_path}.xml",   :action => 'index',            :format => 'xml'
+          controller.assert_routing "#{new_path}.xml",          :action => 'new',              :format => 'xml'
+        end
+        unless options[:without].include?(:member)
+          controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml'
+          controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
+        end
       end

-      assert_recognizes(options[:options].merge(:action => 'index'),               :path => collection_path,  :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'new'),                 :path => new_path,         :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'create'),              :path => collection_path,  :method => :post)
-      assert_recognizes(options[:options].merge(:action => 'show',    :id => '1'), :path => member_path,      :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1'), :path => edit_member_path, :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'update',  :id => '1'), :path => member_path,      :method => :put)
-      assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path,      :method => :delete)
+      unless options[:without].include?(:collection)
+        assert_recognizes(options[:options].merge(:action => 'index'),               :path => collection_path,  :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'new'),                 :path => new_path,         :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'create'),              :path => collection_path,  :method => :post)
+      end
+      unless options[:without].include?(:member)
+        assert_recognizes(options[:options].merge(:action => 'show',    :id => '1'), :path => member_path,      :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1'), :path => edit_member_path, :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'update',  :id => '1'), :path => member_path,      :method => :put)
+        assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path,      :method => :delete)
+      end

-      assert_recognizes(options[:options].merge(:action => 'index',               :format => 'xml'), :path => "#{collection_path}.xml",   :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'new',                 :format => 'xml'), :path => "#{new_path}.xml",          :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'create',              :format => 'xml'), :path => "#{collection_path}.xml",   :method => :post)
-      assert_recognizes(options[:options].merge(:action => 'show',    :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
-      assert_recognizes(options[:options].merge(:action => 'update',  :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :put)
-      assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :delete)
+      unless options[:without].include?(:collection)
+        assert_recognizes(options[:options].merge(:action => 'index',               :format => 'xml'), :path => "#{collection_path}.xml",   :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'new',                 :format => 'xml'), :path => "#{new_path}.xml",          :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'create',              :format => 'xml'), :path => "#{collection_path}.xml",   :method => :post)
+      end
+      unless options[:without].include?(:member)
+        assert_recognizes(options[:options].merge(:action => 'show',    :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
+        assert_recognizes(options[:options].merge(:action => 'update',  :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :put)
+        assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :delete)
+      end

       yield options[:options] if block_given?
     end
@@ -410,23 +441,38 @@
       end
       singular_name ||= controller_name.to_s.singularize
       (options[:options] ||= {})[:controller] = controller_name.to_s
+      options[:without] ||= []
+      options[:without] = [options[:without]].flatten
+
       @controller = "#{controller_name.to_s.camelize}Controller".constantize.new
       @request    = ActionController::TestRequest.new
       @response   = ActionController::TestResponse.new
-      get :index, options[:options]
+      if options[:without].include?(:collection)
+        get :show, options[:options].merge(:id => 1)
+      else
+        get :index, options[:options]
+      end
       options[:options].delete :action

       full_prefix = "/#{options[:path_prefix]}#{controller_name}"
       name_prefix = options[:name_prefix]

-      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options]
-      assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options]
-      assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1')
-      assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1')
-      assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml')
-      assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml')
-      assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml')
-      assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+      unless options[:without].include?(:collection)
+        assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options]
+        assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options]
+      end
+      unless options[:without].include?(:member)
+        assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1')
+        assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1')
+      end
+      unless options[:without].include?(:collection)
+        assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml')
+        assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml')
+      end
+      unless options[:without].include?(:member)
+        assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml')
+        assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
+      end
       yield options[:options] if block_given?
     end

Index: lib/action_controller/resources.rb
===================================================================
--- lib/action_controller/resources.rb	(revision 8)
+++ lib/action_controller/resources.rb	(working copy)
@@ -300,11 +300,15 @@
       def map_resource(entities, options = {}, &block)
         resource = Resource.new(entities, options)

+        options[:without] ||= []
+        without = [options[:without]].flatten
         with_options :controller => resource.controller do |map|
-          map_collection_actions(map, resource)
-          map_default_collection_actions(map, resource)
-          map_new_actions(map, resource)
-          map_member_actions(map, resource)
+          unless without.include?(:collection)
+            map_collection_actions(map, resource)
+            map_default_collection_actions(map, resource)
+            map_new_actions(map, resource)
+          end
+          map_member_actions(map, resource) unless without.include?(:member)

           if block_given?
             with_options(:path_prefix => resource.nesting_path_prefix, &block)