Class: Consent::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
lib/consent/ability.rb

Overview

Defines a CanCan(Can)::Ability class based on a permissions hash

Instance Method Summary collapse

Constructor Details

#initialize(*args, apply_defaults: true) ⇒ Ability

Returns a new instance of Ability.



8
9
10
11
# File 'lib/consent/ability.rb', line 8

def initialize(*args, apply_defaults: true)
  @context = *args
  apply_defaults! if apply_defaults
end

Instance Method Details



27
28
29
30
31
# File 'lib/consent/ability.rb', line 27

def consent(**kwargs)
  consent!(**kwargs)
rescue Consent::ViewNotFound
  nil
end

#consent!(subject: nil, action: nil, view: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/consent/ability.rb', line 13

def consent!(subject: nil, action: nil, view: nil)
  view = case view
         when Consent::View
           view
         when Symbol
           Consent.find_view(subject, action, view)
         end

  can(
    action, subject,
    view&.conditions(*@context), &view&.object_conditions(*@context)
  )
end