diff --git a/app/models/debate.rb b/app/models/debate.rb index e8855086a..b118e8a89 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -21,7 +21,7 @@ class Debate < ActiveRecord::Base before_validation :sanitize_description before_validation :sanitize_tag_list - before_save :calculate_hot_score + before_save :calculate_hot_score, :calculate_confidence_score scope :sort_for_moderation, -> { order(flags_count: :desc, updated_at: :desc) } scope :pending_flag_review, -> { where(ignored_flag_at: nil, hidden_at: nil) } @@ -29,7 +29,7 @@ class Debate < ActiveRecord::Base scope :flagged, -> { where("flags_count > 0") } scope :for_render, -> { includes(:tags) } scope :sort_by_hot_score , -> { order(hot_score: :desc) } - scope :sort_by_score , -> { order(cached_votes_score: :desc) } + scope :sort_by_confidence_score , -> { order(confidence_score: :desc) } scope :sort_by_created_at, -> { order(created_at: :desc) } scope :sort_by_most_commented, -> { order(comments_count: :desc) } scope :sort_by_random, -> { order("RANDOM()") } @@ -130,6 +130,11 @@ class Debate < ActiveRecord::Base self.hot_score = (age_in_units**3 + weighted_score * 1000).round end + def calculate_confidence_score + return unless cached_votes_total > 0 + self.confidence_score = cached_votes_score * cached_votes_up / cached_votes_total + end + def self.search(terms) terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none end diff --git a/db/migrate/20150908102936_add_confidence_score_to_debates.rb b/db/migrate/20150908102936_add_confidence_score_to_debates.rb new file mode 100644 index 000000000..b0494a839 --- /dev/null +++ b/db/migrate/20150908102936_add_confidence_score_to_debates.rb @@ -0,0 +1,6 @@ +class AddConfidenceScoreToDebates < ActiveRecord::Migration + def change + add_column :debates, :confidence_score, :integer, default: 0 + add_index :debates, :confidence_score + end +end diff --git a/db/schema.rb b/db/schema.rb index 8e4dca776..f1b4dfe76 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150907064631) do +ActiveRecord::Schema.define(version: 20150908102936) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -102,12 +102,14 @@ ActiveRecord::Schema.define(version: 20150907064631) do t.integer "cached_anonymous_votes_total", default: 0 t.integer "cached_votes_score", default: 0 t.integer "hot_score", limit: 8, default: 0 + t.integer "confidence_score", default: 0 end add_index "debates", ["cached_votes_down"], name: "index_debates_on_cached_votes_down", using: :btree add_index "debates", ["cached_votes_score"], name: "index_debates_on_cached_votes_score", using: :btree add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree + add_index "debates", ["confidence_score"], name: "index_debates_on_confidence_score", using: :btree add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree diff --git a/spec/factories.rb b/spec/factories.rb index deaaee98d..f779f2e32 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -78,6 +78,10 @@ FactoryGirl.define do before(:save) { |d| d.calculate_hot_score } end + trait :with_confidence_score do + before(:save) { |d| d.calculate_confidence_score } + end + trait :conflictive do after :create do |debate| Flag.flag(FactoryGirl.create(:user), debate) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 5ee0d7243..a2262f909 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -236,6 +236,26 @@ describe Debate do expect(previous).to be < debate.hot_score end end + end + + describe "#confidence_score" do + + it "takes into account percentage of total votes and total_positive and total negative votes" do + debate = create(:debate, :with_confidence_score, cached_votes_up: 100, cached_votes_score: 100, cached_votes_total: 100) + expect(debate.confidence_score).to eq(100) + + debate = create(:debate, :with_confidence_score, cached_votes_up: 0, cached_votes_score: -100, cached_votes_total: 100) + expect(debate.confidence_score).to eq(0) + + debate = create(:debate, :with_confidence_score, cached_votes_up: 50, cached_votes_score: 50, cached_votes_total: 100) + expect(debate.confidence_score).to eq(25) + + debate = create(:debate, :with_confidence_score, cached_votes_up: 500, cached_votes_score: 500, cached_votes_total: 1000) + expect(debate.confidence_score).to eq(250) + + debate = create(:debate, :with_confidence_score, cached_votes_up: 10, cached_votes_score: -80, cached_votes_total: 100) + expect(debate.confidence_score).to eq(-8) + end end