Skip to content

Commit

Permalink
FIX: Care for nil counts when ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
nattsw committed Aug 12, 2024
1 parent a1243b0 commit bc9d75a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module ::DiscourseTopicVoting

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

add_filter_custom_filter("order:votes", &filter_order_votes)
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/topics_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
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: 1).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_med.id, topic_high.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])
).to eq([topic_high.id, topic_med.id, topic_low.id, topic_nil.id])
end
end
end

0 comments on commit bc9d75a

Please sign in to comment.