From 2975c6a38d765f2963361e0a0cb55d315cf83d7e Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 4 Sep 2015 14:04:18 +0200 Subject: [PATCH] Makes votes increase/decrease hot score MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Debates already call update_attibutes in debates (to update cached counters), so there is no need to do an “after_voted”: the comment’s before_save call takes care of it. --- spec/models/debate_spec.rb | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 52d8c0740..93403c591 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -184,35 +184,55 @@ describe Debate do describe '#hot_score' do let(:now) { Time.now } - it "gets bigger for newer debates" do + it "increases for newer debates" do old = create(:debate, :with_hot_score, created_at: now - 1.day) new = create(:debate, :with_hot_score, created_at: now) expect(new.hot_score).to be > old.hot_score end - it "gets bigger for debates with more comments" do + it "increases for debates with more comments" do more_comments = create(:debate, :with_hot_score, created_at: now, comments_count: 10) less_comments = create(:debate, :with_hot_score, created_at: now, comments_count: 1) expect(more_comments.hot_score).to be > less_comments.hot_score end - it "gets bigger for debates with more positive votes" do + it "increases for debates with more positive votes" do more_likes = create(:debate, :with_hot_score, created_at: now, cached_votes_total: 10, cached_votes_up: 5) less_likes = create(:debate, :with_hot_score, created_at: now, cached_votes_total: 10, cached_votes_up: 1) expect(more_likes.hot_score).to be > less_likes.hot_score end - it "gets bigger for debates with more confidence" do + it "increases for debates with more confidence" do more_confidence = create(:debate, :with_hot_score, created_at: now, cached_votes_total: 1000, cached_votes_up: 700) less_confidence = create(:debate, :with_hot_score, created_at: now, cached_votes_total: 10, cached_votes_up: 9) expect(more_confidence.hot_score).to be > less_confidence.hot_score end - it "decays older debates, even if they have more votes" do + it "decays in older debates, even if they have more votes" do older_more_voted = create(:debate, :with_hot_score, created_at: now - 2.days, cached_votes_total: 1000, cached_votes_up: 900) new_less_voted = create(:debate, :with_hot_score, created_at: now, cached_votes_total: 10, cached_votes_up: 9) expect(new_less_voted.hot_score).to be > older_more_voted.hot_score end + + describe 'actions which affect it' do + let(:debate) { create(:debate, :with_hot_score) } + + it "increases with likes" do + previous = debate.hot_score + 5.times { debate.register_vote(create(:user), true) } + expect(previous).to be < debate.hot_score + end + + it "decreases with dislikes" do + debate.register_vote(create(:user), true) + previous = debate.hot_score + 3.times { debate.register_vote(create(:user), false) } + expect(previous).to be > debate.hot_score + end + end + end + + end