-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoderation_policy.rb
51 lines (43 loc) · 1.07 KB
/
moderation_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
class ModerationPolicy < ApplicationPolicy
def index?
logged_in?
end
def show?
moderator? || admin?
end
def create?
logged_in?
end
def update?
moderator? || admin?
end
def destroy?
moderator? || admin?
end
def can_action?(action)
available_actions.include? action.to_sym
end
def available_actions
return [] unless logged_in?
@available_actions ||= target.class.moderatable.select do |action, roles|
roles[:all] ||
(roles[:moderator] && moderator?) ||
(roles[:admin] && admin?) ||
(roles[:team] && team?)
end.keys
end
def target
Array.wrap(record).first.target || (raise ActiveRecord::RecordNotFound.new("Couldn't find target"))
end
class Scope < Scope
def resolve
if user.roles.where(section: 'zooniverse', name: ['moderator', 'admin']).any?
scope
elsif privileged_sections('moderator', 'admin').any?
scope.where 'section = any(array[:sections])', sections: privileged_sections('moderator', 'admin')
else
scope.none
end
end
end
end