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>

View File

@@ -127,6 +127,7 @@ ignore_unused:
- "budgets.phase.*"
- "budgets.investments.index.orders.*"
- "budgets.index.section_header.*"
- "budgets.investments.index.sidebar.voted_info.*"
- "activerecord.*"
- "activemodel.*"
- "attributes.*"

View File

@@ -3,8 +3,12 @@ en:
ballots:
show:
title: Your ballot
amount_spent: "Amount spent <span>%{amount}</span>"
remaining: "You still have <span>%{amount}</span> to invest."
amount_available:
knapsack: "You still have <span>%{amount}</span> to invest."
amount_spent:
knapsack: "Amount spent <span>%{amount}</span>"
amount_limit:
knapsack: "%{amount}"
no_balloted_group_yet: "You have not voted on this group yet, go vote!"
remove: Remove vote
voted:
@@ -84,10 +88,12 @@ en:
other: " containing the term <strong>'%{search_term}'</strong>"
sidebar:
my_ballot: My ballot
voted:
one: "<strong>You voted one proposal with a cost of %{amount_spent}</strong>"
other: "<strong>You voted %{count} proposals with a cost of %{amount_spent}</strong>"
change_vote_info: "You can %{link} at any time until the close of this phase. No need to spend all the money available."
voted_info:
knapsack:
one: "<strong>You voted one proposal with a cost of %{amount_spent}</strong>"
other: "<strong>You voted %{count} proposals with a cost of %{amount_spent}</strong>"
change_vote_info:
knapsack: "You can %{link} at any time until the close of this phase. No need to spend all the money available."
change_vote_link: "change your vote"
different_heading_assigned: "You have active votes in another heading: %{heading_link}"
change_ballot: "If your change your mind you can remove your votes in %{check_ballot} and start again."

View File

@@ -3,8 +3,12 @@ es:
ballots:
show:
title: Mis votos
amount_spent: "Coste total <span>%{amount}</span>"
remaining: "Te quedan <span>%{amount}</span> para invertir"
amount_available:
knapsack: "Te quedan <span>%{amount}</span> para invertir"
amount_spent:
knapsack: "Coste total <span>%{amount}</span>"
amount_limit:
knapsack: "%{amount}"
no_balloted_group_yet: "Todavía no has votado proyectos de este grupo, ¡vota!"
remove: Quitar voto
voted:
@@ -84,10 +88,12 @@ es:
other: " que contienen <strong>'%{search_term}'</strong>"
sidebar:
my_ballot: Mis votos
voted:
one: "<strong>Has votado un proyecto por un valor de %{amount_spent}</strong>"
other: "<strong>Has votado %{count} proyectos por un valor de %{amount_spent}</strong>"
change_vote_info: "Puedes %{link} en cualquier momento hasta el cierre de esta fase. No hace falta que gastes todo el dinero disponible."
voted_info:
knapsack:
one: "<strong>Has votado un proyecto por un valor de %{amount_spent}</strong>"
other: "<strong>Has votado %{count} proyectos por un valor de %{amount_spent}</strong>"
change_vote_info:
knapsack: "Puedes %{link} en cualquier momento hasta el cierre de esta fase. No hace falta que gastes todo el dinero disponible."
change_vote_link: "cambiar tus votos"
different_heading_assigned: "Ya apoyaste proyectos de otra sección del presupuesto: %{heading_link}"
change_ballot: "Si cambias de opinión puedes borrar tus votos en %{check_ballot} y volver a empezar."