A platform for community discussion. Free, open, simple.
FEATURE: support topics filter UI when ordering on votes (#36516)
Sam9405fa676235
diff --git a/plugins/discourse-topic-voting/app/models/discourse_topic_voting/topic_vote_count.rb b/plugins/discourse-topic-voting/app/models/discourse_topic_voting/topic_vote_count.rbindex 5b83a3aa056..c94b4226d69 100644--- a/plugins/discourse-topic-voting/app/models/discourse_topic_voting/topic_vote_count.rb+++ b/plugins/discourse-topic-voting/app/models/discourse_topic_voting/topic_vote_count.rb@@ -13,13 +13,14 @@ end# Table name: topic_voting_topic_vote_count## id :bigint not null, primary key-# topic_id :integer# votes_count :integer# created_at :datetime not null# updated_at :datetime not null+# topic_id :integer## Indexes#-# index_topic_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE-# topic_voting_topic_vote_count_topic_id_idx (topic_id) UNIQUE+# index_topic_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE+# index_topic_voting_topic_vote_count_on_votes_count (votes_count)+# topic_voting_topic_vote_count_topic_id_idx (topic_id) UNIQUE#diff --git a/plugins/discourse-topic-voting/config/locales/server.en.yml b/plugins/discourse-topic-voting/config/locales/server.en.ymlindex 13a3be931d3..158b6619f30 100755--- a/plugins/discourse-topic-voting/config/locales/server.en.yml+++ b/plugins/discourse-topic-voting/config/locales/server.en.yml@@ -16,3 +16,9 @@ en:duplicated_votes:one: "A vote could not be moved because the user already voted in the other topic."other: "%{count} votes could not be moved because their users already voted in the other topic."+ filter:+ description:+ topic_votes_min: "Minimum number of topic votes"+ topic_votes_max: "Maximum number of topic votes"+ order_topic_votes: "Order topics by most votes"+ order_topic_votes_asc: "Order topics by fewest votes"diff --git a/plugins/discourse-topic-voting/db/migrate/20251209012613_add_vote_counts_index.rb b/plugins/discourse-topic-voting/db/migrate/20251209012613_add_vote_counts_index.rbnew file mode 100644index 00000000000..a57acb1f8e2--- /dev/null+++ b/plugins/discourse-topic-voting/db/migrate/20251209012613_add_vote_counts_index.rb@@ -0,0 +1,8 @@+# frozen_string_literal: true+class AddVoteCountsIndex < ActiveRecord::Migration[8.0]+ def change+ add_index :topic_voting_topic_vote_count,+ :votes_count,+ name: "index_topic_voting_topic_vote_count_on_votes_count"+ end+enddiff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_votes_filter.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_votes_filter.rbnew file mode 100644index 00000000000..d18e3fd9b03--- /dev/null+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/topic_votes_filter.rb@@ -0,0 +1,26 @@+# frozen_string_literal: true++module DiscourseTopicVoting+ class TopicVotesFilter+ class << self+ def apply(scope, min_votes: nil, max_votes: nil, order_direction: nil)+ column_sql = "COALESCE(topic_voting_topic_vote_count.votes_count, 0)::integer"+ scoped =+ scope.joins(+ "INNER JOIN topic_voting_category_settings tvcs ON tvcs.category_id = topics.category_id",+ ).left_outer_joins(:topic_vote_count)++ scoped = scoped.where("#{column_sql} >= ?", min_votes) if min_votes++ scoped = scoped.where("#{column_sql} <= ?", max_votes) if max_votes++ if order_direction+ direction = order_direction.upcase == "ASC" ? "ASC" : "DESC"+ scoped = scoped.order("#{column_sql} #{direction}, topics.bumped_at DESC")+ end++ scoped+ end+ end+ end+enddiff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex 9f8e4107d98..da3036f2047 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -25,6 +25,7 @@ module ::DiscourseTopicVotingendrequire_relative "lib/discourse_topic_voting/engine"+require_relative "lib/discourse_topic_voting/topic_votes_filter"after_initialize doreloadable_patch do@@ -117,15 +118,61 @@ after_initialize do)end+ register_modifier(:topics_filter_options) do |results, _guardian|+ results.concat(+ [+ {+ name: "votes-min:",+ description: I18n.t("topic_voting.filter.description.topic_votes_min"),+ type: "number",+ },+ {+ name: "votes-max:",+ description: I18n.t("topic_voting.filter.description.topic_votes_max"),+ type: "number",+ },+ {+ name: "order:votes",+ description: I18n.t("topic_voting.filter.description.order_topic_votes"),+ },+ {+ name: "order:votes-asc",+ description: I18n.t("topic_voting.filter.description.order_topic_votes_asc"),+ },+ ],+ )++ results+ end+add_to_serializer(:current_user, :votes_exceeded) { object.reached_voting_limit? }add_to_serializer(:current_user, :votes_count) { object.vote_count }add_to_serializer(:current_user, :votes_left) { [object.vote_limit - object.vote_count, 0].max }add_to_serializer(:current_user, :vote_limit) { object.vote_limit }+ topic_votes_value_from = ->(values) do+ value = values&.last+ return if value.blank? || value !~ /\A\d+\z/++ value.to_i+ end++ add_filter_custom_filter("votes-min") do |scope, filter_values, _guardian|+ value = topic_votes_value_from.call(filter_values)+ next scope if value.nil?++ DiscourseTopicVoting::TopicVotesFilter.apply(scope, min_votes: value)+ end++ add_filter_custom_filter("votes-max") do |scope, filter_values, _guardian|+ value = topic_votes_value_from.call(filter_values)+ next scope if value.nil?++ DiscourseTopicVoting::TopicVotesFilter.apply(scope, max_votes: value)+ end+filter_order_votes = ->(scope, order_direction, _guardian) do- scope.joins(:topic_vote_count).order(- "COALESCE(topic_voting_topic_vote_count.votes_count, 0)::integer #{order_direction}",- )+ DiscourseTopicVoting::TopicVotesFilter.apply(scope, order_direction:)endadd_filter_custom_filter("order:votes", &filter_order_votes)diff --git a/plugins/discourse-topic-voting/spec/lib/topics_filter_spec.rb b/plugins/discourse-topic-voting/spec/lib/topics_filter_spec.rbindex 08e8e4421d7..564e81a4c02 100644--- a/plugins/discourse-topic-voting/spec/lib/topics_filter_spec.rb+++ b/plugins/discourse-topic-voting/spec/lib/topics_filter_spec.rb@@ -1,25 +1,105 @@# frozen_string_literal: trueRSpec.describe TopicsFilter do- describe "adding order:votes to DiscoursePluginRegistry.custom_filter_mappings" do- fab!(:topic_high) { Fabricate(:topic_voting_vote_count, votes_count: 10).topic }- fab!(:topic_med) { Fabricate(:topic_voting_vote_count, votes_count: 5).topic }- fab!(:topic_low) { Fabricate(:topic_voting_vote_count, votes_count: 0).topic }- fab!(:topic_nil) { Fabricate(:topic_voting_vote_count, votes_count: nil).topic }-- it "sorts votes in ascending order" do- expect(- TopicsFilter- .new(guardian: Guardian.new)- .filter_from_query_string("order:votes-asc")- .pluck(:id),- ).to eq([topic_low.id, topic_nil.id, topic_med.id, topic_high.id])- end-- it "sorts votes in default descending order" do- expect(- TopicsFilter.new(guardian: Guardian.new).filter_from_query_string("order:votes").pluck(:id),- ).to eq([topic_high.id, topic_med.id, topic_low.id, topic_nil.id])+ before { SiteSetting.topic_voting_enabled = true }++ describe "topics filter option metadata" do+ it "adds topic voting range filters and order helpers" do+ options = TopicsFilter.option_info(Guardian.new)++ min_option = options.find { |option| option[:name] == "votes-min:" }+ max_option = options.find { |option| option[:name] == "votes-max:" }+ order_option = options.find { |option| option[:name] == "order:votes" }+ order_asc_option = options.find { |option| option[:name] == "order:votes-asc" }++ expect(min_option).to include(+ name: "votes-min:",+ description: I18n.t("topic_voting.filter.description.topic_votes_min"),+ type: "number",+ )++ expect(max_option).to include(+ name: "votes-max:",+ description: I18n.t("topic_voting.filter.description.topic_votes_max"),+ type: "number",+ )++ expect(order_option).to include(+ name: "order:votes",+ description: I18n.t("topic_voting.filter.description.order_topic_votes"),+ )++ expect(order_asc_option).to include(+ name: "order:votes-asc",+ description: I18n.t("topic_voting.filter.description.order_topic_votes_asc"),+ )+ end+ end++ describe "topic votes filtering" do+ fab!(:voting_category, :category)+ fab!(:non_voting_category, :category)+ let!(:_category_setting) do+ DiscourseTopicVoting::CategorySetting.create!(category: voting_category)+ end++ fab!(:topic_high) do+ topic = Fabricate(:topic, category: voting_category)+ Fabricate(:topic_voting_vote_count, topic:, votes_count: 10)+ topic+ end++ fab!(:topic_med) do+ topic = Fabricate(:topic, category: voting_category)+ Fabricate(:topic_voting_vote_count, topic:, votes_count: 5)+ topic+ end++ fab!(:topic_low) do+ topic = Fabricate(:topic, category: voting_category)+ Fabricate(:topic_voting_vote_count, topic:, votes_count: 0)+ topic+ end++ fab!(:topic_nil) do+ topic = Fabricate(:topic, category: voting_category)+ Fabricate(:topic_voting_vote_count, topic:, votes_count: nil)+ topic+ end++ fab!(:topic_without_count) { Fabricate(:topic, category: voting_category) }++ fab!(:non_voting_topic) do+ topic = Fabricate(:topic, category: non_voting_category)+ Fabricate(:topic_voting_vote_count, topic:, votes_count: 20)+ topic+ end++ let(:filter) { TopicsFilter.new(guardian: Guardian.new) }++ it "filters by minimum vote count" do+ expect(filter.filter_from_query_string("votes-min:6").pluck(:id)).to eq([topic_high.id])+ end++ it "filters by maximum vote count including topics without vote counts" do+ expect(filter.filter_from_query_string("votes-max:0").pluck(:id)).to match_array(+ [topic_low.id, topic_nil.id, topic_without_count.id],+ )+ end++ it "sorts by topic votes descending by default" do+ ids = filter.filter_from_query_string("order:votes").pluck(:id)++ expect(ids).to match_array(+ [topic_high.id, topic_med.id, topic_low.id, topic_nil.id, topic_without_count.id],+ )+ end++ it "sorts by topic votes ascending when requested" do+ ids = filter.filter_from_query_string("order:votes-asc").pluck(:id)++ expect(ids.last(2)).to eq([topic_med.id, topic_high.id])+ expect(ids.first(3)).to match_array([topic_low.id, topic_nil.id, topic_without_count.id])endendend