Module: Mongoid::Locker::Wrapper

Defined in:
lib/mongoid/locker/wrapper.rb

Overview

Normalizes queries between Mongoid 2 and 3.

Constant Summary collapse

IS_OLD_MONGOID =
Mongoid::VERSION.start_with? '2.'

Class Method Summary collapse

Class Method Details

.locked_until(doc) ⇒ Time

Determine whether the provided document is locked in the database or not.

Parameters:

  • The (Class)

    model instance

Returns:

  • (Time)

    The timestamp of when the document is locked until, nil if not locked.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mongoid/locker/wrapper.rb', line 28

def self.locked_until(doc)
  existing_query = {
    _id: doc.id,
    locked_until: { '$exists' => true }
  }

  if IS_OLD_MONGOID
    existing = doc.class.collection.find_one(existing_query, fields: { locked_until: 1 })
    existing ? existing['locked_until'] : nil
  else
    existing = doc.class.where(existing_query).limit(1).only(:locked_until).first
    existing ? existing.locked_until : nil
  end
end

.update(klass, query, setter) ⇒ Boolean

Update the document for the provided Class matching the provided query with the provided setter.

Parameters:

  • The (Class)

    model class

  • The (Hash)

    Mongoid query

  • The (Hash)

    Mongoid setter

Returns:

  • (Boolean)

    true if the document was successfully updated, false otherwise



13
14
15
16
17
18
19
20
21
22
# File 'lib/mongoid/locker/wrapper.rb', line 13

def self.update(klass, query, setter)
  error_obj =
    if IS_OLD_MONGOID
      klass.collection.update(query, setter, safe: true)
    else
      klass.with(safe: true).collection.find(query).update(setter)
    end

  error_obj['n'] == 1
end