Skip to content

Commit 431c101

Browse files
committed
Implement feedback
1 parent 98a3a79 commit 431c101

10 files changed

Lines changed: 86 additions & 38 deletions

File tree

app/controllers/event/tours/reports_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def permitted_attrs
3535
:remarks,
3636
participations_attributes: [:state, :means_of_transport],
3737
revenues_attributes: [:id, :description, :count, :amount, :_destroy],
38-
expenditures_attributes: [:id, :description, :count, :amount, :_destroy],
38+
expenses_attributes: [:id, :description, :count, :amount, :_destroy],
3939
receipts_attributes: [:id, :description, :file, :_destroy]
4040
)
4141
end

app/models/event/cost.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class Event::Cost < ApplicationRecord
1212
validates :description, :count, :amount, presence: true
1313

1414
def total
15+
return 0 if amount.nil? || count.nil?
16+
1517
amount * count
1618
end
1719
end

app/models/event/tour/report_form.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def revenues
5151
@revenues ||= report.costs.where(income: true).to_a
5252
end
5353

54-
def expenditures
55-
@expenditures ||= report.costs.where(income: false).to_a
54+
def expenses
55+
@expenses ||= report.costs.where(income: false).to_a
5656
end
5757

5858
def receipts
@@ -74,8 +74,8 @@ def revenues_attributes=(attrs)
7474
@revenues = build_records(attrs, collection: report.costs, fixed_attributes: {income: true})
7575
end
7676

