Sha256: 03f9a8257c870ee3d752932045c3141146407d89a6320a41b19c43f844182304

Contents?: true

Size: 1.45 KB

Versions: 51

Compression:

Stored size: 1.45 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Validations
      def self.configure(model)
        model.class_eval do
          include Validatable
          extend Validations::DocumentMacros
        end
      end

      module DocumentMacros
        def validates_uniqueness_of(*args)
          add_validations(args, 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] = /^#{Regexp.escape(instance[attribute].to_s)}$/i unless case_sensitive
          conditions
        end
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 3 rubygems

Version Path
mongo_mapper-unstable-2010.08.19 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.18 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.17 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.16 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.15 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.14 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.13 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.12 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.11 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.10 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.09 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-0.8.3 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.08 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.06 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.05 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.04 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.03 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.02 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.08.01 lib/mongo_mapper/plugins/validations.rb
mongo_mapper-unstable-2010.07.31 lib/mongo_mapper/plugins/validations.rb