module ActiveModel
module Validations
class AsEnumValidator < ActiveModel::Validator
attr_reader :attributes
def initialize(options)
@attributes = Array.wrap(options.delete(:attributes))
raise ":attributes cannot be blank" if @attributes.empty?
super
end
def setup(klass)
@klass = klass
end
def validate(record)
attributes.each do |attribute|
enum_def = @klass.enum_definitions[attribute]
raw_value = record.send(enum_def[:column])
next if (raw_value.nil? && options[:allow_nil]) || (raw_value.blank? && options[:allow_blank])
unless @klass.send(enum_def[:name].to_s.pluralize).values.include?(raw_value)
record.errors.add(attribute, :invalid_enum, options)
end
end
end
end
module HelperMethods
# Validates an +as_enum+ field based on the value of it's column.
#
# Model:
# class User < ActiveRecord::Base
# as_enum :gender, [ :male, :female ]
# validates_as_enum :gender
# end
#
# View:
# <%= select(:user, :gender, User.genders.keys) %>
#
# Configuration options:
# * :message - A custom error message (default: is [:activerecord, :errors, :messages, :invalid_enum]).
# * :on - Specifies when this validation is active (default is always, other options :create, :update).
# * :if - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The
# method, proc or string should return or evaluate to a true or false value.
# * :unless - Specifies a method, proc or string to call to determine if the validation should
# not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The
# method, proc or string should return or evaluate to a true or false value.
def validates_as_enum(*attr_names)
validates_with AsEnumValidator, _merge_attributes(attr_names)
end
end
end
end