From f5d9ea4357f1d4f433927c448c714f50e8701a17 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 28 Oct 2015 16:25:35 +0100 Subject: [PATCH] adds confidence_score capacity to the comment model --- app/models/comment.rb | 8 ++++++++ spec/factories.rb | 4 ++++ spec/models/comment_spec.rb | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/app/models/comment.rb b/app/models/comment.rb index e9bb59775..6fdf4d294 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,10 +17,13 @@ class Comment < ActiveRecord::Base belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :user, -> { with_hidden } + before_save :calculate_confidence_score + scope :recent, -> { order(id: :desc) } scope :for_render, -> { with_hidden.includes(user: :organization) } scope :with_visible_author, -> { joins(:user).where("users.hidden_at IS NULL") } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } + scope :sort_by_confidence_score , -> { order(confidence_score: :desc) } scope :sort_by_created_at, -> { order(created_at: :desc) } after_create :call_after_commented @@ -88,6 +91,11 @@ class Comment < ActiveRecord::Base 1000 end + def calculate_confidence_score + self.confidence_score = ScoreCalculator.confidence_score(cached_votes_total, + cached_votes_up) + end + private def validate_body_length diff --git a/spec/factories.rb b/spec/factories.rb index eff656edf..ddbb1d072 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -216,6 +216,10 @@ FactoryGirl.define do Flag.flag(FactoryGirl.create(:user), debate) end end + + trait :with_confidence_score do + before(:save) { |d| d.calculate_confidence_score } + end end factory :administrator do diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 6d37eb71b..636bf21a4 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -39,6 +39,44 @@ describe Comment do end end + describe "#confidence_score" do + + it "takes into account percentage of total votes and total_positive and total negative votes" do + comment = create(:comment, :with_confidence_score, cached_votes_up: 100, cached_votes_total: 100) + expect(comment.confidence_score).to eq(10000) + + comment = create(:comment, :with_confidence_score, cached_votes_up: 0, cached_votes_total: 100) + expect(comment.confidence_score).to eq(0) + + comment = create(:comment, :with_confidence_score, cached_votes_up: 75, cached_votes_total: 100) + expect(comment.confidence_score).to eq(3750) + + comment = create(:comment, :with_confidence_score, cached_votes_up: 750, cached_votes_total: 1000) + expect(comment.confidence_score).to eq(37500) + + comment = create(:comment, :with_confidence_score, cached_votes_up: 10, cached_votes_total: 100) + expect(comment.confidence_score).to eq(-800) + end + + describe 'actions which affect it' do + let(:comment) { create(:comment, :with_confidence_score) } + + it "increases with like" do + previous = comment.confidence_score + 5.times { comment.vote_by(voter: create(:user), vote: true) } + expect(previous).to be < comment.confidence_score + end + + it "decreases with dislikes" do + comment.vote_by(voter: create(:user), vote: true) + previous = comment.confidence_score + 3.times { comment.vote_by(voter: create(:user), vote: false) } + expect(previous).to be > comment.confidence_score + end + end + + end + describe "cache" do let(:comment) { create(:comment) }