Skip to content
This repository
Browse code

Make sure map.resources(:things, :new => { :new => :any }) correctly …

…sets /things/new to be accessible by any method rather than creating another resource at /things/new;new

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5195 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6a8dcc8a5bd9e8510c75d7a88a1072d88067aaa9 1 parent a0bf019
Jamis Buck authored September 26, 2006
13  actionpack/lib/action_controller/resources.rb
@@ -46,6 +46,7 @@ def arrange_actions
46 46
         
47 47
         def add_default_actions
48 48
           add_default_action(member_methods, :get, :edit)
  49
+          add_default_action(new_methods, :get, :new)
49 50
         end
50 51
 
51 52
         def set_prefixes
@@ -253,13 +254,15 @@ def map_new_actions(map, resource)
253 254
         resource.new_methods.each do |method, actions|
254 255
           route_options = requirements_for(method)
255 256
           actions.each do |action|
256  
-            map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", route_options.merge(:action => action.to_s))
257  
-            map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", route_options.merge(:action => action.to_s))
  257
+            if action == :new
  258
+              map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, route_options.merge(:action => "new"))
  259
+              map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", route_options.merge(:action => "new"))
  260
+            else
  261
+              map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", route_options.merge(:action => action.to_s))
  262
+              map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", route_options.merge(:action => action.to_s))
  263
+            end
258 264
           end
259 265
         end
260  
-
261  
-        map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, :action => "new", :conditions => { :method => :get })
262  
-        map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", :action => "new", :conditions => { :method => :get })
263 266
       end
264 267
       
265 268
       def map_member_actions(map, resource)
28  actionpack/test/controller/resources_test.rb
@@ -17,11 +17,11 @@ def test_should_arrange_actions
17 17
       :member     => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
18 18
       :new        => { :preview => :get, :draft => :get })
19 19
 
20  
-    assert_resource_methods [:rss],               resource, :collection, :get
21  
-    assert_resource_methods [:csv, :reorder],     resource, :collection, :post
22  
-    assert_resource_methods [:edit, :rss, :atom], resource, :member,     :get
23  
-    assert_resource_methods [:upload, :fix],      resource, :member,     :post
24  
-    assert_resource_methods [:preview, :draft],   resource, :new,        :get
  20
+    assert_resource_methods [:rss],                   resource, :collection, :get
  21
+    assert_resource_methods [:csv, :reorder],         resource, :collection, :post
  22
+    assert_resource_methods [:edit, :rss, :atom],     resource, :member,     :get
  23
+    assert_resource_methods [:upload, :fix],          resource, :member,     :post
  24
+    assert_resource_methods [:new, :preview, :draft], resource, :new,        :get
25 25
   end
26 26
 
27 27
   def test_default_restful_routes
@@ -122,6 +122,24 @@ def test_with_new_action
122 122
     end
123 123
   end
124 124
 
  125
+  def test_override_new_method
  126
+    with_restful_routing :messages do
  127
+      assert_restful_routes_for :messages do |options|
  128
+        assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
  129
+        assert_raises(ActionController::RoutingError) do
  130
+          ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
  131
+        end
  132
+      end
  133
+    end
  134
+
  135
+    with_restful_routing :messages, :new => { :new => :any } do
  136
+      assert_restful_routes_for :messages do |options|
  137
+        assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
  138
+        assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
  139
+      end
  140
+    end
  141
+  end
  142
+
125 143
   def test_nested_restful_routes
126 144
     with_routing do |set|
127 145
       set.draw do |map|

0 notes on commit 6a8dcc8

Please sign in to comment.
Something went wrong with that request. Please try again.