Skip to content

Commit

Permalink
remove format parms from back_to
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Nov 24, 2024
1 parent b3d0d65 commit 16daef0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/helpers/talks_helper.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/views/talks/_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% language = Language.by_code(talk.language) %>

<div class="card card-compact bg-white border w-full" id="<%= dom_id talk %>">
<%= 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" %>
<div class="absolute top-0 left-0 z-20 m-3 p-1 px-2 bg-black/15 backdrop-blur-md rounded-full">
<span><%= language_to_emoji(language) %></span>
Expand Down Expand Up @@ -62,7 +62,7 @@
<div class="card-body flex flex-row justify-between items-start gap-2">
<div class="flex flex-col items-start h-full justify-between gap-2 w-full">
<div class="flex items-start justify-between gap-2 w-full">
<%= 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 %>
Expand Down
21 changes: 21 additions & 0 deletions test/helpers/talks_helper_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 16daef0

Please sign in to comment.