Module: Authorizable::PermissionUtilities

Defined in:
lib/authorizable/permission_utilities.rb

Constant Summary collapse

KIND =
0
DEFAULT_ACCESS =
1
DESCRIPTION =
2
VISIBILITY_PROC =
3
ACCESS_PROC =
4
OBJECT =
0
ACCESS =
1

Class Method Summary collapse

Class Method Details

.has_key?(permission) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/authorizable/permission_utilities.rb', line 61

def self.has_key?(permission)
  permissions[permission.to_sym].present?
end

.has_procs?(permission) ⇒ Boolean

returns procs or false

Returns:

  • (Boolean)

27
28
29
# File 'lib/authorizable/permission_utilities.rb', line 27

def self.has_procs?(permission)
  permission_data_helper(permission, ACCESS_PROC)
end

.has_visibility_procs?(permission) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/authorizable/permission_utilities.rb', line 31

def self.has_visibility_procs?(permission)
  permission_data_helper(permission, VISIBILITY_PROC)
end

.is_access?(permission) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/authorizable/permission_utilities.rb', line 65

def self.is_access?(permission)
  permissions[permission][KIND] == ACCESS
end

.is_object?(permission) ⇒ Boolean

Returns:

  • (Boolean)

69
70
71
# File 'lib/authorizable/permission_utilities.rb', line 69

def self.is_object?(permission)
  permissions[permission][KIND] == OBJECT
end

.name_for(permission) ⇒ Object


46
47
48
49
50
51
52
53
54
# File 'lib/authorizable/permission_utilities.rb', line 46

def self.name_for(permission)
  result = permission_data_helper(permission.to_sym, DESCRIPTION)

  if !result
    result = permission.to_s.humanize
  end

  result
end

.permissionsObject


14
15
16
# File 'lib/authorizable/permission_utilities.rb', line 14

def self.permissions
  Authorizable::Permissions.definitions
end

.set_for_role(role) ⇒ Object


18
19
20
21
22
23
24
# File 'lib/authorizable/permission_utilities.rb', line 18

def self.set_for_role(role)
  permissions.inject({}) { |h,(k, v)|
    value = v[DEFAULT_ACCESS]
    h[k.to_s] = value.is_a?(Array) ? value[role] : value
    h
  }
end

.should_render?(permission, *args) ⇒ Boolean

Returns:

  • (Boolean)

35
36
37
38
39
40
41
42
43
44
# File 'lib/authorizable/permission_utilities.rb', line 35

def self.should_render?(permission, *args)
  result = true
  proc = self.has_visibility_procs?(permission)

  if proc
    result = proc.call(*args)
  end

  result
end

.value_for(permission, role) ⇒ Object


56
57
58
59
# File 'lib/authorizable/permission_utilities.rb', line 56

def self.value_for(permission, role)
  value = permissions[permission.to_sym][DEFAULT_ACCESS]
  value.is_a?(Array) ? value[role] : value
end