Remove duplication calculating insufficient funds

We were using the same logic twice.

I've moved the logic to the Ballot model, which to me is a more natural
place to calculate whether there's enough money left than the Investment
model. After all, the remaining money is in the ballot, and not in the
investment.
This commit is contained in:
Javi Martín
2020-07-12 23:39:01 +02:00
parent a32c0f8154
commit a9900e3f27
3 changed files with 6 additions and 7 deletions

View File

@@ -33,6 +33,10 @@ class Budget
budget.formatted_amount(amount_available(heading)) budget.formatted_amount(amount_available(heading))
end end
def enough_money?(investment)
investment.price.to_i <= amount_available(investment.heading)
end
def has_lines_in_group?(group) def has_lines_in_group?(group)
groups.include?(group) groups.include?(group)
end end

View File

@@ -20,7 +20,7 @@ class Budget
def check_sufficient_funds def check_sufficient_funds
ballot.lock! ballot.lock!
errors.add(:money, "insufficient funds") if ballot.amount_available(investment.heading) < investment.price.to_i errors.add(:money, "insufficient funds") unless ballot.enough_money?(investment)
end end
def check_valid_heading def check_valid_heading

View File

@@ -268,7 +268,7 @@ class Budget
return :not_selected unless selected? return :not_selected unless selected?
return :no_ballots_allowed unless budget.balloting? return :no_ballots_allowed unless budget.balloting?
return :different_heading_assigned unless ballot.valid_heading?(heading) return :different_heading_assigned unless ballot.valid_heading?(heading)
return :not_enough_money unless enough_money?(ballot) return :not_enough_money unless ballot.enough_money?(self)
return :casted_offline if ballot.casted_offline? return :casted_offline if ballot.casted_offline?
end end
@@ -301,11 +301,6 @@ class Budget
user.headings_voted_within_group(group).where(id: heading.id).exists? user.headings_voted_within_group(group).where(id: heading.id).exists?
end end
def enough_money?(ballot)
available_money = ballot.amount_available(heading)
price.to_i <= available_money
end
def register_selection(user) def register_selection(user)
vote_by(voter: user, vote: "yes") if selectable_by?(user) vote_by(voter: user, vote: "yes") if selectable_by?(user)
end end