Class: WeakParameters::ObjectValidator

Inherits:
HashValidator show all
Defined in:
lib/weak_parameters/object_validator.rb

Instance Attribute Summary collapse

Attributes inherited from BaseValidator

#block, #controller, #options

Instance Method Summary collapse

Methods inherited from BaseValidator

#key, #required?, #strong?, #type

Constructor Details

#initialize(controller, key, validators, options = {}) ⇒ ObjectValidator

Returns a new instance of ObjectValidator.



4
5
6
7
# File 'lib/weak_parameters/object_validator.rb', line 4

def initialize(controller, key, validators, options = {})
  super controller, key, options
  @validators = validators
end

Instance Attribute Details

#validatorsObject (readonly)

Returns the value of attribute validators.



3
4
5
# File 'lib/weak_parameters/object_validator.rb', line 3

def validators
  @validators
end

Instance Method Details

#strong_params(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/weak_parameters/object_validator.rb', line 19

def strong_params(*args)
  super

  strong_values = validators.map do |validator|
    validator.strong_params(*path)
  end.inject(ActionController::Parameters.new, &:merge)
  return {} if strong_values.blank?
  { key => strong_values }
end

#validate(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/weak_parameters/object_validator.rb', line 9

def validate(*args)
  super

  if valid? && exist?
    validators.each do |validator|
      validator.validate(*path)
    end
  end
end