-
Notifications
You must be signed in to change notification settings - Fork 25
/
team.rb
48 lines (38 loc) · 1.19 KB
/
team.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
class Team < ApplicationRecord
validates :name,
presence: true,
uniqueness: true
before_save :write_permissions
before_validation :validate_permissions
def permissions
@permissions ||= permission_set_class.load self[:permissions]
end
def permissions_attributes=(value)
permissions.replace value
end
delegate :computed_permissions, to: :permissions
private
def permission_set_class
@permission_set_class ||=
begin
# You MAY need to cache this if you meet N+1 issue here.
# Consider caching at app start.
# Don't forget to refresh the cache when permission definition changed.
dynamic_permissions = DynamicPermission.all
RoleCore::PermissionSet.derive("DynamicPermissionSet").draw do
dynamic_permissions.each do |dp|
permission dp.key, default: dp.default?
end
end
end
end
def write_permissions
self[:permissions] = permission_set_class.dump(permissions)
end
def validate_permissions
unless permissions.valid?
errors.add :permissions, :invalid
end
end
end