DEV: Scope topic voting tables to avoid confusion with post voting
This commit is contained in:
parent
fdb1f98a96
commit
05396b6fb4
|
@ -2,7 +2,7 @@
|
|||
|
||||
module DiscourseTopicVoting
|
||||
class CategorySetting < ActiveRecord::Base
|
||||
self.table_name = "discourse_voting_category_settings"
|
||||
self.table_name = "topic_voting_category_settings"
|
||||
|
||||
belongs_to :category, inverse_of: :discourse_topic_voting_category_setting
|
||||
|
||||
|
@ -12,24 +12,24 @@ module DiscourseTopicVoting
|
|||
|
||||
def unarchive_votes
|
||||
DB.exec(<<~SQL, { category_id: self.category_id })
|
||||
UPDATE discourse_voting_votes
|
||||
UPDATE topic_voting_votes
|
||||
SET archive=false
|
||||
FROM topics
|
||||
WHERE topics.category_id = :category_id
|
||||
AND topics.deleted_at is NULL
|
||||
AND NOT topics.closed
|
||||
AND NOT topics.archived
|
||||
AND discourse_voting_votes.topic_id = topics.id
|
||||
AND topic_voting_votes.topic_id = topics.id
|
||||
SQL
|
||||
end
|
||||
|
||||
def archive_votes
|
||||
DB.exec(<<~SQL, { category_id: self.category_id })
|
||||
UPDATE discourse_voting_votes
|
||||
UPDATE topic_voting_votes
|
||||
SET archive=true
|
||||
FROM topics
|
||||
WHERE topics.category_id = :category_id
|
||||
AND discourse_voting_votes.topic_id = topics.id
|
||||
AND topic_voting_votes.topic_id = topics.id
|
||||
SQL
|
||||
end
|
||||
|
||||
|
@ -41,7 +41,7 @@ end
|
|||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: discourse_voting_category_settings
|
||||
# Table name: topic_voting_category_settings
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# category_id :integer
|
||||
|
@ -50,5 +50,5 @@ end
|
|||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_discourse_voting_category_settings_on_category_id (category_id) UNIQUE
|
||||
# index_topic_voting_category_settings_on_category_id (category_id) UNIQUE
|
||||
#
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
module DiscourseTopicVoting
|
||||
class TopicVoteCount < ActiveRecord::Base
|
||||
self.table_name = "discourse_voting_topic_vote_count"
|
||||
self.table_name = "topic_voting_topic_vote_count"
|
||||
|
||||
belongs_to :topic
|
||||
end
|
||||
|
@ -10,7 +10,7 @@ end
|
|||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: discourse_voting_topic_vote_count
|
||||
# Table name: topic_voting_topic_vote_count
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# topic_id :integer
|
||||
|
@ -20,5 +20,5 @@ end
|
|||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_discourse_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE
|
||||
# index_topic_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE
|
||||
#
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
module DiscourseTopicVoting
|
||||
class Vote < ActiveRecord::Base
|
||||
self.table_name = "discourse_voting_votes"
|
||||
self.table_name = "topic_voting_votes"
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :topic
|
||||
|
@ -11,7 +11,7 @@ end
|
|||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: discourse_voting_votes
|
||||
# Table name: topic_voting_votes
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# topic_id :integer
|
||||
|
@ -22,5 +22,5 @@ end
|
|||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_discourse_voting_votes_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
||||
# index_topic_voting_votes_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
||||
#
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "migration/table_dropper"
|
||||
|
||||
class RenameVotingTables < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
unless table_exists?(:topic_voting_topic_vote_count)
|
||||
Migration::TableDropper.read_only_table(:discourse_voting_topic_vote_count)
|
||||
execute <<~SQL
|
||||
CREATE TABLE topic_voting_topic_vote_count
|
||||
(LIKE discourse_voting_topic_vote_count INCLUDING ALL);
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
INSERT INTO topic_voting_topic_vote_count
|
||||
SELECT *
|
||||
FROM discourse_voting_topic_vote_count
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE discourse_voting_topic_vote_count_id_seq
|
||||
RENAME TO topic_voting_topic_vote_count_id_seq
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE topic_voting_topic_vote_count_id_seq
|
||||
OWNED BY topic_voting_topic_vote_count.id
|
||||
SQL
|
||||
|
||||
add_index :topic_voting_topic_vote_count, :topic_id, unique: true
|
||||
end
|
||||
|
||||
unless table_exists?(:topic_voting_votes)
|
||||
Migration::TableDropper.read_only_table(:discourse_voting_votes)
|
||||
execute <<~SQL
|
||||
CREATE TABLE topic_voting_votes
|
||||
(LIKE discourse_voting_votes INCLUDING ALL);
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
INSERT INTO topic_voting_votes
|
||||
SELECT *
|
||||
FROM discourse_voting_votes
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE discourse_voting_votes_id_seq
|
||||
RENAME TO topic_voting_votes_id_seq
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE topic_voting_votes_id_seq
|
||||
OWNED BY topic_voting_votes.id
|
||||
SQL
|
||||
|
||||
add_index :topic_voting_votes, %i[user_id topic_id], unique: true
|
||||
end
|
||||
|
||||
unless table_exists?(:topic_voting_category_settings)
|
||||
Migration::TableDropper.read_only_table(:discourse_voting_category_settings)
|
||||
execute <<~SQL
|
||||
CREATE TABLE topic_voting_category_settings
|
||||
(LIKE discourse_voting_category_settings INCLUDING ALL);
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
INSERT INTO topic_voting_category_settings
|
||||
SELECT *
|
||||
FROM discourse_voting_category_settings
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE discourse_voting_category_settings_id_seq
|
||||
RENAME TO topic_voting_category_settings_id_seq
|
||||
SQL
|
||||
|
||||
execute <<~SQL
|
||||
ALTER SEQUENCE topic_voting_category_settings_id_seq
|
||||
OWNED BY topic_voting_category_settings.id
|
||||
SQL
|
||||
|
||||
add_index :topic_voting_category_settings, :category_id, unique: true
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
|
@ -35,14 +35,14 @@ module DiscourseTopicVoting
|
|||
count = self.votes.count
|
||||
|
||||
DB.exec(<<~SQL, topic_id: self.id, votes_count: count)
|
||||
INSERT INTO discourse_voting_topic_vote_count
|
||||
INSERT INTO topic_voting_topic_vote_count
|
||||
(topic_id, votes_count, created_at, updated_at)
|
||||
VALUES
|
||||
(:topic_id, :votes_count, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT (topic_id) DO UPDATE SET
|
||||
votes_count = :votes_count,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE discourse_voting_topic_vote_count.topic_id = :topic_id
|
||||
WHERE topic_voting_topic_vote_count.topic_id = :topic_id
|
||||
SQL
|
||||
end
|
||||
|
||||
|
|
|
@ -5,15 +5,15 @@ module DiscourseTopicVoting
|
|||
def list_voted_by(user)
|
||||
create_list(:user_topics) do |topics|
|
||||
topics.joins(
|
||||
"INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id",
|
||||
).where("discourse_voting_votes.user_id = ?", user.id)
|
||||
"INNER JOIN topic_voting_votes ON topic_voting_votes.topic_id = topics.id",
|
||||
).where("topic_voting_votes.user_id = ?", user.id)
|
||||
end
|
||||
end
|
||||
|
||||
def list_votes
|
||||
create_list(:votes, unordered: true) do |topics|
|
||||
topics.joins(
|
||||
"LEFT JOIN discourse_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
|
||||
"LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
|
||||
).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
|
||||
end
|
||||
end
|
||||
|
|
12
plugin.rb
12
plugin.rb
|
@ -52,13 +52,13 @@ after_initialize do
|
|||
if user
|
||||
result =
|
||||
result.select(
|
||||
"topics.*, COALESCE((SELECT 1 FROM discourse_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted",
|
||||
"topics.*, COALESCE((SELECT 1 FROM topic_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted",
|
||||
)
|
||||
|
||||
if options[:state] == "my_votes"
|
||||
result =
|
||||
result.joins(
|
||||
"INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id AND discourse_voting_votes.user_id = #{user.id}",
|
||||
"INNER JOIN topic_voting_votes ON topic_voting_votes.topic_id = topics.id AND topic_voting_votes.user_id = #{user.id}",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -67,9 +67,9 @@ after_initialize do
|
|||
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
|
||||
result =
|
||||
result.joins(
|
||||
"LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id",
|
||||
"LEFT JOIN topic_voting_topic_vote_count ON topic_voting_topic_vote_count.topic_id = topics.id",
|
||||
).reorder(
|
||||
"COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC",
|
||||
"COALESCE(topic_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC",
|
||||
)
|
||||
end
|
||||
|
||||
|
@ -104,14 +104,14 @@ after_initialize do
|
|||
|
||||
register_search_advanced_filter(/^min_vote_count:(\d+)$/) do |posts, match|
|
||||
posts.where(
|
||||
"(SELECT votes_count FROM discourse_voting_topic_vote_count WHERE discourse_voting_topic_vote_count.topic_id = posts.topic_id) >= ?",
|
||||
"(SELECT votes_count FROM topic_voting_topic_vote_count WHERE topic_voting_topic_vote_count.topic_id = posts.topic_id) >= ?",
|
||||
match.to_i,
|
||||
)
|
||||
end
|
||||
|
||||
register_search_advanced_order(:votes) do |posts|
|
||||
posts.reorder(
|
||||
"COALESCE((SELECT dvtvc.votes_count FROM discourse_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC",
|
||||
"COALESCE((SELECT dvtvc.votes_count FROM topic_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC",
|
||||
)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue