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, context: nil) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rabarber/models/role.rb', line 19

def add(name, context: nil)
  name = process_role_name(name)
  processed_context = process_context(context)

  return false if exists?(name:, **processed_context)

  !!create!(name:, **processed_context)
end

.assignees(name, context: nil) ⇒ Object



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

def assignees(name, context: nil)
  Rabarber::HasRoles.roleable_class.joins(:rabarber_roles).where(
    rabarber_roles: { name: process_role_name(name), **process_context(context) }
  )
end

.names(context: nil) ⇒ Object



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

def names(context: nil)
  where(process_context(context)).pluck(:name).map(&:to_sym)
end

.remove(name, context: nil, force: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/rabarber/models/role.rb', line 40

def remove(name, context: nil, force: false)
  processed_context = process_context(context)
  role = find_by(name: process_role_name(name), **processed_context)

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

  delete_roleables_cache(role, context: processed_context)

  !!role.destroy!
end

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



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

def rename(old_name, new_name, context: nil, force: false)
  processed_context = process_context(context)
  role = find_by(name: process_role_name(old_name), **processed_context)
  name = process_role_name(new_name)

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

  delete_roleables_cache(role, context: processed_context)

  role.update!(name:)
end