Sha256: f9da79c660e459b6fc1b8e0a00740bb1bf8612efd79180675f54285777900cd2

Contents?: true

Size: 1.44 KB

Versions: 8

Compression:

Stored size: 1.44 KB

Contents

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

    # Validates whether or not an association is valid or not. Will correctly
    # handle has one and has many associations.
    #
    # @example Set up the association validations.
    #
    #   class Person
    #     include Mongoid::Document
    #     embeds_one :name
    #     embeds_many :addresses
    #
    #     validates_associated :name, :addresses
    #   end
    class AssociatedValidator < ActiveModel::EachValidator

      # Validates that the associations provided are either all nil or all
      # valid. If neither is true then the appropriate errors will be added to
      # the parent document.
      #
      # @example Validate the association.
      #   validator.validate_each(document, :name, name)
      #
      # @param [ Document ] document The document to validate.
      # @param [ Symbol ] attribute The relation to validate.
      # @param [ Object ] value The value of the relation.
      def validate_each(document, attribute, value)
        begin
          document.begin_validate
          valid = Array.wrap(value).collect do |doc|
            if doc.nil?
              true
            else
              doc.validated? ? true : doc.valid?
            end
          end.all?
        ensure
          document.exit_validate
        end
        return if valid
        document.errors.add(attribute, :invalid, options.merge(:value => value))
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mongoid-2.2.6 lib/mongoid/validations/associated.rb
mongoid-2.2.5 lib/mongoid/validations/associated.rb
mongoid-2.2.4 lib/mongoid/validations/associated.rb
mongoid-2.2.3 lib/mongoid/validations/associated.rb
mongoid-2.2.2 lib/mongoid/validations/associated.rb
mongoid-2.2.1 lib/mongoid/validations/associated.rb
mongoid-2.2.0 lib/mongoid/validations/associated.rb
mongoid-2.1.9 lib/mongoid/validations/associated.rb