Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/controllers/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SearchesController < ApplicationController
def show
@query = params[:q].blank? ? nil : params[:q]

if card = Current.user.accessible_cards.find_by_id(@query)
if card = find_card(@query)
respond_to do |format|
format.html { @card = card }
format.json { set_page_and_extract_portion_from Current.user.accessible_cards.where(id: card.id) }
Expand All @@ -23,4 +23,16 @@ def show
end
end
end

private
def find_card(query)
return if query.blank?

if query.to_s.match?(/\A#\d+\z/)
number = query.to_s.delete_prefix("#").to_i
Current.user.accessible_cards.find_by(number: number)
else
Current.user.accessible_cards.find_by(id: query)
end
end
end
8 changes: 8 additions & 0 deletions test/controllers/searches_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class SearchesControllerTest < ActionDispatch::IntegrationTest
get search_path(q: @card.id, script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']"

# Searching by card number without prefix uses full search (no direct redirect)
get search_path(q: @card.number, script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']", count: 0

# Searching by card number with hash prefix
get search_path(q: "##{@card.number}", script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']"

# Searching with non-existent card id
get search_path(q: "999999", script_name: "/#{@account.external_account_id}")
assert_select "form[data-controller='auto-submit']", count: 0
Expand Down