/
facility_policy.rb
72 lines (58 loc) · 1.45 KB
/
facility_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
67
68
69
70
71
72
class Manage::Facility::FacilityPolicy < ApplicationPolicy
def index?
user.user_permissions
.where(permission_slug: [:manage_facilities, :manage_facility_groups])
.present?
end
def show?
user_has_any_permissions?(
[:manage_facilities, nil],
[:manage_facilities, record.organization],
[:manage_facilities, record.facility_group]
)
end
def share_anonymized_data?
user_has_any_permissions?([:manage_organizations, nil])
end
def create?
user_has_any_permissions?(
[:manage_facilities, nil],
[:manage_facilities, record.organization],
[:manage_facilities, record.facility_group]
)
end
def new?
create?
end
def update?
create?
end
def edit?
update?
end
def destroy?
destroyable? && create?
end
def upload?
user.user_permissions
.where(permission_slug: [:manage_facilities])
.present?
end
def destroyable?
record.registered_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_facilities, :manage_facility_groups])
facility_ids = facility_ids_for_permission([:manage_facilities, :manage_facility_groups])
return scope.all if facility_ids.empty?
scope.where(id: facility_ids)
end
end
end