Sha256: ac6039a67e4fd4c2fc50e773475fe7f24d76fb773ffc79981dee01443c2968ce
Contents?: true
Size: 925 Bytes
Versions: 5
Compression:
Stored size: 925 Bytes
Contents
module Mongoid module Enum module Validators # Mongoid Enum # # Multiple Enums Validator # class MultipleValidator < ActiveModel::EachValidator def validate_each(record, attribute, values) values = Array(values).map(&:to_sym) if options[:allow_nil] unless all_included?(values, options[:in]) add_error_message record, attribute end elsif values.empty? || !all_included?(values, options[:in]) add_error_message record, attribute end end def add_error_message(record, attribute) record.errors[attribute] << (options[:message] || "is not in #{options[:in].join ', '}") end def all_included?(values, allowed) (values - allowed).empty? end def self.kind :custom end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems