Class: Rabarber::Role
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Rabarber::Role
- Defined in:
- lib/rabarber/models/role.rb
Class Method Summary collapse
- .amend(old_name, new_name, context: nil, force: false) ⇒ Object
- .drop(name, context: nil, force: false) ⇒ Object
- .list(context: nil) ⇒ Object
- .list_all ⇒ Object
- .prune ⇒ Object
- .register(name, context: nil) ⇒ Object
Instance Method Summary collapse
Class Method Details
.amend(old_name, new_name, context: nil, force: false) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rabarber/models/role.rb', line 35 def amend(old_name, new_name, context: nil, force: false) 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 role.update!(name:) delete_roleables_cache(role.roleables.pluck(:id), context: processed_context) true rescue ActiveRecord::RecordNotUnique false end |
.drop(name, context: nil, force: false) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rabarber/models/role.rb', line 54 def drop(name, context: nil, force: false) 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 roleable_ids = role.roleables.pluck(:id) role.destroy! delete_roleables_cache(roleable_ids, context: processed_context) true end |
.list(context: nil) ⇒ Object
10 11 12 |
# File 'lib/rabarber/models/role.rb', line 10 def list(context: nil) where(process_context(context)).pluck(:name).map(&:to_sym) end |
.list_all ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/rabarber/models/role.rb', line 14 def list_all 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 |
.prune ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rabarber/models/role.rb', line 71 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 Rabarber::Core::Cache.clear end |
.register(name, context: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rabarber/models/role.rb', line 24 def register(name, context: nil) name = process_role_name(name) processed_context = process_context(context) return false if exists?(name:, **processed_context) !!create!(name:, **processed_context) rescue ActiveRecord::RecordNotUnique false end |
Instance Method Details
#context ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/rabarber/models/role.rb', line 114 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 |