Class: MongoMapper::Plugins::Validations::UniquenessValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- MongoMapper::Plugins::Validations::UniquenessValidator
- Defined in:
- lib/novelys_mongo_mapper/plugins/validations.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ UniquenessValidator
constructor
A new instance of UniquenessValidator.
-
#setup(klass) ⇒ Object
Unfortunately, we have to tie Uniqueness validators to a class.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ UniquenessValidator
Returns a new instance of UniquenessValidator.
15 16 17 |
# File 'lib/novelys_mongo_mapper/plugins/validations.rb', line 15 def initialize() super(.reverse_merge(:case_sensitive => true)) end |
Instance Method Details
#setup(klass) ⇒ Object
Unfortunately, we have to tie Uniqueness validators to a class.
20 21 22 |
# File 'lib/novelys_mongo_mapper/plugins/validations.rb', line 20 def setup(klass) @klass = klass end |
#validate_each(record, attribute, value) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/novelys_mongo_mapper/plugins/validations.rb', line 24 def validate_each(record, attribute, value) # finder_class = find_finder_class_for(record) # table = finder_class.unscoped # # table_name = record.class.quoted_table_name # sql, params = mount_sql_and_params(finder_class, table_name, attribute, value) # # relation = table.where(sql, *params) # # Array(options[:scope]).each do |scope_item| # scope_value = record.send(scope_item) # relation = relation.where(scope_item => scope_value) # end # # unless record.new_record? # # TODO : This should be in Arel # relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id)) # end # # if relation.exists? # record.errors.add(attribute, :taken, :default => options[:message], :value => value) # end # value = instance[attribute] # return true if allow_blank && value.blank? # base_conditions = case_sensitive ? {self.attribute => value} : {} where_conditions = {} where_conditions[attribute] = /#{record[attribute].to_s}/i doc = record.class.first(where_conditions) record.errors.add(attribute, :taken, :default => [:message], :value => value) unless doc.nil? || record._id == doc._id end |