diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-13 11:38:36 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-13 16:35:15 -0800 |
commit | 95ec44858023d7263a2227effc064d5b8a22b772 (patch) | |
tree | eb8bccc925040a63174e91fe73c3d9237060e578 | |
parent | bf3546549b5b355d4d03626a043cb110f628dd5b (diff) | |
download | rails-95ec44858023d7263a2227effc064d5b8a22b772.tar.gz rails-95ec44858023d7263a2227effc064d5b8a22b772.tar.bz2 rails-95ec44858023d7263a2227effc064d5b8a22b772.zip |
start using options object
-rw-r--r-- | actionpack/lib/action_controller/metal/params_wrapper.rb | 29 | ||||
-rw-r--r-- | actionpack/test/controller/params_wrapper_test.rb | 2 |
2 files changed, 21 insertions, 10 deletions
diff --git a/actionpack/lib/action_controller/metal/params_wrapper.rb b/actionpack/lib/action_controller/metal/params_wrapper.rb index f9f6cc27a3..0a463257ce 100644 --- a/actionpack/lib/action_controller/metal/params_wrapper.rb +++ b/actionpack/lib/action_controller/metal/params_wrapper.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/hash/except' require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/struct' require 'action_dispatch/http/mime_types' module ActionController @@ -72,12 +73,22 @@ module ActionController EXCLUDE_PARAMETERS = %w(authenticity_token _method utf8) + Options = Struct.new(:name, :format, :include, :exclude) do # :nodoc: + def self.from_hash(hash) + new(*hash.values_at(:name, :format, :include, :exclude)) + end + end + included do class_attribute :_wrapper_options - self._wrapper_options = { :format => [] } + self._wrapper_options = Options.from_hash(format: []) end module ClassMethods + def _set_wrapper_options(options) + self._wrapper_options = Options.from_hash(options) + end + # Sets the name of the wrapper key, or the model which +ParamsWrapper+ # would use to determine the attribute names from. # @@ -119,7 +130,7 @@ module ActionController model = name_or_model_or_options end - opts = _wrapper_options.slice(:format).merge(options) + opts = _wrapper_options.to_h.slice(:format).merge(options) params = opts.values_at(:name, :format, :include, :exclude) _set_wrapper_defaults(*params, model) @@ -129,8 +140,8 @@ module ActionController # wrapper key and attribute names. Will be called automatically when the # module is inherited. def inherited(klass) - if klass._wrapper_options[:format].any? - params = klass._wrapper_options.values_at(:name, :format, :include, :exclude) + if klass._wrapper_options.format.any? + params = klass._wrapper_options.to_h.values_at(:name, :format, :include, :exclude) klass._set_wrapper_defaults(*params) end super @@ -181,7 +192,7 @@ module ActionController opts[:include] = Array(include).collect(&:to_s) if include opts[:exclude] = Array(exclude).collect(&:to_s) if exclude - self._wrapper_options = opts + self._wrapper_options = Options.from_hash(opts) end end @@ -207,20 +218,20 @@ module ActionController # Returns the wrapper key which will use to stored wrapped parameters. def _wrapper_key - _wrapper_options[:name] + _wrapper_options.name end # Returns the list of enabled formats. def _wrapper_formats - _wrapper_options[:format] + _wrapper_options.format end # Returns the list of parameters which will be selected for wrapped. def _wrap_parameters(parameters) - value = if include_only = _wrapper_options[:include] + value = if include_only = _wrapper_options.include parameters.slice(*include_only) else - exclude = _wrapper_options[:exclude] || [] + exclude = _wrapper_options.exclude || [] parameters.except(*(exclude + EXCLUDE_PARAMETERS)) end diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index 209f021cf7..d87e2b85b0 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -4,7 +4,7 @@ module Admin; class User; end; end module ParamsWrapperTestHelp def with_default_wrapper_options(&block) - @controller.class._wrapper_options = {:format => [:json]} + @controller.class._set_wrapper_options({:format => [:json]}) @controller.class.inherited(@controller.class) yield end |