Module: ActionController::ParamsWrapper::ClassMethods

Defined in:
actionpack/lib/action_controller/metal/params_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#_set_wrapper_options(options) ⇒ Object



165
166
167
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 165

def _set_wrapper_options(options)
  self._wrapper_options = Options.from_hash(options)
end

#inherited(klass) ⇒ Object

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.



220
221
222
223
224
225
226
227
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 220

def inherited(klass)
  if klass._wrapper_options.format.any?
    params = klass._wrapper_options.dup
    params.klass = klass
    klass._wrapper_options = params
  end
  super
end

#wrap_parameters(name_or_model_or_options, options = {}) ⇒ Object

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.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'actionpack/lib/action_controller/metal/params_wrapper.rb', line 196

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

  opts   = Options.from_hash _wrapper_options.to_h.slice(:format).merge(options)
  opts.model = model
  opts.klass = self

  self._wrapper_options = opts
end