Class: Reform::Validation::Groups::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/reform/validation/groups.rb

Overview

Runs all validations groups according to their rules and returns result. Populates errors passed into #call.

Instance Method Summary collapse

Constructor Details

#initialize(groups) ⇒ Result

DISCUSS: could be in Groups.



43
44
45
# File 'lib/reform/validation/groups.rb', line 43

def initialize(groups)
  @groups = groups
end

Instance Method Details

#call(fields, errors, form) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/reform/validation/groups.rb', line 47

def call(fields, errors, form)
  result = true
  results = {}

  @groups.each do |cfg|
    name, group, options = cfg
    depends_on = options[:if]

    if evaluate_if(depends_on, results, form)
      # puts "evaluating #{group.instance_variable_get(:@validator).instance_variable_get(:@checker).inspect}"
      results[name] = group.(fields, errors, form).empty? # validate.
    end

    result &= errors.empty?
  end

  result
end

#evaluate_if(depends_on, results, form) ⇒ Object



66
67
68
69
70
# File 'lib/reform/validation/groups.rb', line 66

def evaluate_if(depends_on, results, form)
  return true if depends_on.nil?
  return results[depends_on] if depends_on.is_a?(Symbol)
  form.instance_exec(results, &depends_on)
end