Class: Rabarber::Role
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Rabarber::Role
- Defined in:
- lib/rabarber/models/role.rb
Class Method Summary collapse
- .add(name, context: nil) ⇒ Object
- .all_names ⇒ Object
- .assignees(name, context: nil) ⇒ Object
- .names(context: nil) ⇒ Object
- .remove(name, context: nil, force: false) ⇒ Object
- .rename(old_name, new_name, context: nil, force: false) ⇒ Object
Instance Method Summary collapse
Class Method Details
.add(name, context: nil) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/rabarber/models/role.rb', line 29 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 |
.all_names ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/rabarber/models/role.rb', line 21 def all_names includes(:context).group_by(&:context).transform_values { |roles| roles.map { _1.name.to_sym } } rescue ActiveRecord::RecordNotFound => e raise Rabarber::Error, "Context not found: #{e.model}##{e.id}" rescue NameError => e raise Rabarber::Error, "Context not found: #{e.name}" end |
.assignees(name, context: nil) ⇒ Object
61 62 63 64 65 |
# File 'lib/rabarber/models/role.rb', line 61 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
17 18 19 |
# File 'lib/rabarber/models/role.rb', line 17 def names(context: nil) where(process_context(context)).pluck(:name).map(&:to_sym) end |
.remove(name, context: nil, force: false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rabarber/models/role.rb', line 50 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
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rabarber/models/role.rb', line 38 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 |
Instance Method Details
#context ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/rabarber/models/role.rb', line 90 def context return context_type.constantize if context_type.present? && context_id.blank? record = super raise ActiveRecord::RecordNotFound.new(nil, context_type, nil, context_id) if context_id.present? && !record record end |