Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make inheritance of map.resources :only/:except options behave more p…

…redictably

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 2ecec6052f7f290252a9fd9cc27ec804c7aad36c 1 parent 94d6716
@tomstuart tomstuart authored NZKoz committed
View
33 actionpack/lib/action_controller/resources.rb
@@ -42,7 +42,7 @@ module ActionController
#
# Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
module Resources
- INHERITABLE_OPTIONS = :namespace, :shallow, :only, :except
+ INHERITABLE_OPTIONS = :namespace, :shallow, :actions
class Resource #:nodoc:
DEFAULT_ACTIONS = :index, :create, :new, :edit, :show, :update, :destroy
@@ -119,7 +119,7 @@ def uncountable?
end
def has_action?(action)
- !DEFAULT_ACTIONS.include?(action) || action_allowed?(action)
+ !DEFAULT_ACTIONS.include?(action) || @options[:actions].nil? || @options[:actions].include?(action)
end
protected
@@ -135,29 +135,24 @@ def add_default_actions
end
def set_allowed_actions
- only, except = @options.values_at(:only, :except)
- @allowed_actions ||= {}
+ only = @options.delete(:only)
+ except = @options.delete(:except)
- if only == :all || except == :none
- only = nil
- except = []
+ if only && except
+ raise ArgumentError, 'Please supply either :only or :except, not both.'
+ elsif only == :all || except == :none
+ options[:actions] = DEFAULT_ACTIONS
elsif only == :none || except == :all
- only = []
- except = nil
- end
-
- if only
- @allowed_actions[:only] = Array(only).map(&:to_sym)
+ options[:actions] = []
+ elsif only
+ options[:actions] = DEFAULT_ACTIONS & Array(only).map(&:to_sym)
elsif except
- @allowed_actions[:except] = Array(except).map(&:to_sym)
+ options[:actions] = DEFAULT_ACTIONS - Array(except).map(&:to_sym)
+ else
+ # leave options[:actions] alone
end
end
- def action_allowed?(action)
- only, except = @allowed_actions.values_at(:only, :except)
- (!only || only.include?(action)) && (!except || !except.include?(action))
- end
-
def set_prefixes
@path_prefix = options.delete(:path_prefix)
@name_prefix = options.delete(:name_prefix)
View
26 actionpack/test/controller/resources_test.rb
@@ -971,6 +971,32 @@ def test_nested_resource_has_only_show_and_member_action
end
end
+ def test_nested_resource_ignores_only_option
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :only => :show do |product|
+ product.resources :images, :except => :destroy
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, [:index, :new, :create, :show, :edit, :update], :destroy, 'products/1/images')
+ end
+ end
+
+ def test_nested_resource_ignores_except_option
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :products, :except => :show do |product|
+ product.resources :images, :only => :destroy
+ end
+ end
+
+ assert_resource_allowed_routes('images', { :product_id => '1' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
+ assert_resource_allowed_routes('images', { :product_id => '1', :format => 'xml' }, { :id => '2' }, :destroy, [:index, :new, :create, :show, :edit, :update], 'products/1/images')
+ end
+ end
+
protected
def with_restful_routing(*args)
with_routing do |set|

1 comment on commit 2ecec60

@vomelchenko

ыба!

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