A platform for community discussion. Free, open, simple.
DEV: Support `posters-(min|max):<count>` on `/filter` route (#21095)
Alan Guo Xiang Tan782b26d0eb60
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex d74b4c7093a..50edd294be1 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -41,9 +41,21 @@ class TopicsFilterwhen "in"filter_in(values: values)when "posts-min"- filter_by_number_of_posts(min: values.last)+ min = values.last+ break if !integer_string?(min)+ filter_by_number_of_posts(min: min)when "posts-max"- filter_by_number_of_posts(max: values.last)+ max = values.last+ break if !integer_string?(max)+ filter_by_number_of_posts(max: max)+ when "posters-min"+ min = values.last+ break if !integer_string?(min)+ filter_by_number_of_posters(min: min)+ when "posters-max"+ max = values.last+ break if !integer_string?(max)+ filter_by_number_of_posters(max: max)when "status"values.each { |status| @scope = filter_status(status: status) }when "tags"@@ -81,13 +93,21 @@ class TopicsFilterprivate- def filter_by_number_of_posts(min: nil, max: nil)+ def filter_by_topic_range(column_name:, min: nil, max: nil){ min => ">=", max => "<=" }.each do |value, operator|- next if !value || value !~ /^\d+$/- @scope = @scope.where("topics.posts_count #{operator} ?", value)+ next if !value+ @scope = @scope.where("topics.#{column_name} #{operator} ?", value)endend+ def filter_by_number_of_posts(min: nil, max: nil)+ filter_by_topic_range(column_name: "posts_count", min:, max:)+ end++ def filter_by_number_of_posters(min: nil, max: nil)+ filter_by_topic_range(column_name: "participant_count", min:, max:)+ end+def filter_categories(values:)exclude_subcategories_category_slugs = []include_subcategories_category_slugs = []@@ -332,4 +352,8 @@ class TopicsFilterdef include_topics_with_any_tags(tag_ids)@scope = @scope.joins(:topic_tags).where("topic_tags.tag_id IN (?)", tag_ids).distinct(:id)end++ def integer_string?(string)+ string =~ /\A\d+\z/+ endenddiff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 548f46f8ac9..3424ee191bb 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -929,5 +929,100 @@ RSpec.describe TopicsFilter doendendend++ describe "when filtering by number of posters in a topic" do+ fab!(:topic_with_1_participant) { Fabricate(:topic, participant_count: 1) }+ fab!(:topic_with_2_participants) { Fabricate(:topic, participant_count: 2) }+ fab!(:topic_with_3_participants) { Fabricate(:topic, participant_count: 3) }++ describe "when query string is `posters-min:1`" do+ it "should only return topics with at least 1 participant" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-min:1")+ .pluck(:id),+ ).to contain_exactly(+ topic_with_1_participant.id,+ topic_with_2_participants.id,+ topic_with_3_participants.id,+ )+ end+ end++ describe "when query string is `posters-min:3`" do+ it "should only return topics with at least 3 participants" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-min:3")+ .pluck(:id),+ ).to contain_exactly(topic_with_3_participants.id)+ end+ end++ describe "when query string is `posters-max:1`" do+ it "should only return topics with at most 1 participant" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-max:1")+ .pluck(:id),+ ).to contain_exactly(topic_with_1_participant.id)+ end+ end++ describe "when query string is `posters-max:3`" do+ it "should only return topics with at most 3 participants" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-max:3")+ .pluck(:id),+ ).to contain_exactly(+ topic_with_1_participant.id,+ topic_with_2_participants.id,+ topic_with_3_participants.id,+ )+ end+ end++ describe "when query string is `posters-min:1 posters-max:2`" do+ it "should only return topics with at least 1 participant and at most 2 participants" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-min:1 posters-max:2")+ .pluck(:id),+ ).to contain_exactly(topic_with_1_participant.id, topic_with_2_participants.id)+ end+ end++ describe "when query string is `posters-min:3 posters-min:2 posters-max:1 posters-max:3`" do+ it "should only return topics with at least 2 participants and at most 3 participants as it ignores earlier filters which are duplicated" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-min:3 posters-min:2 posters-max:1 posters-max:3")+ .pluck(:id),+ ).to contain_exactly(topic_with_2_participants.id, topic_with_3_participants.id)+ end+ end++ describe "when query string is `posters-min:invalid posters-max:invalid`" do+ it "should ignore the filters with invalid values" do+ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string("posters-min:invalid posters-max:invalid")+ .pluck(:id),+ ).to contain_exactly(+ topic_with_1_participant.id,+ topic_with_2_participants.id,+ topic_with_3_participants.id,+ )+ end+ end+ endendend