From 6a68ba147b0d7812e591f20936c1571f16043e9a Mon Sep 17 00:00:00 2001 From: Isaac Janzen Date: Thu, 16 Mar 2023 14:09:22 -0500 Subject: [PATCH] FIX: Use tag-intersection route when building more_topics_url from tag-intersection --- app/controllers/tags_controller.rb | 12 ++++++++++-- spec/requests/tags_controller_spec.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 5604b401d2..a90a23ce94 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -102,8 +102,13 @@ class TagsController < ::ApplicationController Discourse.filters.each do |filter| define_method("show_#{filter}") do @tag_id = params[:tag_id].force_encoding("UTF-8") - @additional_tags = - params[:additional_tag_ids].to_s.split("/").map { |t| t.force_encoding("UTF-8") } + if params[:additional_tag_ids] + @additional_tags = params[:additional_tag_ids].to_s.split("/") + elsif params[:tags] + # Set additional tags to all passed tags excluding the primary tag (tag_id) + @additional_tags = params[:tags].reject { |t| t == params[:tag_id] } + end + @additional_tags = @additional_tags&.map { |t| t.force_encoding("UTF-8") } list_opts = build_topic_list_options @list = nil @@ -483,6 +488,9 @@ class TagsController < ::ApplicationController def url_method(opts = {}) if opts[:category_slug_path_with_id] "tag_category_#{action_name}_path" + # expect tag intersection if multiple tags are present + elsif opts[:tags] && opts[:tags].length > 1 + "tag_intersection_path" else "tag_#{action_name}_path" end diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb index 63b60c5c31..8656675993 100644 --- a/spec/requests/tags_controller_spec.rb +++ b/spec/requests/tags_controller_spec.rb @@ -392,6 +392,24 @@ RSpec.describe TagsController do end end end + + context "with a tag intersection" do + it "should generate a tag intersection `load_more_topic` url" do + tag = Fabricate(:tag) + tag_2 = Fabricate(:tag) + + # Create topics with both tags included + 3.times { Fabricate(:topic, tags: [tag, tag_2]) } + + sign_in(admin) + get "/tags/intersection/#{tag.name}/#{tag_2.name}.json?per_page=2" + + expect(response.status).to eq(200) + expect(response.parsed_body["topic_list"]["more_topics_url"]).to eq( + "/tags/intersection/#{tag.name}/#{tag_2.name}?match_all_tags=true&page=1&per_page=2&tags%5B%5D=#{tag.name}&tags%5B%5D=#{tag_2.name}", + ) + end + end end describe "#show" do