Sha256: da6ca80f21de64181135456fa4124d795834665dfa9f010db7fcd3ff66431d62

Contents?: true

Size: 709 Bytes

Versions: 6

Compression:

Stored size: 709 Bytes

Contents

module ActiveModel
  module Validations
    class BooleanPresenceValidator < ActiveModel::EachValidator
      # You can't use validates :presence for boolean attributes!
      # You could do this instead:
      #   validates_inclusion_of :attr, :in => [true, false]
      # but the main reason to use this validator instead of is that the error message is better ("not
      # in list? what list?") and the meaning is clearer when reading the code (we're validating the
      # presence of a boolean attribute -- true or false).
      def validate_each(record, attribute, value)
        unless [true, false].include? value
          record.errors.add attribute, :blank
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activemodel-validators-3.0.1 lib/activemodel-validators/boolean_presence_validator.rb
activemodel-validators-3.0.0 lib/activemodel-validators/boolean_presence_validator.rb
activemodel-validators-2.0.0 lib/activemodel-validators/boolean_presence_validator.rb
activemodel-validators-1.2.0 lib/activemodel-validators/boolean_presence_validator.rb
activemodel-validators-1.1.0 lib/activemodel-validators/boolean_presence_validator.rb
activemodel-validators-1.0.0 lib/activemodel-validators/boolean_presence_validator.rb