class MongoMapper::Plugins::Validations::UniquenessValidator
Public Class Methods
new(options)
click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/validations.rb, line 31 def initialize(options) @klass = options[:class] #only provided in ActiveModel 4.1 and higher super(options.reverse_merge(:case_sensitive => true)) end
Public Instance Methods
message(instance)
click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/validations.rb, line 61 def message(instance) super || "has already been taken" end
scope_conditions(instance)
click to toggle source
# File lib/mongo_mapper/plugins/validations.rb, line 65 def scope_conditions(instance) Array(options[:scope] || []).inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end
setup(klass)
click to toggle source
# File lib/mongo_mapper/plugins/validations.rb, line 39 def setup(klass) @klass = klass end
validate_each(record, attribute, value)
click to toggle source
# File lib/mongo_mapper/plugins/validations.rb, line 44 def validate_each(record, attribute, value) conditions = scope_conditions(record) if options[:case_sensitive] conditions[attribute] = value else conditions[attribute] = /^#{Regexp.escape(value.to_s)}$/i end # Make sure we're not including the current document in the query conditions[:_id.ne] = record._id if record._id if @klass.exists?(conditions) record.errors.add(attribute, :taken, **options.except(:case_sensitive, :scope).merge(:value => value)) end end