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

Version Path
mongoid-enum-i18n-0.8.1 lib/mongoid/enum/validators/multiple_validator.rb
mongoid-enum-i18n-0.5.0 lib/mongoid/enum/validators/multiple_validator.rb
mongoid-enum-i18n-0.3.3 lib/mongoid/enum/validators/multiple_validator.rb
mongoid-enum-i18n-0.3.0 lib/mongoid/enum/validators/multiple_validator.rb
mongoid-enum-i18n-0.2.9 lib/mongoid/enum/validators/multiple_validator.rb