class ParameterSets::Schema

Public Class Methods

new(name, options = {}, &block) click to toggle source
# File lib/parameter_sets/schema.rb, line 7
def initialize(name, options = {}, &block)
  @name = name
  @options = options
  @block = block
end

Public Instance Methods

base_param_name() click to toggle source

Return the base parameter name to get from the request's parameters.

# File lib/parameter_sets/schema.rb, line 14
def base_param_name
  @options[:param_name] || @name
end
parameters(controller, object = nil, options = {}) click to toggle source

Return a suitablely scoped ActionController::Parameters object based on the rules defined for this param set.

@param controller [ActionController::Base] the controller we're working within @param object [ActiveModel::Base] an object to generate attributes for

# File lib/parameter_sets/schema.rb, line 23
def parameters(controller, object = nil, options = {})
  dsl = SchemaDSL.new
  controller.instance_exec(dsl, object, options, &@block) if @block
  if dsl.fields.empty?
    raise NoParametersPermittedError, "No fields were permitted"
  else
    controller.params.require(base_param_name).permit(*dsl.fields)
  end
end