Class: Role

Inherits:
Object
  • Object
show all
Defined in:
lib/rad/models/role.rb

Constant Summary collapse

ORDERED_ROLES =
%w{manager member user}
SYSTEM_ROLES =
%w{admin anonymous manager member owner registered user}.sort.freeze
PRESERVED_USER_NAMES =
(SYSTEM_ROLES + ['admin']).sort.freeze

Class Method Summary collapse

Class Method Details

.denormalize_to_higher_roles(roles) ⇒ Object



14
15
16
17
18
# File 'lib/rad/models/role.rb', line 14

def denormalize_to_higher_roles roles
  ordinary_roles, ordered_roles = split roles
  ordinary_roles.push *higher_roles(lower_role(ordered_roles))
  ordinary_roles.sort
end

.denormalize_to_lower_roles(roles) ⇒ Object



20
21
22
23
24
# File 'lib/rad/models/role.rb', line 20

def denormalize_to_lower_roles roles
  ordinary_roles, ordered_roles = split roles
  ordinary_roles.push *lower_roles(higher_role(ordered_roles))
  ordinary_roles.sort
end

.higher_role(roles) ⇒ Object



26
27
28
29
30
31
# File 'lib/rad/models/role.rb', line 26

def higher_role roles
  ORDERED_ROLES.each do |role|
    return role if roles.include? role
  end
  nil
end

.lower_role(roles) ⇒ Object



33
34
35
36
37
38
# File 'lib/rad/models/role.rb', line 33

def lower_role roles
  ORDERED_ROLES.reverse.each do |role|
    return role if roles.include? role
  end
  nil
end

.major_roles(roles) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rad/models/role.rb', line 40

def major_roles roles
  major_roles = roles.select{|role| !SYSTEM_ROLES.include?(role)}
  if higher_role = higher_role(roles)
    major_roles << higher_role
  end
  major_roles.sort
end

.minor_roles(roles) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rad/models/role.rb', line 48

def minor_roles roles
  minor_roles = roles.select{|role| !SYSTEM_ROLES.include?(role)}
  if lower_role = lower_role(roles)
    minor_roles << lower_role
  end
  minor_roles.sort
end

.normalize_roles(roles) ⇒ Object



8
9
10
11
12
# File 'lib/rad/models/role.rb', line 8

def normalize_roles roles    
  ordinary_roles, ordered_roles = split roles
  ordinary_roles << lower_role(ordered_roles)    
  ordinary_roles.sort
end