Class: Faalis::Discovery::Permissions

Inherits:
Object
  • Object
show all
Defined in:
lib/faalis/discovery/permissions.rb

Overview

This class is responsible to discoering all Permissions classes and provide a list of available permissions

Class Method Summary collapse

Class Method Details

.all_permissionsObject



27
28
29
30
31
32
33
34
# File 'lib/faalis/discovery/permissions.rb', line 27

def self.all_permissions
  permissions = []
  permission_objects do |object|
    permissions.concat(object.permission_strings(object))
  end

  permissions.uniq
end

.create_all_permissionsObject

Creating all possible permissions in database using ‘Faalis::Permission` model



38
39
40
41
42
43
44
45
46
# File 'lib/faalis/discovery/permissions.rb', line 38

def self.create_all_permissions

  permission_objects do |object|
    object.possible_permissions.each do |perm|
      Faalis::Permission.where(model: object.to_s,
                               permission_type: perm).first_or_create
    end
  end
end

.permission_objects(&block) ⇒ Object

Run the given block for each object with permissions



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/faalis/discovery/permissions.rb', line 8

def self.permission_objects(&block)
  objects = []

  # Walkthrough all models in application by using
  # model_discovery gem and check for `permission_strings`
  # method in models (If `Faalis::Permissions` included in
  # model it has the permission_strings). And concat the
  # permissions to `@permissions` instance method.
  ::ApplicationModels.to_adapter.find_all.each do |m|
    # `m` is a string contains name of a model and
    # we use contstantize to get the model class with
    # such name
    model = m.model.constantize
    if model.respond_to? :permission_strings
      block.call(model)
    end
  end
end