From 160964fcdc551543c6196c53357cfd53ceae595e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 13 Jul 2020 14:18:05 +0200 Subject: [PATCH] Make method to check a line can be added generic In the Knapsack voting style, we can't add an investment if its cost is greater than the money we've got left, but in other voting styles money might not be the issue. So we're introducing the term "resources" and adapting the code accordingly. --- app/models/budget/ballot.rb | 5 +++-- app/models/budget/ballot/line.rb | 9 ++++++--- app/models/budget/investment.rb | 3 ++- app/models/budget/voting_styles/knapsack.rb | 10 +++++++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index 074bf6a8f..399982d8e 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -64,8 +64,9 @@ class Budget @voting_style ||= voting_style_class.new(self) end delegate :amount_available, :amount_available_info, :amount_spent, :amount_spent_info, - :amount_limit_info, :change_vote_info, :enough_money?, :formatted_amount_available, - :formatted_amount_limit, :formatted_amount_spent, :voted_info, + :amount_limit_info, :change_vote_info, :enough_resources?, :formatted_amount_available, + :formatted_amount_limit, :formatted_amount_spent, :not_enough_resources_error, + :reason_for_not_being_ballotable, :voted_info, to: :voting_style private diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 9d9805781..58078de98 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -10,7 +10,7 @@ class Budget validates :ballot_id, :investment_id, :heading_id, :group_id, :budget_id, presence: true validate :check_selected - validate :check_sufficient_funds + validate :check_enough_resources validate :check_valid_heading scope :by_investment, ->(investment_id) { where(investment_id: investment_id) } @@ -18,9 +18,12 @@ class Budget before_validation :set_denormalized_ids after_save :store_user_heading - def check_sufficient_funds + def check_enough_resources ballot.lock! - errors.add(:money, "insufficient funds") unless ballot.enough_money?(investment) + + unless ballot.enough_resources?(investment) + errors.add(:resources, ballot.not_enough_resources_error) + end end def check_valid_heading diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index 63f03441a..4adf23556 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -268,8 +268,9 @@ class Budget return :not_selected unless selected? return :no_ballots_allowed unless budget.balloting? return :different_heading_assigned unless ballot.valid_heading?(heading) - return :not_enough_money unless ballot.enough_money?(self) return :casted_offline if ballot.casted_offline? + + ballot.reason_for_not_being_ballotable(self) end def permission_problem(user) diff --git a/app/models/budget/voting_styles/knapsack.rb b/app/models/budget/voting_styles/knapsack.rb index 815a19102..4f2c72a19 100644 --- a/app/models/budget/voting_styles/knapsack.rb +++ b/app/models/budget/voting_styles/knapsack.rb @@ -1,8 +1,16 @@ class Budget::VotingStyles::Knapsack < Budget::VotingStyles::Base - def enough_money?(investment) + def enough_resources?(investment) investment.price.to_i <= amount_available(investment.heading) end + def reason_for_not_being_ballotable(investment) + :not_enough_money unless enough_resources?(investment) + end + + def not_enough_resources_error + "insufficient funds" + end + def amount_available(heading) amount_limit(heading) - amount_spent(heading) end