/
refunds_controller.rb
113 lines (87 loc) · 2.53 KB
/
refunds_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class Staff::RefundsController < Staff::ActivitiesController
include Secured
include Activities::Breadcrumbed
def new
@activity = activity
@refund = RefundForm.new
@refund.parent_activity = @activity
@refund.report = @report
authorize(@refund, policy_class: RefundPolicy)
prepare_default_activity_trail(@activity)
add_breadcrumb t("breadcrumb.refund.new"), new_activity_refund_path(@activity)
end
def create
@activity = activity
authorize @activity
@refund = RefundForm.new(refund_params)
return render :new unless @refund.valid?
CreateRefund.new(
activity: @activity,
user: current_user
).call(attributes: @refund.attributes)
flash[:notice] = t("action.refund.create.success")
redirect_to organisation_activity_path(@activity.organisation, @activity)
end
def edit
@activity = activity
@refund = RefundForm.new(attributes_for_editing)
authorize(@refund, policy_class: RefundPolicy)
prepare_default_activity_trail(@activity)
add_breadcrumb t("breadcrumb.refund.edit"), edit_activity_refund_path(@activity, @refund.id)
end
def update
@refund = RefundForm.new(attributes_for_editing.merge(refund_params))
authorize(@refund, policy_class: RefundPolicy)
return render :edit unless @refund.valid?
result = UpdateRefund.new(
refund: Refund.find(id),
user: current_user
).call(attributes: refund_params)
if result.success?
flash[:notice] = t("action.refund.update.success")
redirect_to organisation_activity_path(activity.organisation, activity)
else
render :edit
end
end
def destroy
@refund = Refund.find(id)
authorize @refund
@refund.destroy
flash[:notice] = t("action.refund.destroy.success")
redirect_to organisation_activity_path(activity.organisation, activity)
end
private
def refund_params
params.require(:refund_form).permit(
:value,
:financial_quarter,
:financial_year,
:comment
)
end
def attributes_for_editing
refund = Refund.find(id)
HashWithIndifferentAccess
.new
.merge(refund.attributes.slice(
"id",
"value",
"financial_year",
"financial_quarter"
))
.merge(report: refund.report,
parent_activity: refund.parent_activity,
comment: refund.comment.body)
.merge(persisted: true)
end
def activity_id
params[:activity_id]
end
def id
params[:id]
end
def activity
@activity ||= Activity.find(activity_id)
end
end