Module: MongoidAbility::Lock

Defined in:
lib/mongoid_ability/lock.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongoid_ability/lock.rb', line 4

def self.included base
  base.extend ClassMethods
  base.class_eval do
    field :action, type: Symbol, default: :read
    field :outcome, type: Boolean, default: false

    # ---------------------------------------------------------------------
    
    belongs_to :subject, polymorphic: true, touch: true

    # ---------------------------------------------------------------------
      
    validates :action, presence: true, uniqueness: { scope: [ :subject_type, :subject_id, :outcome ] }
    validates :outcome, presence: true

    # ---------------------------------------------------------------------
      
    scope :for_action, -> action { where(action: action.to_sym) }

    scope :for_subject_type, -> subject_type { where(subject_type: subject_type.to_s) }
    scope :for_subject_id, -> subject_id { where(subject_id: subject_id) }
    scope :for_subject, -> subject { where(subject_type: subject.class.model_name, subject_id: subject.id) }

    scope :class_locks, -> { where(subject_id: nil) }
    scope :id_locks, -> { ne(subject_id: nil) }
  end
end

Instance Method Details

#calculated_outcome ⇒ Object

=====================================================================



39
40
41
# File 'lib/mongoid_ability/lock.rb', line 39

def calculated_outcome
  self.outcome
end

#class_lock? ⇒ Boolean


Returns:

  • (Boolean)


55
56
57
# File 'lib/mongoid_ability/lock.rb', line 55

def class_lock?
  !id_lock?
end

#closed? ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mongoid_ability/lock.rb', line 49

def closed?
  !open?
end

#conditions ⇒ Object




65
66
67
68
69
70
# File 'lib/mongoid_ability/lock.rb', line 65

def conditions
  res = { _type: subject_type }
  res = res.merge(_id: subject_id) if subject_id.present?
  res = { '$not' => res } if calculated_outcome == false
  res
end

#id_lock? ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mongoid_ability/lock.rb', line 59

def id_lock?
  self.subject_id.present?
end

#open? ⇒ Boolean


Returns:

  • (Boolean)


45
46
47
# File 'lib/mongoid_ability/lock.rb', line 45

def open?
  self.calculated_outcome == true
end