Class: Role

Inherits:
ActiveYaml::Base
  • Object
show all
Includes:
ActiveYaml::Aliases
Defined in:
lib/models/role.rb

Defined Under Namespace

Classes: AbilityGenerator, Collection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adminObject



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

def self.admin
  find_by_key("admin")
end

.assignableObject



39
40
41
# File 'lib/models/role.rb', line 39

def self.assignable
  all.reject(&:default?)
end

.defaultObject



18
19
20
# File 'lib/models/role.rb', line 18

def self.default
  find_by_key("default")
end

.find(key) ⇒ Object



43
44
45
# File 'lib/models/role.rb', line 43

def self.find(key)
  all.find { |role| role.key == key.to_s }
end

.find_by_key(key) ⇒ Object

Allow us to use either symbols or strings when searching



23
24
25
# File 'lib/models/role.rb', line 23

def self.find_by_key(key)
  super(key.to_s)
end

.includes(role_or_key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/models/role.rb', line 27

def self.includes(role_or_key)
  role_key = role_or_key.is_a?(Role) ? role_or_key.key : role_or_key.to_s
  role = Role.find_by_key(role_key)
  return [] if role.nil?
  return Role.all.select(&:assignable?) if role.default?
  result = []
  all.each do |role|
    result << role if role.includes.include?(role_key)
  end
  result
end

Instance Method Details

#ability_generator(user, through, parent, intermediary) ⇒ Object



98
99
100
101
102
103
# File 'lib/models/role.rb', line 98

def ability_generator(user, through, parent, intermediary)
  models.each do |model_name, _|
    ag = AbilityGenerator.new(self, model_name, user, through, parent, intermediary)
    yield(ag)
  end
end

#admin?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/models/role.rb', line 71

def admin?
  key == "admin"
end

#assignable?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/models/role.rb', line 94

def assignable?
  !default?
end

#default?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/models/role.rb', line 67

def default?
  key == "default"
end

#idObject

We don’t want to ever use the automatically generated ids from ActiveYaml. These are created based on the order of objects in the yml file so if someone ever changed the order of that file around, they would really mess up the user permissions. Instead, we’re using the key attribute.



49
50
51
# File 'lib/models/role.rb', line 49

def id
  key
end

#included_byObject



57
58
59
# File 'lib/models/role.rb', line 57

def included_by
  Role.includes(self)
end

#included_rolesObject



75
76
77
78
79
# File 'lib/models/role.rb', line 75

def included_roles
  default_roles = []
  default_roles << Role.default unless default?
  (default_roles + includes.map { |included_key| Role.find_by_key(included_key) }).uniq.compact
end

#key_plus_included_by_keysObject

We need to search for memberships that have this role included directly OR any memberships that have this role through it being included in another role they have



62
63
64
65
# File 'lib/models/role.rb', line 62

def key_plus_included_by_keys
  # get direct parent roles
  (included_by.map(&:key_plus_included_by_keys).flatten + [id]).uniq
end

#manageable_by?(role_or_roles) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/models/role.rb', line 81

def manageable_by?(role_or_roles)
  return true if default?
  roles = role_or_roles.is_a?(Array) ? role_or_roles : [role_or_roles]
  roles.each do |role|
    return true if role.manageable_roles.include?(key)
    role.included_roles.each do |included_role|
      return true if manageable_by?([included_role])
    end
  end

  false
end

#to_sObject



53
54
55
# File 'lib/models/role.rb', line 53

def to_s
  key
end