Sha256: 10550a6fe65be96f86da8194dc5459d8f22f0ab9fef5d0f4ab1a797782663867
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
module MongoMapper module Validations module Macros def validates_uniqueness_of(*args) add_validations(args, MongoMapper::Validations::ValidatesUniquenessOf) end def validates_exclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesExclusionOf) end def validates_inclusion_of(*args) add_validations(args, MongoMapper::Validations::ValidatesInclusionOf) end end class ValidatesUniquenessOf < Validatable::ValidationBase option :scope def valid?(instance) doc = instance.class.find(:first, :conditions => {self.attribute => instance[attribute]}.merge(scope_conditions(instance)), :limit => 1) doc.nil? || instance.id == doc.id end def message(instance) super || "has already been taken" end def scope_conditions(instance) return {} unless scope Array(scope).inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end end class ValidatesExclusionOf < Validatable::ValidationBase required_option :within def valid?(instance) value = instance[attribute] return true if allow_nil && value.nil? return true if allow_blank && value.blank? !within.include?(instance[attribute]) end def message(instance) super || "is reserved" end end class ValidatesInclusionOf < Validatable::ValidationBase required_option :within def valid?(instance) value = instance[attribute] return true if allow_nil && value.nil? return true if allow_blank && value.blank? within.include?(value) end def message(instance) super || "is not in the list" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
djsun-mongomapper-0.4.1.2 | lib/mongomapper/validations.rb |