Skip to content
This repository
Browse code

Fix map.resources to always generate named routes if they're needed

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

1 note on commit 4c09210

Geoff Garside

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

Michael Koziarski
Owner

Thanks geoff, applied.

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