77-
def expenditures_attributes=(attrs)
78-
@expenditures = build_records(attrs, collection: report.costs,
77+
def expenses_attributes=(attrs)
78+
@expenses = build_records(attrs, collection: report.costs,
7979
fixed_attributes: {income: false})
8080
end
8181

@@ -98,7 +98,7 @@ def assert_participation_states_editable
9898
end
9999

100100
def assert_cost_records_valid
101-
cost_records.flat_map { _1.reject(&:valid?) }.each do |model|
101+
cost_records.flat_map { _1.reject(&:marked_for_destruction?).reject(&:valid?) }.each do |model|
102102
model.errors.full_messages.each { errors.add(:base, _1) }
103103
end
104104
end
@@ -123,7 +123,7 @@ def participation_by_id
123123
end
124124

125125
def cost_records
126-
[@revenues, @expenditures, @receipts].compact
126+
[@revenues, @expenses, @receipts].compact
127127
end
128128

129129
def build_records(attrs, collection:, fixed_attributes: {})

app/views/event/tours/reports/_cost_summary_fields.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
= f.button t(".add_tour_fees"), type: :button, data: {action: "click->sac--prefill-revenues#prefill"}, class: "btn btn-sm btn-outline-primary mb-3"
99
= render "event/tours/reports/cost_table_fields", f: f, entries: entry.revenues, cost_type: :revenues
1010

11-
= field_set_tag(t(".expenditures")) do
12-
= render "event/tours/reports/cost_table_fields", f: f, entries: entry.expenditures, cost_type: :expenditures
11+
= field_set_tag(t(".expenses")) do
12+
= render "event/tours/reports/cost_table_fields", f: f, entries: entry.expenses, cost_type: :expenses
1313

1414
= field_set_tag(t(".receipts")) do
1515
= render "event/tours/reports/receipt_table_fields", f: f, entries: entry.receipts

app/views/event/tours/reports/_cost_table_fields.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
= ff.input_field :description
2121
.col-xl-3
2222
.d-xl-none.muted= Event::Cost.human_attribute_name(:count)
23-
= ff.input_field :count, step: 1, data: {field_multiplication_target: "source", action: "input->field-multiplication#recalculate"}
23+
= ff.input_field :count, step: 1, value: format("%g", ff.object.count), data: {field_multiplication_target: "source", action: "input->field-multiplication#recalculate"}
2424
.col-xl-3
2525
.d-xl-none.muted= Event::Cost.human_attribute_name(:amount)
2626
= ff.input_field :amount, step: 0.05, data: {field_multiplication_target: "source", action: "input->field-multiplication#recalculate"}

config/locales/wagon.de.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ de:
17291729
cost_summary_fields:
17301730
revenues: Einnahmen
17311731
add_tour_fees: Tourengebühren hinzufügen
1732-
expenditures: Ausgaben
1732+
expenses: Ausgaben
17331733
receipts: Belege
17341734
form_tabs:
17351735
status: Status

spec/controllers/event/tours/reports_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@
106106
expect(event_costs(:participation_fee).reload.description).to eq "We made moneyyyyyyy"
107107
end
108108

109-
it "updates expenditures" do
109+
it "updates expenses" do
110110
put :update, params: params.merge(
111111
event_tour_report_form: {
112-
expenditures_attributes: {
112+
expenses_attributes: {
113113
"0" => {
114114
id: event_costs(:transport).id,
115115
description: "Spent it all again sadly...",

spec/features/event/tours/reports_spec.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:event) { events(:section_tour) }
1313
let(:report) { event_reports(:section_tour_report) }
1414
let(:revenue) { event_costs(:participation_fee) }
15-
let(:expenditure) { event_costs(:transport) }
15+
let(:expense) { event_costs(:transport) }
1616
let(:receipt) { event_cost_receipts(:tankstelle) }
1717

1818
def create_participation(price_category:, price:)
@@ -89,7 +89,7 @@ def set_field(name, value)
8989
end
9090

9191
it "updates the section total when a row is removed" do
92-
table_total = find("#expenditures_total")
92+
table_total = find("#expenses_total")
9393

9494
within("fieldset", text: "Einnahmen") do
9595
click_link "Entfernen"
@@ -155,10 +155,32 @@ def set_field(name, value)
155155

156156
expect(page).not_to have_field(name: /\[description\]/, with: "Kosten SAC-Mitglied (extern)")
157157
end
158+
159+
it "does not add duplicate dom rows when clicking add tour prices twice" do
160+
create_participation(price_category: "price_member", price: 10)
161+
162+
click_button "Tourengebühren hinzufügen"
163+
164+
expect(all(".fields", visible: :all).size).to eq 3
165+
166+
click_button "Tourengebühren hinzufügen"
167+
168+
expect(all(".fields", visible: :all).size).to eq 3
169+
end
170+
171+
it "assigns unique field names to rows added in the same click" do
172+
create_participation(price_category: "price_member", price: 10)
173+
create_participation(price_category: "price_special", price: 40)
174+
175+
click_button "Tourengebühren hinzufügen"
176+
177+
field_names = all(".fields[data-new-record] input[name*='[description]']").pluck(:name)
178+
expect(field_names).to match_array(field_names.uniq)
179+
end
158180
end
159181

160-
describe "expenditures" do
161-
it "can add an expenditure" do
182+
describe "expenses" do
183+
it "can add an expense" do
162184
within("fieldset", text: "Ausgaben") do
163185
click_link "Eintrag hinzufügen"
164186
within(".fields[data-new-record]") do
@@ -175,7 +197,7 @@ def set_field(name, value)
175197
end
176198

177199
it "updates the row total and section total when count and amount change" do
178-
table_total = find("#expenditures_total")
200+
table_total = find("#expenses_total")
179201

180202
within("fieldset", text: "Ausgaben") do
181203
click_link "Eintrag hinzufügen"
@@ -209,7 +231,7 @@ def set_field(name, value)
209231
end
210232

211233
it "updates the section total when a row is removed" do
212-
table_total = find("#expenditures_total")
234+
table_total = find("#expenses_total")
213235

214236
within("fieldset", text: "Ausgaben") do
215237
click_link "Entfernen"

spec/models/event/cost_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,17 @@
3838
it "calculates total of amount and count" do
3939
expect(event_cost.total).to eq 300
4040
end
41+
42+
it "is zero when amount is nil" do
43+
event_cost.amount = nil
44+
45+
expect(event_cost.total).to be_zero
46+
end
47+
48+
it "is zero when count is nil" do
49+
event_cost.count = nil
50+
51+
expect(event_cost.total).to be_zero
52+
end
4153
end
4254
end

spec/models/event/tour/report_form_spec.rb

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
participation: Fabricate(:event_participation, participant: people(:mitglied), event: event)).participation
1616
end
1717
let(:revenue) { event_costs(:participation_fee) }
18-
let(:expenditure) { event_costs(:transport) }
18+
let(:expense) { event_costs(:transport) }
1919
let(:receipt) { event_cost_receipts(:tankstelle) }
2020
let(:form) { described_class.new(report) }
2121

@@ -81,21 +81,21 @@
8181
expect { form.save }.to change { report.costs.count }.by(-1)
8282
end
8383

84-
it "persists changes to existing expenditure" do
85-
form.expenditures_attributes = {
84+
it "persists changes to existing expense" do
85+
form.expenses_attributes = {
8686
"0" => {
87-
id: expenditure.id,
87+
id: expense.id,
8888
description: "something else"
8989
}
9090
}
9191

92-
expect { form.save }.to change { expenditure.reload.description }.to("something else")
92+
expect { form.save }.to change { expense.reload.description }.to("something else")
9393
end
9494

95-
it "persists new expenditure" do
96-
form.expenditures_attributes = {
95+
it "persists new expense" do
96+
form.expenses_attributes = {
9797
"0" => {
98-
description: "New expenditure",
98+
description: "New expense",
9999
count: "2",
100100
amount: "75"
101101
}
@@ -104,10 +104,10 @@
104104
expect { form.save }.to change { report.costs.where(income: false).count }.by(1)
105105
end
106106

107-
it "destroys expenditure marked for destruction" do
108-
form.expenditures_attributes = {
107+
it "destroys expense marked for destruction" do
108+
form.expenses_attributes = {
109109
"0" => {
110-
id: expenditure.id,
110+
id: expense.id,
111111
_destroy: "1"
112112
}
113113
}
@@ -162,14 +162,14 @@
162162
describe "#revenues" do
163163
it "returns costs with income true" do
164164
expect(form.revenues).to include(revenue)
165-
expect(form.revenues).not_to include(expenditure)
165+
expect(form.revenues).not_to include(expense)
166166
end
167167
end
168168

169-
describe "#expenditures" do
169+
describe "#expenses" do
170170
it "returns costs with income false" do
171-
expect(form.expenditures).to include(expenditure)
172-
expect(form.expenditures).not_to include(revenue)
171+
expect(form.expenses).to include(expense)
172+
expect(form.expenses).not_to include(revenue)
173173
end
174174
end
175175

@@ -227,17 +227,17 @@
227227
expect(form.revenues.first.errors.full_messages).to match_array ["Bezeichnung muss ausgefüllt werden"]
228228
end
229229

230-
it "is not valid if expenditure is invalid" do
231-
form.expenditures_attributes = {
230+
it "is not valid if expense is invalid" do
231+
form.expenses_attributes = {
232232
"0" => {
233-
id: expenditure.id,
233+
id: expense.id,
234234
description: ""
235235
}
236236
}
237237

238238
expect(form).not_to be_valid
239239
expect(form.errors.full_messages).to match_array ["Bezeichnung muss ausgefüllt werden"]
240-
expect(form.expenditures.first.errors.full_messages).to match_array ["Bezeichnung muss ausgefüllt werden"]
240+
expect(form.expenses.first.errors.full_messages).to match_array ["Bezeichnung muss ausgefüllt werden"]
241241
end
242242

243243
it "is not valid if receipt is invalid" do
@@ -252,6 +252,18 @@
252252
expect(form.errors.full_messages).to match_array ["Beschreibung muss ausgefüllt werden"]
253253
expect(form.receipts.first.errors.full_messages).to match_array ["Beschreibung muss ausgefüllt werden"]
254254
end
255+
256+
it "is valid if invalid revenue is marked for destruction" do
257+
form.revenues_attributes = {
258+
"0" => {
259+
id: revenue.id,
260+
description: "",
261+
_destroy: "1"
262+
}
263+
}
264+
265+
expect(form).to be_valid
266+
end
255267
end
256268

257269
describe "#tour_completed?" do

0 commit comments

Comments
 (0)