public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make inheritance of map.resources :only/:except options behave more predictably

Signed-off-by: Michael Koziarski <michael@koziarski.com>
Tom Stuart (author)
Thu Nov 13 12:00:11 -0800 2008
NZKoz (committer)
Fri Nov 14 03:26:43 -0800 2008
commit  2ecec6052f7f290252a9fd9cc27ec804c7aad36c
tree    4a692a5e956f72e32fe8e48686c4d99abdd3c89b
parent  94d6716324126028b89dde886f160474049b1b0c
...
42
43
44
45
 
46
47
48
...
119
120
121
122
 
123
124
125
...
135
136
137
138
139
 
 
140
141
142
143
 
 
 
 
144
145
146
147
148
149
150
 
 
 
151
152
 
 
 
153
154
155
156
157
158
159
160
161
162
163
...
42
43
44
 
45
46
47
48
...
119
120
121
 
122
123
124
125
...
135
136
137
 
 
138
139
140
 
 
 
141
142
143
144
145
 
 
 
 
 
 
146
147
148
149
 
150
151
152
153
154
155
 
 
 
 
 
156
157
158
0
@@ -42,7 +42,7 @@ module ActionController
0
   #
0
   # Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
0
   module Resources
0
-    INHERITABLE_OPTIONS = :namespace, :shallow, :only, :except
0
+    INHERITABLE_OPTIONS = :namespace, :shallow, :actions
0
 
0
     class Resource #:nodoc:
0
       DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
0
@@ -119,7 +119,7 @@ module ActionController
0
       end
0
 
0
       def has_action?(action)
0
-        !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
0
+        !DEFAULT_ACTIONS.include?(action) || @options[:actions].nil? || @options[:actions].include?(action)
0
       end
0
 
0
       protected
0
@@ -135,29 +135,24 @@ module ActionController
0
         end
0
 
0
         def set_allowed_actions
0
-          only, except = @options.values_at(:only, :except)
0
-          @allowed_actions ||= {}
0
+          only    = @options.delete(:only)
0
+          except  = @options.delete(:except)
0
 
0
-          if only == :all || except == :none
0
-            only = nil
0
-            except = []
0
+          if only && except
0
+            raise ArgumentError, 'Please supply either :only or :except, not both.'
0
+          elsif only == :all || except == :none
0
+            options[:actions] = DEFAULT_ACTIONS
0
           elsif only == :none || except == :all
0
-            only = []
0
-            except = nil
0
-          end
0
-
0
-          if only
0
-            @allowed_actions[:only] = Array(only).map(&:to_sym)
0
+            options[:actions] = []
0
+          elsif only
0
+            options[:actions] = DEFAULT_ACTIONS & Array(only).map(&:to_sym)
0
           elsif except
0
-            @allowed_actions[:except] = Array(except).map(&:to_sym)
0
+            options[:actions] = DEFAULT_ACTIONS - Array(except).map(&:to_sym)
0
+          else
0
+            # leave options[:actions] alone
0
           end
0
         end
0
 
0
-        def action_allowed?(action)
0
-          only, except = @allowed_actions.values_at(:only, :except)
0
-          (!only || only.include?(action)) && (!except || !except.include?(action))
0
-        end
0
-
0
         def set_prefixes
0
           @path_prefix = options.delete(:path_prefix)
0
           @name_prefix = options.delete(:name_prefix)
...
971
972
973
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
974
975
976
...
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
0
@@ -971,6 +971,32 @@ class ResourcesTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+  def test_nested_resource_ignores_only_option
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resources :products, :only => :show do |product|
0
+          product.resources :images, :except => :destroy
0
+        end
0
+      end
0
+
0
+      assert_resource_allowed_routes('images', { :product_id => '1' },                    { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
0
+      assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' },  { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
0
+    end
0
+  end
0
+
0
+  def test_nested_resource_ignores_except_option
0
+    with_routing do |set|
0
+      set.draw do |map|
0
+        map.resources :products, :except => :show do |product|
0
+          product.resources :images, :only => :destroy
0
+        end
0
+      end
0
+
0
+      assert_resource_allowed_routes('images', { :product_id => '1' },                    { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
0
+      assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' },  { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
0
+    end
0
+  end
0
+
0
   protected
0
     def with_restful_routing(*args)
0
       with_routing do |set|

Comments

vomelchenko Thu Apr 30 05:10:03 -0700 2009

ыба!