diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 54eadb947..1d56ae4d3 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,10 +1,5 @@ module ApplicationHelper - def percentage(vote, debate) - return "0%" if debate.total_votes == 0 - debate.send(vote).percent_of(debate.total_votes).to_s + "%" - end - def home_page? return false if user_signed_in? # Using path because fullpath yields false negatives since it contains diff --git a/app/helpers/votes_helper.rb b/app/helpers/votes_helper.rb index dafb108c7..dc1df0396 100644 --- a/app/helpers/votes_helper.rb +++ b/app/helpers/votes_helper.rb @@ -1,5 +1,18 @@ module VotesHelper + def debate_percentage_of_likes(debate) + debate.likes.percent_of(debate.total_votes) + end + + def votes_percentage(vote, debate) + return "0%" if debate.total_votes == 0 + if vote == 'likes' + debate_percentage_of_likes(debate).to_s + "%" + elsif vote == 'dislikes' + (100 - debate_percentage_of_likes(debate)).to_s + "%" + end + end + def css_classes_for_vote(votes, votable) case votes[votable.id] when true diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index 89a03752f..970bcc8a5 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -4,7 +4,7 @@ <%= link_to vote_debate_path(debate, value: 'yes'), class: "like #{voted_classes[:in_favor]}", title: t('votes.agree'), method: "post", remote: true do %> - <%= percentage('likes', debate) %> + <%= votes_percentage('likes', debate) %> <% end %> @@ -13,7 +13,7 @@
<%= link_to vote_debate_path(debate, value: 'no'), class: "unlike #{voted_classes[:against]}", title: t('votes.disagree'), method: "post", remote: true do %> - <%= percentage('dislikes', debate) %> + <%= votes_percentage('dislikes', debate) %> <% end %>
diff --git a/spec/helpers/votes_helper_spec.rb b/spec/helpers/votes_helper_spec.rb index 96be3b3fe..d695b881a 100644 --- a/spec/helpers/votes_helper_spec.rb +++ b/spec/helpers/votes_helper_spec.rb @@ -18,4 +18,15 @@ describe VotesHelper do end end + describe "#votes_percentage" do + it "should always sum 100%" do + debate = create(:debate) + create_list(:vote, 8, votable: debate, vote_flag: true) + create_list(:vote, 3, votable: debate, vote_flag: false) + + expect(votes_percentage('likes', debate)).to eq("72%") + expect(votes_percentage('dislikes', debate)).to eq("28%") + end + end + end \ No newline at end of file