/
reports_state_controller.rb
72 lines (60 loc) · 1.9 KB
/
reports_state_controller.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
# frozen_string_literal: true
class Staff::ReportsStateController < Staff::BaseController
include Secured
STATE_TO_POLICY_ACTION = {
"active" => "activate",
"submitted" => "submit",
"in_review" => "review",
"awaiting_changes" => "request_changes",
"approved" => "approve"
}
def edit
case report.state
when "active"
show_state_change_confirmation(:submit)
when "submitted"
show_state_change_confirmation(:review)
when "in_review"
params[:request_changes] ? show_state_change_confirmation(:request_changes) : show_state_change_confirmation(:approve)
when "awaiting_changes"
show_state_change_confirmation(:submit)
else
authorize report
redirect_to report_path(report)
end
end
def update
state = params[:state]
Report.transaction do
if STATE_TO_POLICY_ACTION.key?(state)
change_report_state_to(state)
else
authorize report
redirect_to report_path(report)
end
end
end
private def show_state_change_confirmation(policy_action)
authorize report, policy_action.to_s + "?"
@report_presenter = ReportPresenter.new(report)
render "staff/reports_state/#{policy_action}/confirm"
end
private def change_report_state_to(state)
policy_action = STATE_TO_POLICY_ACTION.fetch(state)
unless report.valid?(policy_action.to_sym)
authorize report
flash[:error] = {title: t("action.report.#{policy_action}.failure"), errors: report.errors}
return redirect_to report_path(report)
end
unless report.state == state
authorize report, policy_action + "?"
report.update!(state: state)
Report::SendStateChangeEmails.new(report).send!
end
@report_presenter = ReportPresenter.new(report)
render "staff/reports_state/#{policy_action}/complete"
end
private def report
@report ||= Report.lock.find(params[:report_id])
end
end