implements percentage in proposal supports

This commit is contained in:
Juanjo Bazán
2015-09-14 19:06:07 +02:00
parent 4a8b86642a
commit a472b4acc8
3 changed files with 33 additions and 2 deletions

View File

@@ -8,4 +8,14 @@ module ProposalsHelper
end end
end end
def supports_percentage(proposal)
percentage = (proposal.cached_votes_up.to_f * 100 / Proposal.votes_needed_for_success)
case percentage
when 0 then "0%"
when 0..(0.1) then "0.1%"
when (0.1)..100 then number_to_percentage(percentage, strip_insignificant_zeros: true, precision: 1)
else "100%"
end
end
end end

View File

@@ -8,7 +8,7 @@
<span class="total-supports"> <span class="total-supports">
<%= t("proposals.proposal.supports", count: proposal.total_votes) %>&nbsp; <%= t("proposals.proposal.supports", count: proposal.total_votes) %>&nbsp;
<!-- percentage of supports --> <!-- percentage of supports -->
(0.1%) (<%= supports_percentage(proposal) %>)
<!-- /. percentage of supports --> <!-- /. percentage of supports -->
<span> <span>
<abbr title="<%= t("proposals.proposal.census_percent") %>"> <abbr title="<%= t("proposals.proposal.census_percent") %>">

View File

@@ -3,7 +3,6 @@ require 'rails_helper'
describe ProposalsHelper do describe ProposalsHelper do
describe "#progress_bar_percentage" do describe "#progress_bar_percentage" do
it "should be 0 if no votes" do it "should be 0 if no votes" do
proposal = create(:proposal) proposal = create(:proposal)
expect(progress_bar_percentage(proposal)).to eq 0 expect(progress_bar_percentage(proposal)).to eq 0
@@ -20,4 +19,26 @@ describe ProposalsHelper do
end end
end end
describe "#supports_percentage" do
it "should be 0 if no votes" do
proposal = create(:proposal)
expect(supports_percentage(proposal)).to eq "0%"
end
it "should be a between 0.1 from 1 to 0.1% of needed votes" do
proposal = create(:proposal, cached_votes_up: 1)
expect(supports_percentage(proposal)).to eq "0.1%"
end
it "should be a between 1 and 100 if there are votes but less than needed" do
proposal = create(:proposal, cached_votes_up: Proposal.votes_needed_for_success/2)
expect(supports_percentage(proposal)).to eq "50%"
end
it "should be 100 if there are more votes than needed" do
proposal = create(:proposal, cached_votes_up: Proposal.votes_needed_for_success*2)
expect(supports_percentage(proposal)).to eq "100%"
end
end
end end