Class: Privilege
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Privilege
- Includes:
- Checkable, Toggleable
- Defined in:
- app/models/privilege.rb
Constant Summary collapse
- DESCRIPTION_LIMIT =
350- NAME_LIMIT =
250- SLUG_LIMIT =
250- PRIORITY_RANGE =
(1..32_767)
Class Method Summary collapse
Instance Method Summary collapse
- #branch_ids ⇒ Array<Integer>
- #cache_children! ⇒ Object
- #cache_parents! ⇒ Object
- #can_be_deleted? ⇒ Boolean
- #change_priority(delta) ⇒ Object
- #full_title ⇒ String
- #grant(user, region_id = nil) ⇒ Object
- #has_user?(user, region_ids = []) ⇒ Boolean
-
#ids ⇒ Object
deprecated
Deprecated.
use #subbranch_ids
- #parents ⇒ Object
- #revoke(user, region_id = nil) ⇒ Object
- #subbranch_ids ⇒ Array<Integer>
Class Method Details
.creation_parameters ⇒ Object
53 54 55 |
# File 'app/models/privilege.rb', line 53 def self.creation_parameters entity_parameters + %i[parent_id] end |
.entity_parameters ⇒ Object
49 50 51 |
# File 'app/models/privilege.rb', line 49 def self.entity_parameters %i[administrative description name priority regional slug] end |
.page_for_administration ⇒ Object
45 46 47 |
# File 'app/models/privilege.rb', line 45 def self.page_for_administration ordered_by_name end |
Instance Method Details
#branch_ids ⇒ Array<Integer>
73 74 75 |
# File 'app/models/privilege.rb', line 73 def branch_ids parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id] end |
#cache_children! ⇒ Object
92 93 94 95 96 97 |
# File 'app/models/privilege.rb', line 92 def cache_children! child_privileges.order('id asc').each do |child| self.children_cache += [child.id] + child.children_cache end save! end |
#cache_parents! ⇒ Object
85 86 87 88 89 90 |
# File 'app/models/privilege.rb', line 85 def cache_parents! return if parent.nil? self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '') save! end |
#can_be_deleted? ⇒ Boolean
99 100 101 |
# File 'app/models/privilege.rb', line 99 def can_be_deleted? deletable? && child_privileges.count < 1 end |
#change_priority(delta) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/privilege.rb', line 138 def change_priority(delta) new_priority = priority + delta criteria = { priority: new_priority } adjacent = self.class.siblings(self).find_by(criteria) if adjacent.is_a?(self.class) && (adjacent.id != id) adjacent.update!(priority: priority) end update(priority: new_priority) self.class.for_tree(parent_id).map { |e| [e.id, e.priority] }.to_h end |
#full_title ⇒ String
58 59 60 |
# File 'app/models/privilege.rb', line 58 def full_title (parents.map(&:name) + [name]).join ' / ' end |
#grant(user, region_id = nil) ⇒ Object
119 120 121 122 123 124 125 |
# File 'app/models/privilege.rb', line 119 def grant(user, region_id = nil) return if user.nil? criteria = { privilege: self, user: user } criteria[:region_id] = region_id if regional? UserPrivilege.create(criteria) unless UserPrivilege.exists?(criteria) end |
#has_user?(user, region_ids = []) ⇒ Boolean
105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/privilege.rb', line 105 def has_user?(user, region_ids = []) return false if user.nil? return true if user.super_user? result = user_in_non_regional_branch?(user) if regional? && region_ids.any? && !result result = user_in_regional_branch?(user, region_ids) end result end |
#ids ⇒ Object
Deprecated.
use #subbranch_ids
63 64 65 |
# File 'app/models/privilege.rb', line 63 def ids subbranch_ids end |
#parents ⇒ Object
77 78 79 80 81 82 83 |
# File 'app/models/privilege.rb', line 77 def parents if parents_cache.blank? [] else Privilege.where(id: parents_cache.split(',').compact).order('id asc') end end |
#revoke(user, region_id = nil) ⇒ Object
129 130 131 132 133 134 135 |
# File 'app/models/privilege.rb', line 129 def revoke(user, region_id = nil) return if user.nil? criteria = { privilege: self, user: user } criteria[:region_id] = region_id if regional? UserPrivilege.where(criteria).delete_all end |
#subbranch_ids ⇒ Array<Integer>
68 69 70 |
# File 'app/models/privilege.rb', line 68 def subbranch_ids [id] + children_cache end |