module ActionController::ParamsWrapper::ClassMethods
def _default_wrap_model #:nodoc:
try to find Foo::Bar::User, Foo::User and finally User.
This method also does namespace lookup. Foo::Bar::UsersController will
will try to find if the +User+ model exists.
same singularize name as the controller. For example, +UsersController+
this could be done by trying to find the defined model that has the
Determine the wrapper model from the controller's name. By convention,
def _default_wrap_model #:nodoc: return nil if self.anonymous? model_name = self.name.sub(/Controller$/, '').classify begin if model_klass = model_name.safe_constantize model_klass else namespaces = model_name.split("::") namespaces.delete_at(-2) break if namespaces.last == model_name model_name = namespaces.join("::") end end until model_klass model_klass end
def _set_wrapper_defaults(options, model=nil)
def _set_wrapper_defaults(options, model=nil) options = options.dup unless options[:include] || options[:exclude] model ||= _default_wrap_model role = options.has_key?(:as) ? options[:as] : :default if model.respond_to?(:accessible_attributes) && model.accessible_attributes(role).present? options[:include] = model.accessible_attributes(role).to_a elsif model.respond_to?(:attribute_names) && model.attribute_names.present? options[:include] = model.attribute_names end end unless options[:name] || self.anonymous? model ||= _default_wrap_model options[:name] = model ? model.to_s.demodulize.underscore : controller_name.singularize end options[:include] = Array.wrap(options[:include]).collect(&:to_s) if options[:include] options[:exclude] = Array.wrap(options[:exclude]).collect(&:to_s) if options[:exclude] options[:format] = Array.wrap(options[:format]) self._wrapper_options = options end
def inherited(klass)
wrapper key and attribute names. Will be called automatically when the
Sets the default wrapper key or model which will be used to determine
def inherited(klass) if klass._wrapper_options[:format].present? klass._set_wrapper_defaults(klass._wrapper_options.slice(:format)) end super end
def wrap_parameters(name_or_model_or_options, options = {})
* :exclude - The list of attribute names which parameters wrapper
will wrap into a nested hash.
* :include - The list of attribute names which parameters wrapper
will be enabled.
* :format - The list of formats in which the parameters wrapper
==== Options
# disables parameters wrapping for this controller altogether.
wrap_parameters false
# wraps only +:username+ and +:title+ attributes from parameters.
wrap_parameters :include => [:username, :title]
(+person+, in this case) and the list of attribute names
# wraps parameters by determining the wrapper key from Person class
wrap_parameters Person
# wraps parameters into +params[:person]+ hash
wrap_parameters :person
# enables the parameter wrapper for XML format
wrap_parameters :format => :xml
==== Examples
would use to determine the attribute names from.
Sets the name of the wrapper key, or the model which +ParamsWrapper+
def wrap_parameters(name_or_model_or_options, options = {}) model = nil case name_or_model_or_options when Hash options = name_or_model_or_options when false options = options.merge(:format => []) when Symbol, String options = options.merge(:name => name_or_model_or_options) else model = name_or_model_or_options end _set_wrapper_defaults(_wrapper_options.slice(:format).merge(options), model) end