Class: RbacCore::PermissionSet

Inherits:
OptionsModel::Base
  • Object
show all
Defined in:
lib/rbac_core/permission_set.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.draw(**constraints, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rbac_core/permission_set.rb', line 35

def draw(**constraints, &block)
  unless block_given?
    raise ArgumentError, "must provide a block"
  end

  Mapper.new(self, constraints).instance_exec(&block)

  self
end

.i18n_scopeObject



19
20
21
# File 'lib/rbac_core/permission_set.rb', line 19

def i18n_scope
  :rbac_core
end

.permission_classObject



23
24
25
# File 'lib/rbac_core/permission_set.rb', line 23

def permission_class
  @permission_class || RbacCore.permission_class
end

.permission_class=(klass) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rbac_core/permission_set.rb', line 27

def permission_class=(klass)
  unless klass && klass < Permission
    raise ArgumentError, "#{klass} should be sub-class of #{Permission}."
  end

  @permission_class = klass
end

.register_permission(name, default = false, **options, &block) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/rbac_core/permission_set.rb', line 49

def register_permission(name, default = false, **options, &block)
  raise ArgumentError, "`name` can't be blank" if name.blank?

  attribute name, :boolean, default: default
  registered_permissions[name] = permission_class.new name, options, &block
end

.registered_permissionsObject



45
46
47
# File 'lib/rbac_core/permission_set.rb', line 45

def registered_permissions
  @registered_permissions ||= ActiveSupport::HashWithIndifferentAccess.new
end

Instance Method Details

#permitted_permission_namesObject



5
6
7
# File 'lib/rbac_core/permission_set.rb', line 5

def permitted_permission_names
  attributes.select { |_, v| v }.keys
end

#permitted_permissions(include_nested: true) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/rbac_core/permission_set.rb', line 9

def permitted_permissions(include_nested: true)
  permissions = self.class.registered_permissions.slice(*permitted_permission_names).values
  if include_nested && nested_attributes.any?
    permissions.concat nested_attributes.values.map(&:permitted_permissions).flatten!
  end

  ComputedPermissions.new(permissions)
end