Class: MongoMapper::Validations::ValidatesUniquenessOf

Inherits:
Validatable::ValidationBase
  • Object
show all
Defined in:
lib/mongomapper/validations.rb

Instance Method Summary collapse

Instance Method Details

#message(instance) ⇒ Object



25
26
27
# File 'lib/mongomapper/validations.rb', line 25

def message(instance)
  super || "has already been taken"
end

#scope_conditions(instance) ⇒ Object



29
30
31
32
33
34
# File 'lib/mongomapper/validations.rb', line 29

def scope_conditions(instance)
  return {} unless scope
  Array(scope).inject({}) do |conditions, key|
    conditions.merge(key => instance[key])
  end
end

#valid?(instance) ⇒ Boolean

Returns:



20
21
22
23
# File 'lib/mongomapper/validations.rb', line 20

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