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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user