Sha256: c4432c51538994453951b640df4725a0b76ad35607d6d4a1741659679cb8a859

Contents?: true

Size: 1.22 KB

Versions: 94

Compression:

Stored size: 1.22 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Validations #:nodoc:

    # Validates that the specified attributes are not blank (as defined by
    # Object#blank?).
    #
    # @example Define the presence validator.
    #
    #   class Person
    #     include Mongoid::Document
    #     field :title
    #
    #     validates_presence_of :title
    #   end
    class PresenceValidator < ActiveModel::EachValidator

      # Validate the document for the attribute and value.
      #
      # @example Validate the document.
      #   validator.validate_each(doc, :title, "")
      #
      # @param [ Document ] document The document to validate.
      # @param [ Symbol ] attribute The attribute name.
      # @param [ Object ] value The current value of the field.
      #
      # @since 2.4.0
      def validate_each(document, attribute, value)
        field = document.fields[attribute.to_s]
        if field.try(:localized?) && !value.blank?
          value.each_pair do |locale, value|
            document.errors.add(attribute, :blank_on_locale, options.merge(:location => locale)) if value.blank?
          end
        else
          document.errors.add(attribute, :blank, options) if value.blank?
        end
      end
    end
  end
end

Version data entries

94 entries across 48 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.5 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.4 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.7.3 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.2 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.1 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.7.0 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.6.9 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.6.9 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.6.8 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.6.8 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.6.7 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.6.7 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb
classiccms-0.6.6 vendor/bundle/gems/mongoid-2.4.10/lib/mongoid/validations/presence.rb
classiccms-0.6.6 vendor/bundle/gems/mongoid-2.4.9/lib/mongoid/validations/presence.rb