Module: MongoidAbility::Lock
- Defined in:
- lib/mongoid_ability/lock.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#calculated_outcome(options = {}) ⇒ Object
NOTE: override for more complicated results.
- #class_lock? ⇒ Boolean
- #closed?(options = {}) ⇒ Boolean
-
#conditions ⇒ Object
NOTE: override for more complicated results.
-
#default_options ⇒ Object
this is used when calculating inherited outcome.
- #id_lock? ⇒ Boolean
-
#inherited_outcome(options = default_options) ⇒ Object
calculates outcome as if this lock is not present.
- #open?(options = {}) ⇒ Boolean
-
#subject_class ⇒ Object
———————————————————————.
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mongoid_ability/lock.rb', line 5 def self.included base base.extend ClassMethods base.class_eval do field :action, type: Symbol, default: :read field :outcome, type: Boolean, default: false field :options, type: Hash, default: {} belongs_to :subject, polymorphic: true, touch: true # TODO: validate that action is defined on subject or its superclasses 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.presence) } 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(options = {}) ⇒ Object
NOTE: override for more complicated results
32 33 34 |
# File 'lib/mongoid_ability/lock.rb', line 32 def calculated_outcome ={} outcome end |
#class_lock? ⇒ Boolean
71 72 73 |
# File 'lib/mongoid_ability/lock.rb', line 71 def class_lock? !id_lock? end |
#closed?(options = {}) ⇒ Boolean
67 68 69 |
# File 'lib/mongoid_ability/lock.rb', line 67 def closed? ={} !open?() end |
#conditions ⇒ Object
NOTE: override for more complicated results
37 38 39 40 41 42 |
# File 'lib/mongoid_ability/lock.rb', line 37 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 |
#default_options ⇒ Object
this is used when calculating inherited outcome
53 54 55 |
# File 'lib/mongoid_ability/lock.rb', line 53 def {} end |
#id_lock? ⇒ Boolean
75 76 77 |
# File 'lib/mongoid_ability/lock.rb', line 75 def id_lock? subject_id.present? end |
#inherited_outcome(options = default_options) ⇒ Object
calculates outcome as if this lock is not present
45 46 47 48 49 50 |
# File 'lib/mongoid_ability/lock.rb', line 45 def inherited_outcome = return calculated_outcome() unless owner.present? cloned_owner = owner.clone cloned_owner.locks_relation = cloned_owner.locks_relation - [self] MongoidAbility::Ability.new(cloned_owner).can? action, (subject.present? ? subject : subject_class), end |
#open?(options = {}) ⇒ Boolean
63 64 65 |
# File 'lib/mongoid_ability/lock.rb', line 63 def open? ={} calculated_outcome() == true end |
#subject_class ⇒ Object
59 60 61 |
# File 'lib/mongoid_ability/lock.rb', line 59 def subject_class subject_type.constantize end |