Sha256: 88a58c5ecb50ee404393813318167d154e75d2d4edd9b77d847835e131df20dd

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8
require "mongoid/validations/associated"
require "mongoid/validations/uniqueness"

I18n.load_path << File.join(
  File.dirname(__FILE__), "validations", "locale", "en.yml"
)

module Mongoid #:nodoc:
  # This module provides additional validations that ActiveModel does not
  # provide: validates_associated and validates_uniqueness_of
  module Validations
    extend ActiveSupport::Concern
    included do
      include ActiveModel::Validations
    end

    module ClassMethods #:nodoc:
      # Validates whether or not an association is valid or not. Will correctly
      # handle has one and has many associations.
      #
      # Example:
      #
      #   class Person
      #     include Mongoid::Document
      #     has_one :name
      #     has_many :addresses
      #
      #     validates_associated :name, :addresses
      #   end
      def validates_associated(*args)
        validates_with(AssociatedValidator, _merge_attributes(args))
      end

      # Validates whether or not a field is unique against the documents in the
      # database.
      #
      # Example:
      #
      #   class Person
      #     include Mongoid::Document
      #     field :title
      #
      #     validates_uniqueness_of :title
      #   end
      def validates_uniqueness_of(*args)
        validates_with(UniquenessValidator, _merge_attributes(args))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-pre-2.0.0.pre lib/mongoid/validations.rb