Skip to content

Commit

Permalink
FEATURE: Allow order:votes on /filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nattsw committed Aug 7, 2024
1 parent 55970d4 commit 28ef5b2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ module ::DiscourseTopicVoting
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 }

filter_order_votes = ->(scope, order_direction) do
scope = scope.joins(:topic_vote_count)
scope.order("topic_voting_topic_vote_count.votes_count #{order_direction}")
end

add_filter_custom_filter("order:votes", &filter_order_votes)

on(:topic_status_updated) do |topic, status, enabled|
next if topic.trashed?
next if %w[closed autoclosed archived].exclude?(status)
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/topics_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

RSpec.describe TopicsFilter do
describe "when extending order:{col}" 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: 1).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_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])
end
end
end

0 comments on commit 28ef5b2

Please sign in to comment.