@@ -530,6 +530,7 @@ def win_team_clause(name)
530530 end
531531
532532 def apply_filters ( matches )
533+ matches = apply_search ( matches )
533534 matches = matches . by_tournament ( params [ :tournament ] ) if params [ :tournament ] . present?
534535 matches = matches . by_region ( params [ :region ] ) if params [ :region ] . present?
535536 matches = matches . by_patch ( params [ :patch ] ) if params [ :patch ] . present?
@@ -546,6 +547,36 @@ def apply_filters(matches)
546547 matches
547548 end
548549
550+ def apply_search ( matches )
551+ return matches unless params [ :search ] . present?
552+
553+ term = ActiveRecord ::Base . sanitize_sql_like ( params [ :search ] )
554+ norm_term = ActiveRecord ::Base . sanitize_sql_like ( normalize_search_term ( params [ :search ] ) )
555+
556+ # Search by original term (case-insensitive) OR by normalized term
557+ # translate() maps special chars (Ø→O, æ→a, etc.) directly in PostgreSQL.
558+ matches . where (
559+ 'lower(opponent_team_name) LIKE lower(:t) OR lower(our_team_name) LIKE lower(:t) ' \
560+ 'OR lower(tournament_display) LIKE lower(:t) ' \
561+ 'OR translate(lower(opponent_team_name), :from, :to) LIKE :n ' \
562+ 'OR translate(lower(our_team_name), :from, :to) LIKE :n ' \
563+ 'OR translate(lower(tournament_display), :from, :to) LIKE :n' ,
564+ t : "%#{ term } %" ,
565+ n : "%#{ norm_term } %" ,
566+ from : 'øæåðþ' ,
567+ to : 'oaadt'
568+ )
569+ end
570+
571+ def normalize_search_term ( term )
572+ term . downcase
573+ . tr ( 'øåðþ' , 'oadt' )
574+ . gsub ( 'æ' , 'ae' )
575+ . gsub ( 'ß' , 'ss' )
576+ . unicode_normalize ( :nfkd )
577+ . gsub ( /\p {Mn}/ , '' )
578+ end
579+
549580 def build_total_pages ( result , page )
550581 pages = result [ :per_page ] . positive? ? [ ( result [ :total ] . to_f / result [ :per_page ] ) . ceil , 1 ] . max : 1
551582 result [ :data ] . length >= result [ :per_page ] ? [ pages , page ] . max : pages
0 commit comments