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
31 32 33 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 31 def (instance) super || "has already been taken" end |
#scope_conditions(instance) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 35 def scope_conditions(instance) return {} unless scope Array(scope).inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end |
#valid?(instance) ⇒ Boolean
22 23 24 25 26 27 28 29 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 22 def valid?(instance) value = instance[attribute] return allow_nil if value.nil? and not allow_nil.nil? return allow_blank if value.blank? and not allow_blank.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
42 43 44 45 46 |
# File 'lib/mongo_mapper/plugins/validations.rb', line 42 def where_conditions(instance) conditions = {} conditions[attribute] = /^#{Regexp.escape(instance[attribute].to_s)}$/i unless case_sensitive conditions end |