Create Related Content Scores
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
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
|
||||
|
||||
belongs_to :parent_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_many :related_content_scores
|
||||
|
||||
validates :parent_relationable_id, 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? }
|
||||
|
||||
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
|
||||
|
||||
|
||||
6
app/models/related_content_score.rb
Normal file
6
app/models/related_content_score.rb
Normal 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
|
||||
@@ -67,7 +67,7 @@ section "Creating Settings" do
|
||||
Setting.create(key: 'map_latitude', value: 51.48)
|
||||
Setting.create(key: 'map_longitude', value: 0.0)
|
||||
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
|
||||
|
||||
section "Creating Geozones" do
|
||||
|
||||
11
db/migrate/20171219184209_create_related_content_scores.rb
Normal file
11
db/migrate/20171219184209_create_related_content_scores.rb
Normal 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
|
||||
@@ -0,0 +1,5 @@
|
||||
class AddRelatedContentScoresCounterToRelatedContent < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :related_contents, :related_content_scores_count, :integer, default: 0
|
||||
end
|
||||
end
|
||||
15
db/schema.rb
15
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: 20171219111046) do
|
||||
ActiveRecord::Schema.define(version: 20171220002802) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
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", ["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|
|
||||
t.integer "parent_relationable_id"
|
||||
t.string "parent_relationable_type"
|
||||
@@ -862,6 +872,7 @@ ActiveRecord::Schema.define(version: 20171219111046) do
|
||||
t.integer "related_content_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.integer "related_content_scores_count", default: 0
|
||||
end
|
||||
|
||||
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_voters", "polls"
|
||||
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 "valuators", "users"
|
||||
end
|
||||
|
||||
@@ -120,4 +120,4 @@ Setting['map_longitude'] = 0.0
|
||||
Setting['map_zoom'] = 10
|
||||
|
||||
# Related content
|
||||
Setting['related_contents_report_threshold'] = -0.3
|
||||
Setting['related_content_score_threshold'] = -0.3
|
||||
|
||||
Reference in New Issue
Block a user