提交 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
CANONICAL_ACTIONS = %w(index create new show update destroy)
class Resource #:nodoc:
attr_reader :controller, :path, :options, :param
attr_reader :controller, :path, :param
def initialize(entities, api_only, shallow, options = {})
@name = entities.to_s
......@@ -1076,6 +1076,8 @@ def initialize(entities, api_only, shallow, options = {})
@options = options
@shallow = shallow
@api_only = api_only
@only = options.delete :only
@except = options.delete :except
end
def default_actions
......@@ -1087,10 +1089,10 @@ def default_actions
end
def actions
if only = @options[:only]
Array(only).map(&:to_sym)
elsif except = @options[:except]
default_actions - Array(except).map(&:to_sym)
if @only
Array(@only).map(&:to_sym)
elsif @except
default_actions - Array(@except).map(&:to_sym)
else
default_actions
end
......@@ -1213,6 +1215,7 @@ def resource(*resources, &block)
end
with_scope_level(:resource) do
options = apply_action_options options
resource_scope(SingletonResource.new(resources.pop, api_only?, @scope[:shallow], options)) do
yield if block_given?
......@@ -1373,6 +1376,7 @@ def resources(*resources, &block)
end
with_scope_level(:resources) do
options = apply_action_options options
resource_scope(Resource.new(resources.pop, api_only?, @scope[:shallow], options)) do
yield if block_given?
......@@ -1671,23 +1675,20 @@ def apply_common_behavior_for(method, resources, options, &block) #:nodoc:
return true
end
unless action_options?(options)
options.merge!(scope_action_options) if scope_action_options?
end
false
end
def action_options?(options) #:nodoc:
options[:only] || options[:except]
def apply_action_options(options) # :nodoc:
return options if action_options? options
options.merge scope_action_options
end
def scope_action_options? #:nodoc:
@scope[:action_options]
def action_options?(options) #:nodoc:
options[:only] || options[:except]
end
def scope_action_options #:nodoc:
@scope[:action_options]
@scope[:action_options] || {}
end
def resource_scope? #:nodoc:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册