From 4e4804e180b549be433fda53c7ea450b926d64aa Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 8 Feb 2019 18:25:21 +0100 Subject: [PATCH] Replace total votes to cached_votes_score on debates This show votes_score as result of votes_up minus votes_down --- app/models/debate.rb | 4 ++++ app/views/debates/_votes.html.erb | 2 +- spec/features/debates_spec.rb | 37 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/debate.rb b/app/models/debate.rb index a73a1ab6c..2aa1bdce4 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -88,6 +88,10 @@ class Debate < ActiveRecord::Base cached_votes_total end + def votes_score + cached_votes_score + end + def total_anonymous_votes cached_anonymous_votes_total end diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index 9430d4a22..c252aa9fe 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -40,7 +40,7 @@ - <%= t("debates.debate.votes", count: debate.total_votes) %> + <%= t("debates.debate.votes", count: debate.votes_score) %> <% if user_signed_in? && current_user.organization? %> diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 2f9d787fe..1cd490276 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -128,6 +128,43 @@ feature 'Debates' do end end + scenario "Show votes score on index and show" do + debate_positive = create(:debate, title: "Debate positive") + debate_zero = create(:debate, title: "Debate zero") + debate_negative = create(:debate, title: "Debate negative") + + 10.times { create(:vote, votable: debate_positive, vote_flag: true) } + 3.times { create(:vote, votable: debate_positive, vote_flag: false) } + + 5.times { create(:vote, votable: debate_zero, vote_flag: true) } + 5.times { create(:vote, votable: debate_zero, vote_flag: false) } + + 6.times { create(:vote, votable: debate_negative, vote_flag: false) } + + visit debates_path + + within "#debate_#{debate_positive.id}" do + expect(page).to have_content("7 votes") + end + + within "#debate_#{debate_zero.id}" do + expect(page).to have_content("No votes") + end + + within "#debate_#{debate_negative.id}" do + expect(page).to have_content("-6 votes") + end + + visit debate_path(debate_positive) + expect(page).to have_content("7 votes") + + visit debate_path(debate_zero) + expect(page).to have_content("No votes") + + visit debate_path(debate_negative) + expect(page).to have_content("-6 votes") + end + scenario 'Create' do author = create(:user) login_as(author)