adds confidence_score to debates
This commit is contained in:
@@ -21,7 +21,7 @@ class Debate < ActiveRecord::Base
|
|||||||
before_validation :sanitize_description
|
before_validation :sanitize_description
|
||||||
before_validation :sanitize_tag_list
|
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 :sort_for_moderation, -> { order(flags_count: :desc, updated_at: :desc) }
|
||||||
scope :pending_flag_review, -> { where(ignored_flag_at: nil, hidden_at: nil) }
|
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 :flagged, -> { where("flags_count > 0") }
|
||||||
scope :for_render, -> { includes(:tags) }
|
scope :for_render, -> { includes(:tags) }
|
||||||
scope :sort_by_hot_score , -> { order(hot_score: :desc) }
|
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_created_at, -> { order(created_at: :desc) }
|
||||||
scope :sort_by_most_commented, -> { order(comments_count: :desc) }
|
scope :sort_by_most_commented, -> { order(comments_count: :desc) }
|
||||||
scope :sort_by_random, -> { order("RANDOM()") }
|
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
|
self.hot_score = (age_in_units**3 + weighted_score * 1000).round
|
||||||
end
|
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)
|
def self.search(terms)
|
||||||
terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none
|
terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -102,12 +102,14 @@ ActiveRecord::Schema.define(version: 20150907064631) do
|
|||||||
t.integer "cached_anonymous_votes_total", default: 0
|
t.integer "cached_anonymous_votes_total", default: 0
|
||||||
t.integer "cached_votes_score", default: 0
|
t.integer "cached_votes_score", default: 0
|
||||||
t.integer "hot_score", limit: 8, default: 0
|
t.integer "hot_score", limit: 8, default: 0
|
||||||
|
t.integer "confidence_score", default: 0
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "debates", ["cached_votes_down"], name: "index_debates_on_cached_votes_down", using: :btree
|
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_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_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", ["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", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree
|
||||||
add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree
|
add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,10 @@ FactoryGirl.define do
|
|||||||
before(:save) { |d| d.calculate_hot_score }
|
before(:save) { |d| d.calculate_hot_score }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trait :with_confidence_score do
|
||||||
|
before(:save) { |d| d.calculate_confidence_score }
|
||||||
|
end
|
||||||
|
|
||||||
trait :conflictive do
|
trait :conflictive do
|
||||||
after :create do |debate|
|
after :create do |debate|
|
||||||
Flag.flag(FactoryGirl.create(:user), debate)
|
Flag.flag(FactoryGirl.create(:user), debate)
|
||||||
|
|||||||
@@ -236,6 +236,26 @@ describe Debate do
|
|||||||
expect(previous).to be < debate.hot_score
|
expect(previous).to be < debate.hot_score
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user