Class: MongoMapper::Plugins::Validations::ValidatesUniquenessOf
- Inherits:
-
Validatable::ValidationBase
- Object
- Validatable::ValidationBase
- MongoMapper::Plugins::Validations::ValidatesUniquenessOf
- Defined in:
- lib/mongo_mapper/plugins/validations.rb
Instance Method Summary collapse
- #message(instance) ⇒ Object
- #scope_conditions(instance) ⇒ Object
- #valid?(instance) ⇒ Boolean
- #where_conditions(instance) ⇒ Object
Instance Method Details
#message(instance) ⇒ Object
27 28 29 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 27 def (instance) super || "has already been taken" end |
#scope_conditions(instance) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 31 def scope_conditions(instance) return {} unless scope Array(scope).inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end |
#valid?(instance) ⇒ Boolean
18 19 20 21 22 23 24 25 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 18 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 |
#where_conditions(instance) ⇒ Object
38 39 40 41 42 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 38 def where_conditions(instance) conditions = {} conditions[attribute] = /#{instance[attribute].to_s}/i unless case_sensitive conditions end |