Class: DerivationValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/derivation_validator.rb

Overview

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ void

This method returns an undefined value.

Validates that attribute's value equals derived_'s value. If they are not equal then the error message is 'must match its derivation'.

Parameters:

  • record (#errors, ActiveRecord::Base)

    ActiveModel or ActiveRecord

  • attribute (Symbol)

    name of derived attribute.

  • value (Object)

    value of attribute in record.



10
11
12
13
14
15
16
# File 'app/validators/derivation_validator.rb', line 10

def validate_each(record, attribute, value)
  derived_value = record.send("derived_#{attribute}")

  if value != derived_value
    record.errors[attribute] << 'must match its derivation'
  end
end