Class: ClientSideValidations::MongoMapper::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/client_side_validations/mongo_mapper/middleware.rb

Class Method Summary collapse

Class Method Details

.is_class?(klass) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/client_side_validations/mongo_mapper/middleware.rb', line 4

def self.is_class?(klass)
  klass < ::MongoMapper::Document
end

.is_unique?(klass, attribute, value, params) ⇒ Boolean

Still need to handle embedded documents

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/client_side_validations/mongo_mapper/middleware.rb', line 9

def self.is_unique?(klass, attribute, value, params)
  if params[:case_sensitive] == 'false'
    value = Regexp.new("^#{Regexp.escape(value.to_s)}$", Regexp::IGNORECASE)
  end

  criteria = klass.where(attribute => value)
  criteria = criteria.where(:_id => {'$ne' => BSON::ObjectId(params[:id])}) if params[:id]

  (params[:scope] || {}).each do |key, value|
    criteria = criteria.where(key => value)
  end

  !criteria.exists?
end