Class: ParameterSets::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/parameter_sets/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Schema



7
8
9
10
11
# File 'lib/parameter_sets/schema.rb', line 7

def initialize(name, options = {}, &block)
  @name = name
  @options = options
  @block = block
end

Instance Method Details

#base_param_nameObject

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



14
15
16
# File 'lib/parameter_sets/schema.rb', line 14

def base_param_name
  @options[:param_name] || @name
end

#parameters(controller, object = nil, options = {}) ⇒ Object

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



23
24
25
26
27
28
29
30
31
# 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