Create Related Content Scores

This commit is contained in:
Bertocq
2017-12-20 01:36:35 +01:00
parent 7cb20bb084
commit 065b0ed3c8
7 changed files with 41 additions and 5 deletions

View File

@@ -1,10 +1,11 @@
class RelatedContent < ActiveRecord::Base class RelatedContent < ActiveRecord::Base
RELATED_CONTENTS_REPORT_THRESHOLD = Setting['related_contents_report_threshold'].to_f RELATED_CONTENT_SCORE_THRESHOLD = Setting['related_content_score_threshold'].to_f
RELATIONABLE_MODELS = %w{proposals debates}.freeze RELATIONABLE_MODELS = %w{proposals debates}.freeze
belongs_to :parent_relationable, polymorphic: true, touch: true belongs_to :parent_relationable, polymorphic: true, touch: true
belongs_to :child_relationable, polymorphic: true, touch: true belongs_to :child_relationable, polymorphic: true, touch: true
has_one :opposite_related_content, class_name: 'RelatedContent', foreign_key: :related_content_id has_one :opposite_related_content, class_name: 'RelatedContent', foreign_key: :related_content_id
has_many :related_content_scores
validates :parent_relationable_id, presence: true validates :parent_relationable_id, presence: true
validates :parent_relationable_type, presence: true validates :parent_relationable_type, presence: true
@@ -14,7 +15,7 @@ class RelatedContent < ActiveRecord::Base
after_create :create_opposite_related_content, unless: proc { opposite_related_content.present? } after_create :create_opposite_related_content, unless: proc { opposite_related_content.present? }
scope :not_hidden, -> { where('positive_score - negative_score / LEAST(nullif(positive_score + negative_score, 0), 1) >= ?', RELATED_CONTENTS_REPORT_THRESHOLD) } scope :not_hidden, -> { where('positive_score - negative_score / LEAST(nullif(positive_score + negative_score, 0), 1) >= ?', RELATED_CONTENT_SCORE_THRESHOLD) }
private private

View File

@@ -0,0 +1,6 @@
class RelatedContentScore < ActiveRecord::Base
belongs_to :related_content, touch: true, counter_cache: :related_content_scores_count
validates :user_id, presence: true
validates :related_content_id, presence: true
end

View File

@@ -67,7 +67,7 @@ section "Creating Settings" do
Setting.create(key: 'map_latitude', value: 51.48) Setting.create(key: 'map_latitude', value: 51.48)
Setting.create(key: 'map_longitude', value: 0.0) Setting.create(key: 'map_longitude', value: 0.0)
Setting.create(key: 'map_zoom', value: 10) Setting.create(key: 'map_zoom', value: 10)
Setting.create(key: 'related_contents_report_threshold', value: -0.3) Setting.create(key: 'related_content_score_threshold', value: -0.3)
end end
section "Creating Geozones" do section "Creating Geozones" do

View File

@@ -0,0 +1,11 @@
class CreateRelatedContentScores < ActiveRecord::Migration
def change
create_table :related_content_scores do |t|
t.references :user, index: true, foreign_key: true
t.references :related_content, index: true, foreign_key: true
t.integer :value
end
add_index :related_content_scores, [:user_id, :related_content_id], name: "unique_user_related_content_scoring", unique: true, using: :btree
end
end

View File

@@ -0,0 +1,5 @@
class AddRelatedContentScoresCounterToRelatedContent < ActiveRecord::Migration
def change
add_column :related_contents, :related_content_scores_count, :integer, default: 0
end
end

View File

@@ -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: 20171219111046) do ActiveRecord::Schema.define(version: 20171220002802) 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"
@@ -854,6 +854,16 @@ ActiveRecord::Schema.define(version: 20171219111046) do
add_index "proposals", ["title"], name: "index_proposals_on_title", using: :btree add_index "proposals", ["title"], name: "index_proposals_on_title", using: :btree
add_index "proposals", ["tsv"], name: "index_proposals_on_tsv", using: :gin add_index "proposals", ["tsv"], name: "index_proposals_on_tsv", using: :gin
create_table "related_content_scores", force: :cascade do |t|
t.integer "user_id"
t.integer "related_content_id"
t.integer "value"
end
add_index "related_content_scores", ["related_content_id"], name: "index_related_content_scores_on_related_content_id", using: :btree
add_index "related_content_scores", ["user_id", "related_content_id"], name: "unique_user_related_content_scoring", unique: true, using: :btree
add_index "related_content_scores", ["user_id"], name: "index_related_content_scores_on_user_id", using: :btree
create_table "related_contents", force: :cascade do |t| create_table "related_contents", force: :cascade do |t|
t.integer "parent_relationable_id" t.integer "parent_relationable_id"
t.string "parent_relationable_type" t.string "parent_relationable_type"
@@ -862,6 +872,7 @@ ActiveRecord::Schema.define(version: 20171219111046) do
t.integer "related_content_id" t.integer "related_content_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.integer "related_content_scores_count", default: 0
end end
add_index "related_contents", ["child_relationable_type", "child_relationable_id"], name: "index_related_contents_on_child_relationable", using: :btree add_index "related_contents", ["child_relationable_type", "child_relationable_id"], name: "index_related_contents_on_child_relationable", using: :btree
@@ -1188,6 +1199,8 @@ ActiveRecord::Schema.define(version: 20171219111046) do
add_foreign_key "poll_recounts", "poll_officer_assignments", column: "officer_assignment_id" add_foreign_key "poll_recounts", "poll_officer_assignments", column: "officer_assignment_id"
add_foreign_key "poll_voters", "polls" add_foreign_key "poll_voters", "polls"
add_foreign_key "proposals", "communities" add_foreign_key "proposals", "communities"
add_foreign_key "related_content_scores", "related_contents"
add_foreign_key "related_content_scores", "users"
add_foreign_key "users", "geozones" add_foreign_key "users", "geozones"
add_foreign_key "valuators", "users" add_foreign_key "valuators", "users"
end end

View File

@@ -120,4 +120,4 @@ Setting['map_longitude'] = 0.0
Setting['map_zoom'] = 10 Setting['map_zoom'] = 10
# Related content # Related content
Setting['related_contents_report_threshold'] = -0.3 Setting['related_content_score_threshold'] = -0.3