Class: PGTrunk::DifferenceValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/pg_trunk/core/validators/difference_validator.rb

Overview

Ensure that an attribute is different from another one

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pg_trunk/core/validators/difference_validator.rb', line 6

def validate_each(record, attribute, value)
  another_name = options.fetch(:from)
  another_value = record.send(another_name).presence

  case another_value
  when PGTrunk::QualifiedName
    return unless value.maybe_eq?(another_value)
  else
    return unless value == another_value
  end

  record.errors.add attribute, "must be different from the #{another_name}"
end