Class: Rabarber::Role

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rabarber/models/role.rb

Class Method Summary collapse

Class Method Details

.add(name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rabarber/models/role.rb', line 16

def add(name)
  name = process_role_name(name)

  return false if exists?(name: name)

  !!create!(name: name)
end

.assignees(name) ⇒ Object



45
46
47
48
49
# File 'lib/rabarber/models/role.rb', line 45

def assignees(name)
  Rabarber::HasRoles.roleable_class.joins(:rabarber_roles).where(
    rabarber_roles: { name: Rabarber::Input::Role.new(name).process }
  )
end

.namesObject



12
13
14
# File 'lib/rabarber/models/role.rb', line 12

def names
  pluck(:name).map(&:to_sym)
end

.remove(name, force: false) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rabarber/models/role.rb', line 35

def remove(name, force: false)
  role = find_by(name: process_role_name(name))

  return false if !role || assigned_to_roleables(role).any? && !force

  delete_roleables_cache(role)

  !!role.destroy!
end

.rename(old_name, new_name, force: false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rabarber/models/role.rb', line 24

def rename(old_name, new_name, force: false)
  role = find_by(name: process_role_name(old_name))
  name = process_role_name(new_name)

  return false if !role || exists?(name: name) || assigned_to_roleables(role).any? && !force

  delete_roleables_cache(role)

  role.update!(name: name)
end