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).freeze
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
52 53 54 |
# File 'app/models/privilege.rb', line 52 def self.creation_parameters entity_parameters + i[parent_id] end |
.entity_parameters ⇒ Object
48 49 50 |
# File 'app/models/privilege.rb', line 48 def self.entity_parameters i[administrative description name priority slug] end |
.page_for_administration ⇒ Object
44 45 46 |
# File 'app/models/privilege.rb', line 44 def self.page_for_administration ordered_by_name end |
Instance Method Details
#branch_ids ⇒ Array<Integer>
72 73 74 |
# File 'app/models/privilege.rb', line 72 def branch_ids parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id] end |
#cache_children! ⇒ Object
91 92 93 94 95 96 |
# File 'app/models/privilege.rb', line 91 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
84 85 86 87 88 89 |
# File 'app/models/privilege.rb', line 84 def cache_parents! return if parent.nil? self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '') save! end |
#can_be_deleted? ⇒ Boolean
98 99 100 |
# File 'app/models/privilege.rb', line 98 def can_be_deleted? deletable? && child_privileges.count < 1 end |
#change_priority(delta) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/privilege.rb', line 130 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
57 58 59 |
# File 'app/models/privilege.rb', line 57 def full_title (parents.map(&:name) + [name]).join ' / ' end |
#grant(user, region_id = nil) ⇒ Object
113 114 115 116 117 118 |
# File 'app/models/privilege.rb', line 113 def grant(user, region_id = nil) return if user.nil? criteria = { privilege: self, user: user } UserPrivilege.create(criteria) unless UserPrivilege.exists?(criteria) end |
#has_user?(user, region_ids = []) ⇒ Boolean
104 105 106 107 108 109 |
# File 'app/models/privilege.rb', line 104 def has_user?(user, region_ids = []) return false if user.nil? return true if user.super_user? user_in_non_regional_branch?(user) end |
#ids ⇒ Object
Deprecated.
use #subbranch_ids
62 63 64 |
# File 'app/models/privilege.rb', line 62 def ids subbranch_ids end |
#parents ⇒ Object
76 77 78 79 80 81 82 |
# File 'app/models/privilege.rb', line 76 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
122 123 124 125 126 127 |
# File 'app/models/privilege.rb', line 122 def revoke(user, region_id = nil) return if user.nil? criteria = { privilege: self, user: user } UserPrivilege.where(criteria).delete_all end |
#subbranch_ids ⇒ Array<Integer>
67 68 69 |
# File 'app/models/privilege.rb', line 67 def subbranch_ids [id] + children_cache end |