Class: ActiveModel::Validations::MustBeValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validators/must_be_validator.rb

Constant Summary collapse

MATCHERS =
{
  equal_to: :==,
  greater_than: :>,
  greater_or_equal_to: :>=,
  less_than: :<,
  less_or_equal_to: :<=,
  not_equal_to: :!=,
  matching: :=~,
  # one_of: :in?
}.freeze
MESSAGE_PREFIX =
"must be"
ERRORS_METHOD =
:errors

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/validators/must_be_validator.rb', line 20

def validate_each(record, attribute, value) 
  raise "must_be_validator requires at least one comparison operator for attribute." unless options.slice(MATCHERS.keys + [:blank, :present])

  original_value = record.read_attribute_before_type_cast( attribute )

  message = self.class::MESSAGE_PREFIX.dup

  return if options[:blank] && value.blank?
  message << " blank" if options[:blank]

  return if options[:present] && value.present?
  message << " present" if options[:present]

  return if options[:one_of] && Array(options[:one_of]).flatten.include?(value)
  message = ": '#{value}' is not a valid value" if options[:one_of]

  return if options[:not_any_of] && !(options[:not_any_of].include?(value))
  message = ": '#{value}' is not a valid value" if options[:not_any_of]

  return if options[:only_from] && (options[:only_from] & Array(value) == Array(value))
  message = ": #{Array(value).join(",").gsub('"', '\'')} is not a valid value" if options[:only_from]

  # handle before and after date comparisons using date validator
  if options[:before] 
    before_resp = DateValidator.new(options.merge(attributes: attributes)).validate_before_option(record, attribute, value, original_value)
    return if before_resp == true
    message = before_resp
  end
  if options[:after] 
    after_resp = DateValidator.new(options.merge(attributes: attributes)).validate_after_option(record, attribute, value, original_value)
    return if after_resp == true
    message = after_resp
  end

  options.slice(*MATCHERS.keys).each do |key, operand|
    operand_msg = operand.is_a?(Regexp) ? operand.inspect : operand.to_s
    operand = record.send(operand) if operand.is_a? Symbol
    return if operand.nil?
    return if value&.send(MATCHERS[key], operand)
    return if key == :not_equal_to && value != operand
    message << " #{key.to_s.humanize(capitalize: false)} #{operand_msg}"
  end

  if options[:when].present?
    # check if conditions to validate satisfied
    conditions = options[:when]
    raise "Invalid :when option provided to must_be, must be a Hash or method" unless (conditions.is_a? Hash || conditions.is_a?(Method))
    # add error message with predicate info
    message << " when "
    condition_errors = []
    conditions.each do |field, values|
      case values
      when Regexp
        return if !values.match?(record[field])
        condition_errors << "#{field} = #{record[field]}"
      when Array
        return if !values.flatten.include?(record[field])
        condition_errors << "#{field} = #{record[field]}"
      when :blank
        return unless record.send(field).blank?
        condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
      when :present
        return unless record.send(field).present?
        condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
      when Symbol
        return if !record.send(field)&.send(values)
        condition_errors << "#{field} is #{values.to_s.gsub('?', '')}"
      else
        return if values != record[field]
        condition_errors << "#{field} = #{record[field]}"
      end
    end
    message << condition_errors.join(' and ')
  end

  if options[:one_of] && options.fetch(:show_values, true)
    message << ". Valid values: #{options[:one_of].join(', ')}"
  end
  if options[:only_from] && options.fetch(:show_values, true)
    message << ". Valid values: #{options[:only_from].join(', ')}"
  end
  if options[:not_any_of] && options.fetch(:show_values, true)
    message << ". Invalid values: #{options[:not_any_of].join(', ')}"
  end

  record.send(self.class::ERRORS_METHOD).add(attribute, (options[:message] || "#{message}."), rule_name: options[:rule_name])
end