From 9dee1bc8b234cbd67892d9883f411c578ed2ca50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sun, 13 Sep 2015 19:00:11 +0200 Subject: [PATCH] adds helper to show the right progress bar --- app/helpers/proposals_helper.rb | 11 +++++++++++ app/views/proposals/_votes.html.erb | 2 +- spec/helpers/proposals_helper_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 app/helpers/proposals_helper.rb create mode 100644 spec/helpers/proposals_helper_spec.rb diff --git a/app/helpers/proposals_helper.rb b/app/helpers/proposals_helper.rb new file mode 100644 index 000000000..85edd6e07 --- /dev/null +++ b/app/helpers/proposals_helper.rb @@ -0,0 +1,11 @@ +module ProposalsHelper + + def progress_bar_percentage(proposal) + case proposal.cached_votes_up + when 0 then 0 + when 1..Proposal.votes_needed_for_success then (proposal.cached_votes_up.to_f * 100 / Proposal.votes_needed_for_success).floor + else 100 + end + end + +end \ No newline at end of file diff --git a/app/views/proposals/_votes.html.erb b/app/views/proposals/_votes.html.erb index 3405f0f35..332c2c058 100644 --- a/app/views/proposals/_votes.html.erb +++ b/app/views/proposals/_votes.html.erb @@ -2,7 +2,7 @@
- +
diff --git a/spec/helpers/proposals_helper_spec.rb b/spec/helpers/proposals_helper_spec.rb new file mode 100644 index 000000000..595c21fbb --- /dev/null +++ b/spec/helpers/proposals_helper_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe ProposalsHelper do + + describe "#progress_bar_percentage" do + + it "should be 0 if no votes" do + proposal = create(:proposal) + expect(progress_bar_percentage(proposal)).to eq 0 + 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(progress_bar_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(progress_bar_percentage(proposal)).to eq 100 + end + end + +end \ No newline at end of file