Class: ClientSideValidations::Mongoid::Middleware

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

Class Method Summary collapse

Class Method Details

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

Still need to handle embedded documents

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/client_side_validations/mongoid/middleware.rb', line 5

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