diff --git a/app/helpers/talks_helper.rb b/app/helpers/talks_helper.rb index 5edbaed47..55e2d37e9 100644 --- a/app/helpers/talks_helper.rb +++ b/app/helpers/talks_helper.rb @@ -1,2 +1,10 @@ module TalksHelper + def normalize_back_to(back_to) + return nil if back_to.blank? + + uri = URI.parse(back_to) + params = Rack::Utils.parse_query(uri.query.to_s).except("infinite_count", "format", "page") + uri.query = params.present? ? params.to_query : nil + uri.to_s + end end diff --git a/app/views/talks/_card.html.erb b/app/views/talks/_card.html.erb index cf701b90f..249293c0d 100644 --- a/app/views/talks/_card.html.erb +++ b/app/views/talks/_card.html.erb @@ -3,7 +3,7 @@ <% language = Language.by_code(talk.language) %>
- <%= link_to talk_path(talk, back_to: back_to, back_to_title: back_to_title), class: "flex aspect-video overflow-hidden relative group", data: {action: "mouseover->preserve-scroll#updateLinkBackToWithScrollPosition"} do %> + <%= link_to talk_path(talk, back_to: normalize_back_to(back_to), back_to_title: back_to_title), class: "flex aspect-video overflow-hidden relative group", data: {action: "mouseover->preserve-scroll#updateLinkBackToWithScrollPosition"} do %> <% if language && language != "English" %>
<%= language_to_emoji(language) %> @@ -62,7 +62,7 @@
- <%= link_to talk_path(talk, back_to: back_to, back_to_title: back_to_title), data: {action: "mouseover->preserve-scroll#updateLinkBackToWithScrollPosition"} do %> + <%= link_to talk_path(talk, back_to: normalize_back_to(back_to), back_to_title: back_to_title), data: {action: "mouseover->preserve-scroll#updateLinkBackToWithScrollPosition"} do %> <%= content_tag :h2, class: "text-sm font-sans font-medium" do %> <%= sanitize(talk.title_with_snippet, tags: ["mark"]) %> <% end %> diff --git a/test/helpers/talks_helper_test.rb b/test/helpers/talks_helper_test.rb new file mode 100644 index 000000000..9ae2aa12a --- /dev/null +++ b/test/helpers/talks_helper_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +class TalksHelperTest < ActionView::TestCase + test "normalize_back_to removes infinite_count and format params" do + assert_equal "talks_url?s=rails", normalize_back_to("talks_url?s=rails") + assert_equal "talks_url?s=rails", normalize_back_to("talks_url?s=rails&infinite_count=1") + assert_equal "talks_url?s=rails", normalize_back_to("talks_url?s=rails&format=turbo_stream") + assert_equal "talks_url?page=2", normalize_back_to("talks_url?format=turbo_stream&page=2") + assert_equal "talks_url?s=rails", normalize_back_to("talks_url?s=rails&infinite_count=1&format=turbo_stream") + assert_equal "talks_url?s=rails", normalize_back_to("talks_url?s=rails&infinite_count=111&format=turbo_stream") + end + + test "normalize_back_to handles empty params" do + assert_equal "talks_url", normalize_back_to("talks_url") + end + + test "normalize_back_to preserves other params" do + result = normalize_back_to("talks_url?topic=ruby&speaker=john&infinite_count=1") + assert_equal ["speaker=john", "topic=ruby"].sort, URI.parse(result).query.split("&").sort + end +end