public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix map.resources to always generate named routes if they're needed

Signed-off-by: Michael Koziarski <michael@koziarski.com>
Tom Stuart (author)
Thu Nov 13 06:31:36 -0800 2008
NZKoz (committer)
Thu Nov 13 08:49:09 -0800 2008
commit  4c0921024471c0463d67f8b8fb6a115a94d343aa
tree    c0611632d1d4cf43934378dd6113ab381d6cdb6b
parent  57d795bad43d4a3e5eef7151099a8e40808a1031
...
597
598
599
600
 
601
602
603
604
 
605
606
607
...
632
633
634
635
636
637
 
 
 
 
638
639
640
...
642
643
644
645
 
646
647
648
...
597
598
599
 
600
601
602
603
 
604
605
606
607
...
632
633
634
 
 
 
635
636
637
638
639
640
641
...
643
644
645
 
646
647
648
649
0
@@ -597,11 +597,11 @@ module ActionController
0
         end
0
 
0
         map_resource_routes(map, resource, :index, resource.path, index_route_name)
0
-        map_resource_routes(map, resource, :create, resource.path)
0
+        map_resource_routes(map, resource, :create, resource.path, index_route_name)
0
       end
0
 
0
       def map_default_singleton_actions(map, resource)
0
-        map_resource_routes(map, resource, :create, resource.path)
0
+        map_resource_routes(map, resource, :create, resource.path, "#{resource.shallow_name_prefix}#{resource.singular}")
0
       end
0
 
0
       def map_new_actions(map, resource)
0
@@ -632,9 +632,10 @@ module ActionController
0
           end
0
         end
0
 
0
-        map_resource_routes(map, resource, :show, resource.member_path, "#{resource.shallow_name_prefix}#{resource.singular}")
0
-        map_resource_routes(map, resource, :update, resource.member_path)
0
-        map_resource_routes(map, resource, :destroy, resource.member_path)
0
+        route_path = "#{resource.shallow_name_prefix}#{resource.singular}"
0
+        map_resource_routes(map, resource, :show, resource.member_path, route_path)
0
+        map_resource_routes(map, resource, :update, resource.member_path, route_path)
0
+        map_resource_routes(map, resource, :destroy, resource.member_path, route_path)
0
       end
0
 
0
       def map_resource_routes(map, resource, action, route_path, route_name = nil, method = nil)
0
@@ -642,7 +643,7 @@ module ActionController
0
           action_options = action_options_for(action, resource, method)
0
           formatted_route_path = "#{route_path}.:format"
0
 
0
-          if route_name
1
+          if route_name && @set.named_routes[route_name.to_sym].nil?
0
             map.named_route(route_name, route_path, action_options)
0
             map.named_route("formatted_#{route_name}", formatted_route_path, action_options)
0
           else
...
822
823
824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
825
826
827
...
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
0
@@ -822,6 +822,84 @@ class ResourcesTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+  def test_resource_has_only_create_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resources :products, :only => :create
0
+      end
0
+
0
+      assert_resource_allowed_routes('products', {},                    { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
0
+      assert_resource_allowed_routes('products', { :format => 'xml' },  { :id => '1' }, :create, [:index, :new, :show, :edit, :update, :destroy])
0
+
0
+      assert_not_nil set.named_routes[:products]
0
+    end
0
+  end
0
+
0
+  def test_resource_has_only_update_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resources :products, :only => :update
0
+      end
0
+
0
+      assert_resource_allowed_routes('products', {},                    { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
0
+      assert_resource_allowed_routes('products', { :format => 'xml' },  { :id => '1' }, :update, [:index, :new, :create, :show, :edit, :destroy])
0
+
0
+      assert_not_nil set.named_routes[:product]
0
+    end
0
+  end
0
+
0
+  def test_resource_has_only_destroy_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resources :products, :only => :destroy
0
+      end
0
+
0
+      assert_resource_allowed_routes('products', {},                    { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
0
+      assert_resource_allowed_routes('products', { :format => 'xml' },  { :id => '1' }, :destroy, [:index, :new, :create, :show, :edit, :update])
0
+
0
+      assert_not_nil set.named_routes[:product]
0
+    end
0
+  end
0
+
0
+  def test_singleton_resource_has_only_create_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resource :account, :only => :create
0
+      end
0
+
0
+      assert_singleton_resource_allowed_routes('accounts', {},                    :create, [:new, :show, :edit, :update, :destroy])
0
+      assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' },  :create, [:new, :show, :edit, :update, :destroy])
0
+
0
+      assert_not_nil set.named_routes[:account]
0
+    end
0
+  end
0
+
0
+  def test_singleton_resource_has_only_update_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resource :account, :only => :update
0
+      end
0
+
0
+      assert_singleton_resource_allowed_routes('accounts', {},                    :update, [:new, :create, :show, :edit, :destroy])
0
+      assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' },  :update, [:new, :create, :show, :edit, :destroy])
0
+
0
+      assert_not_nil set.named_routes[:account]
0
+    end
0
+  end
0
+
0
+  def test_singleton_resource_has_only_destroy_action_and_named_route
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resource :account, :only => :destroy
0
+      end
0
+
0
+      assert_singleton_resource_allowed_routes('accounts', {},                    :destroy, [:new, :create, :show, :edit, :update])
0
+      assert_singleton_resource_allowed_routes('accounts', { :format => 'xml' },  :destroy, [:new, :create, :show, :edit, :update])
0
+
0
+      assert_not_nil set.named_routes[:account]
0
+    end
0
+  end
0
+
0
   def test_resource_has_only_collection_action
0
     with_routing do |set|
0
       set.draw do |map|

Comments

This causes issues with singleton resources whereby the named resource route refers to the create action and not the show action, there is a fix in LH #1400

NZKoz Mon Nov 24 10:22:00 -0800 2008

Thanks geoff, applied.