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

View File

@@ -26,14 +26,12 @@
<h3>
<%= group.name %> - <%= heading.name %>
</h3>
<%= link_to sanitize(t("budgets.ballots.show.remaining",
amount: @ballot.formatted_amount_available(heading))),
<%= link_to sanitize(@ballot.amount_available_info(heading)),
budget_group_path(@budget, group) %>
</div>
<% if @ballot.has_lines_in_group?(group) %>
<h4 class="amount-spent text-right">
<%= sanitize(t("budgets.ballots.show.amount_spent",
amount: @ballot.formatted_amount_spent(heading))) %>
<%= sanitize(@ballot.amount_spent_info(heading)) %>
</h4>
<% else %>
<p>

View File

@@ -1,5 +1,5 @@
<span class="total-amount">
<%= @budget.formatted_heading_price(@heading) %>
<%= sanitize(@ballot.amount_limit_info(@heading)) %>
</span>
<div class="progress" role="progressbar" tabindex="0"

View File

@@ -14,7 +14,7 @@
<% if @heading && can?(:show, @ballot) %>
<p class="callout">
<%= sanitize(t("budgets.investments.index.sidebar.change_vote_info",
<%= sanitize(@ballot.change_vote_info(
link: link_to(t("budgets.investments.index.sidebar.change_vote_link"),
budget_ballot_path(@budget)))) %>
</p>
@@ -39,11 +39,7 @@
<% if @ballot.investments.by_heading(@heading.id).count > 0 %>
<p>
<em>
<%= sanitize(t("budgets.investments.index.sidebar.voted",
count: @ballot.investments.by_heading(@heading.id).count,
amount_spent: @ballot.formatted_amount_spent(@heading))) %>
</em>
<em><%= sanitize(@ballot.voted_info(@heading)) %></em>
</p>
<% elsif @assigned_heading.present? %>
<p>