Sha256: a77f45b975520717b75d035a69e67dc85a210fb0499800b8b9ade5cb531d2c72

Contents?: true

Size: 1.39 KB

Versions: 36

Compression:

Stored size: 1.39 KB

Contents

module MongoMapper
  module Plugins
    module Validations
      def self.configure(model)
        model.class_eval { include Validatable }
      end
      
      module DocumentMacros
        def validates_uniqueness_of(*args)
          add_validations(args, MongoMapper::Plugins::Validations::ValidatesUniquenessOf)
        end
      end
      
      class ValidatesUniquenessOf < Validatable::ValidationBase
        option :scope, :case_sensitive
        default :case_sensitive => true

        def valid?(instance)
          value = instance[attribute]
          return true if allow_blank && value.blank?
          return true if allow_nil && value.nil?
          base_conditions = case_sensitive ? {self.attribute => value} : {}
          doc = instance.class.first(base_conditions.merge(scope_conditions(instance)).merge(where_conditions(instance)))
          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

        def where_conditions(instance)
          conditions = {}
          conditions[attribute] = /#{instance[attribute].to_s}/i unless case_sensitive
          conditions
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 3 rubygems

Version Path
numon-0.0.1 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-0.7.1 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.3.8 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.3.5 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.3.4 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.3.3 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.28 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.27 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.26 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.25 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.24 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.23 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.22 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.19 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.18 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.17 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.16 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.15 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.12 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.2.11 lib/mongo_mapper/plugins/validations.rb