Class: ActiveRecord::Validations::PgEnumValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/enum_kit/active_record_patches/validations/pg_enum_validator.rb

Overview

Validates whether an enum’s value is acceptable by comparing with the acceptable values defined in the PostgreSQL database.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validate the given value is acceptable for the enum.

Parameters:

  • record (ActiveRecord::Base)

    The record being validated.

  • attribute (Symbol)

    The enum attribute being validated.

  • value (String, Symbol, nil)

    The current value of the enum.



19
20
21
22
23
24
25
26
27
28
# File 'lib/enum_kit/active_record_patches/validations/pg_enum_validator.rb', line 19

def validate_each(record, attribute, value)
  values = record.class.pg_enum_values(attribute)

  return if values.include?(value)

  record.errors.add(attribute, options[:message] || :invalid, **options.except(:message).merge!(
    attribute: record.class.human_attribute_name(attribute),
    values: values.join(', ')
  ))
end