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
- .prune ⇒ 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rabarber/models/role.rb', line 32 def add(name, context: nil) deprecation_warning("add", "Rabarber.create_role") 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
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rabarber/models/role.rb', line 20 def all_names deprecation_warning("all_names", "Rabarber.all_roles") includes(:context).each_with_object({}) do |role, hash| (hash[role.context] ||= []) << role.name.to_sym rescue ActiveRecord::RecordNotFound next end rescue NameError => e raise Rabarber::NotFoundError, "Context not found: class #{e.name} may have been renamed or deleted" end |
.assignees(name, context: nil) ⇒ Object
75 76 77 78 79 |
# File 'lib/rabarber/models/role.rb', line 75 def assignees(name, context: nil) deprecation_warning("assignees", "#{Rabarber::Configuration.user_model_name}.with_role") find_by(name: process_role_name(name), **process_context(context))&.roleables || Rabarber::Configuration.user_model.none end |
.names(context: nil) ⇒ Object
14 15 16 17 18 |
# File 'lib/rabarber/models/role.rb', line 14 def names(context: nil) deprecation_warning("names", "Rabarber.roles") where(process_context(context)).pluck(:name).map(&:to_sym) end |
.prune ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rabarber/models/role.rb', line 81 def prune orphaned_roles = where.not(context_id: nil).includes(:context).filter_map do |role| !role.context rescue ActiveRecord::RecordNotFound role.id end return if orphaned_roles.empty? ActiveRecord::Base.transaction do ActiveRecord::Base.connection.execute( ActiveRecord::Base.sanitize_sql( ["DELETE FROM rabarber_roles_roleables WHERE role_id IN (?)", orphaned_roles] ) ) where(id: orphaned_roles).delete_all end end |
.remove(name, context: nil, force: false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rabarber/models/role.rb', line 60 def remove(name, context: nil, force: false) deprecation_warning("remove", "Rabarber.delete_role") processed_context = process_context(context) role = find_by(name: process_role_name(name), **processed_context) raise Rabarber::NotFoundError, "Role not found" unless role return false if role.roleables.exists? && !force delete_roleables_cache(role, context: processed_context) !!role.destroy! end |
.rename(old_name, new_name, context: nil, force: false) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rabarber/models/role.rb', line 43 def rename(old_name, new_name, context: nil, force: false) deprecation_warning("rename", "Rabarber.rename_role") processed_context = process_context(context) role = find_by(name: process_role_name(old_name), **processed_context) raise Rabarber::NotFoundError, "Role not found" unless role name = process_role_name(new_name) return false if exists?(name:, **processed_context) || role.roleables.exists? && !force delete_roleables_cache(role, context: processed_context) role.update!(name:) end |
Instance Method Details
#context ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/rabarber/models/role.rb', line 130 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 |