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
28 29 30 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 28 def (instance) super || "has already been taken" end |
#scope_conditions(instance) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 32 def scope_conditions(instance) return {} unless scope Array(scope).inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end |
#valid?(instance) ⇒ Boolean
19 20 21 22 23 24 25 26 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 19 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
39 40 41 42 43 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 39 def where_conditions(instance) conditions = {} conditions[attribute] = /^#{Regexp.escape(instance[attribute].to_s)}$/i unless case_sensitive conditions end |