/
facility_group_policy.rb
66 lines (52 loc) · 1.2 KB
/
facility_group_policy.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Manage::FacilityGroupPolicy < ApplicationPolicy
def index?
user.user_permissions
.where(permission_slug: [:manage_facility_groups])
.present?
end
def show?
user_has_any_permissions?(
[:manage_facility_groups, nil],
[:manage_facility_groups, record.organization],
[:manage_facility_groups, record]
)
end
def create?
user.user_permissions
.where(permission_slug: [:manage_facility_groups])
.present?
end
def new?
create?
end
def update?
show?
end
def edit?
update?
end
def destroy?
destroyable? && update?
end
def whatsapp_graphics?
show?
end
private
def destroyable?
record.facilities.none? && record.patients.none? && record.blood_pressures.none?
end
class Scope < Scope
attr_reader :user, :scope
def initialize(user, scope)
super
@user = user
@scope = scope
end
def resolve
return scope.none unless user.has_permission?(:manage_facility_groups)
facility_group_ids = facility_group_ids_for_permission(:manage_facility_groups)
return scope.all if facility_group_ids.empty?
scope.where(id: facility_group_ids)
end
end
end