Class/Module Index [+]

Quicksearch

ActionController::ParamsWrapper::ClassMethods

Public Instance Methods

inherited(klass) click to toggle source

Sets the default wrapper key or model which will be used to determine wrapper key and attribute names. Will be called automatically when the module is inherited.

# File lib/action_controller/metal/params_wrapper.rb, line 126
def inherited(klass)
  if klass._wrapper_options[:format].present?
    klass._set_wrapper_defaults(klass._wrapper_options.slice(:format))
  end
  super
end
wrap_parameters(name_or_model_or_options, options = {}) click to toggle source

Sets the name of the wrapper key, or the model which ParamsWrapper would use to determine the attribute names from.

Examples

wrap_parameters :format => :xml
  # enables the parameter wrapper for XML format

wrap_parameters :person
  # wraps parameters into +params[:person]+ hash

wrap_parameters Person
  # wraps parameters by determining the wrapper key from Person class
  (+person+, in this case) and the list of attribute names

wrap_parameters :include => [:username, :title]
  # wraps only +:username+ and +:title+ attributes from parameters.

wrap_parameters false
  # disables parameters wrapping for this controller altogether.

Options

  • :format - The list of formats in which the parameters wrapper will be enabled.

  • :include - The list of attribute names which parameters wrapper will wrap into a nested hash.

  • :exclude - The list of attribute names which parameters wrapper will exclude from a nested hash.

# File lib/action_controller/metal/params_wrapper.rb, line 106
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

Protected Instance Methods

_set_wrapper_defaults(options, model=nil) click to toggle source
# File lib/action_controller/metal/params_wrapper.rb, line 163
def _set_wrapper_defaults(options, model=nil)
  options = options.dup

  unless options[:include] || options[:exclude]
    model ||= _default_wrap_model
    if 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.