Class: Apicasso::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/apicasso/ability.rb

Overview

Ability to parse a scope object from Apicasso::Key

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Ability

Returns a new instance of Ability.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/apicasso/ability.rb', line 8

def initialize(key)
  key ||= Apicasso::Key.new
  cannot :manage, :all
  cannot :read, :all
  key.scope.each do |permission, klasses_clearances|
    klasses_clearances.each do |klass, clearance|
      if clearance == true
        # Usage:
        # To have a key reading all channels and all accouts
        # you would have a scope:
        # => `{read: {channel: true, accout: true}}`
        can permission.to_sym, klass.underscore.singularize.to_sym
        can permission.to_sym, klass.classify.constantize
      elsif clearance.class == Hash
        # Usage:
        # To have a key reading all banners from a channel with id 999
        # you would have a scope:
        # => `{read: {banner: {owner_id: [999]}}}`
        can permission.to_sym,
            klass.underscore.singularize.to_sym
        clearance.each do |by_field, values|
          can permission.to_sym,
              klass.classify.constantize,
              by_field.to_sym => values
        end
      end
    end
  end
end