adds confidence_score capacity to the comment model
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user