Return the persisted line in add_investment

This method was returning a boolean value and caused a
`Naming/PredicateMethod` when upgrading rubocop.

So, instead, we're returning the created line when it was successfully
created, and `nil` when it wasn't.

Having said that, I'm not sure why we added the `.persisted?` back in
commit 3eb22ab7b since as far as I can tell we don't use the return
value for anything. The test added in commit da43e9e2e for this change
passes if we simply return `lines.create(investment: investment)`.

For now I'm leaving the `persisted?` check just in case, but removing it
might be fine.
This commit is contained in:
Javi Martín
2025-10-31 15:31:20 +01:00
parent 15f7632f3d
commit 413d0ed9be
2 changed files with 13 additions and 11 deletions

View File

@@ -10,7 +10,9 @@ class Budget
has_many :headings, -> { distinct }, through: :groups has_many :headings, -> { distinct }, through: :groups
def add_investment(investment) def add_investment(investment)
lines.create(investment: investment).persisted? line = lines.create(investment: investment)
line if line.persisted?
end end
def total_amount_spent def total_amount_spent

View File

@@ -53,43 +53,43 @@ describe Poll::Ballot do
describe "Money" do describe "Money" do
it "is not valid if insufficient funds" do it "is not valid if insufficient funds" do
investment.update!(price: heading.price + 1) investment.update!(price: heading.price + 1)
expect(poll_ballot.add_investment(investment.id)).to be false expect(poll_ballot.add_investment(investment.id)).to be nil
end end
it "is valid if sufficient funds" do it "is valid if sufficient funds" do
investment.update!(price: heading.price - 1) investment.update!(price: heading.price - 1)
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
end end
end end
describe "Heading" do describe "Heading" do
it "is not valid if investment heading is not valid" do it "is not valid if investment heading is not valid" do
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
other_heading = create(:budget_heading, group: group, price: 10000000) other_heading = create(:budget_heading, group: group, price: 10000000)
other_investment = create(:budget_investment, :selected, price: 1000000, heading: other_heading) other_investment = create(:budget_investment, :selected, price: 1000000, heading: other_heading)
expect(poll_ballot.add_investment(other_investment.id)).to be false expect(poll_ballot.add_investment(other_investment.id)).to be nil
end end
it "is valid if investment heading is valid" do it "is valid if investment heading is valid" do
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
other_investment = create(:budget_investment, :selected, price: 1000000, heading: heading) other_investment = create(:budget_investment, :selected, price: 1000000, heading: heading)
expect(poll_ballot.add_investment(other_investment.id)).to be true expect(poll_ballot.add_investment(other_investment.id)).to be_truthy
end end
end end
describe "Selectibility" do describe "Selectibility" do
it "is not valid if investment is unselected" do it "is not valid if investment is unselected" do
investment.update!(selected: false) investment.update!(selected: false)
expect(poll_ballot.add_investment(investment.id)).to be false expect(poll_ballot.add_investment(investment.id)).to be nil
end end
it "is valid if investment is selected" do it "is valid if investment is selected" do
investment.update!(selected: true, price: 20000) investment.update!(selected: true, price: 20000)
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
end end
end end
@@ -101,7 +101,7 @@ describe Poll::Ballot do
end end
it "is valid if investment belongs to the poll's budget" do it "is valid if investment belongs to the poll's budget" do
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
end end
end end
@@ -112,7 +112,7 @@ describe Poll::Ballot do
end end
it "is valid if does not already exist" do it "is valid if does not already exist" do
expect(poll_ballot.add_investment(investment.id)).to be true expect(poll_ballot.add_investment(investment.id)).to be_truthy
end end
end end
end end