提交 f514c9c0 编写于 作者: A Aaron Patterson

don't mutate the caller's variables

Remove the `options` reader from `Resource` because nobody needs to see
that hash.  Also remove mutations on the options hash in
`apply_common_behavior_for` because leaving the side effects in that
method makes it difficult to understand what is going on in the caller.
上级 33d20ea1
...@@ -1065,7 +1065,7 @@ module Resources ...@@ -1065,7 +1065,7 @@ module Resources
CANONICAL_ACTIONS = %w(index create new show update destroy) CANONICAL_ACTIONS = %w(index create new show update destroy)
class Resource #:nodoc: class Resource #:nodoc:
attr_reader :controller, :path, :options, :param attr_reader :controller, :path, :param
def initialize(entities, api_only, shallow, options = {}) def initialize(entities, api_only, shallow, options = {})
@name = entities.to_s @name = entities.to_s
...@@ -1076,6 +1076,8 @@ def initialize(entities, api_only, shallow, options = {}) ...@@ -1076,6 +1076,8 @@ def initialize(entities, api_only, shallow, options = {})
@options = options @options = options
@shallow = shallow @shallow = shallow
@api_only = api_only @api_only = api_only
@only = options.delete :only
@except = options.delete :except
end end
def default_actions def default_actions
...@@ -1087,10 +1089,10 @@ def default_actions ...@@ -1087,10 +1089,10 @@ def default_actions
end end
def actions def actions
if only = @options[:only] if @only
Array(only).map(&:to_sym) Array(@only).map(&:to_sym)
elsif except = @options[:except] elsif @except
default_actions - Array(except).map(&:to_sym) default_actions - Array(@except).map(&:to_sym)
else else
default_actions default_actions
end end
...@@ -1213,6 +1215,7 @@ def resource(*resources, &block) ...@@ -1213,6 +1215,7 @@ def resource(*resources, &block)
end end
with_scope_level(:resource) do with_scope_level(:resource) do
options = apply_action_options options
resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], options)) do resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], options)) do
yield if block_given? yield if block_given?
...@@ -1373,6 +1376,7 @@ def resources(*resources, &block) ...@@ -1373,6 +1376,7 @@ def resources(*resources, &block)
end end
with_scope_level(:resources) do with_scope_level(:resources) do
options = apply_action_options options
resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do
yield if block_given? yield if block_given?
...@@ -1671,23 +1675,20 @@ def apply_common_behavior_for(method, resources, options, &block) #:nodoc: ...@@ -1671,23 +1675,20 @@ def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
return true return true
end end
unless action_options?(options)
options.merge!(scope_action_options) if scope_action_options?
end
false false
end end
def action_options?(options) #:nodoc: def apply_action_options(options) # :nodoc:
options[:only] || options[:except] return options if action_options? options
options.merge scope_action_options
end end
def scope_action_options? #:nodoc: def action_options?(options) #:nodoc:
@scope[:action_options] options[:only] || options[:except]
end end
def scope_action_options #:nodoc: def scope_action_options #:nodoc:
@scope[:action_options] @scope[:action_options] || {}
end end
def resource_scope? #:nodoc: def resource_scope? #:nodoc:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册