Extract methods showing voting style information

The idea is that different voting styles will display different
information messages.
This commit is contained in:
Javi Martín
2020-07-13 02:11:24 +02:00
parent ad6d830c1f
commit 5f726df8be
9 changed files with 91 additions and 31 deletions

View File

@@ -17,10 +17,6 @@ class Budget
investments.sum(:price).to_i
end
def formatted_amount_spent(heading)
budget.formatted_amount(amount_spent(heading))
end
def has_lines_in_group?(group)
groups.include?(group)
end
@@ -67,7 +63,9 @@ class Budget
def voting_style
@voting_style ||= voting_style_class.new(self)
end
delegate :amount_available, :amount_spent, :enough_money?, :formatted_amount_available,
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,
to: :voting_style
private

View File

@@ -4,4 +4,43 @@ class Budget::VotingStyles::Base
def initialize(ballot)
@ballot = ballot
end
def name
self.class.name.split("::").last.underscore
end
def change_vote_info(link:)
I18n.t("budgets.investments.index.sidebar.change_vote_info.#{name}", link: link)
end
def voted_info(heading)
I18n.t("budgets.investments.index.sidebar.voted_info.#{name}",
count: investments(heading).count,
amount_spent: ballot.budget.formatted_amount(investments_price(heading)))
end
def amount_available_info(heading)
I18n.t("budgets.ballots.show.amount_available.#{name}",
amount: formatted_amount_available(heading))
end
def amount_spent_info(heading)
I18n.t("budgets.ballots.show.amount_spent.#{name}",
amount: formatted_amount_spent(heading))
end
def amount_limit_info(heading)
I18n.t("budgets.ballots.show.amount_limit.#{name}",
amount: formatted_amount_limit(heading))
end
private
def investments(heading)
ballot.investments.by_heading(heading.id)
end
def investments_price(heading)
investments(heading).sum(:price).to_i
end
end

View File

@@ -4,14 +4,30 @@ class Budget::VotingStyles::Knapsack < Budget::VotingStyles::Base
end
def amount_available(heading)
ballot.budget.heading_price(heading) - amount_spent(heading)
amount_limit(heading) - amount_spent(heading)
end
def amount_spent(heading)
ballot.investments.by_heading(heading.id).sum(:price).to_i
investments_price(heading)
end
def amount_limit(heading)
ballot.budget.heading_price(heading)
end
def formatted_amount_available(heading)
ballot.budget.formatted_amount(amount_available(heading))
format(amount_available(heading))
end
def formatted_amount_spent(heading)
format(amount_spent(heading))
end
def formatted_amount_limit(heading)
format(amount_limit(heading))
end
def format(amount)
ballot.budget.formatted_amount(amount)
end
end