From 912e455f53e665851fba166d73c6ec1326b83028 Mon Sep 17 00:00:00 2001 From: Eric Pugh Date: Wed, 11 Dec 2024 13:44:19 -0500 Subject: [PATCH 1/2] ship a first cut of being smarter (#1133) * Leverage the use of the helper to be smart about turbo, and use it everywhere * Clean up the link creation titles for cases. --- app/assets/stylesheets/froggy.css | 2 +- app/helpers/application_helper.rb | 39 ++++++++++++++++++- app/helpers/home_helper.rb | 8 +--- .../tries_visualization/show.html.erb | 2 +- app/views/books/_case.html.erb | 2 +- app/views/home/_case.html.erb | 6 +-- app/views/home/_case_summary.html.erb | 2 +- app/views/home/show.html.erb | 2 +- .../_moar_judgements_needed.html.erb | 2 +- app/views/layouts/_header.html.erb | 2 +- app/views/ratings/index.html.erb | 4 +- app/views/search_endpoints/_case.html.erb | 2 +- test/helpers/application_helper_test.rb | 26 +++++++++++++ 13 files changed, 79 insertions(+), 20 deletions(-) diff --git a/app/assets/stylesheets/froggy.css b/app/assets/stylesheets/froggy.css index a29d75fd8..9b42a8c6e 100644 --- a/app/assets/stylesheets/froggy.css +++ b/app/assets/stylesheets/froggy.css @@ -31,7 +31,7 @@ display: inline-block; width: 24px; /* Adjust to match your image width */ height: 24px; /* Adjust to match your image height */ - background-image: url('querqy-icon.png'); + background-image: url('images/querqy-icon.png'); background-size: contain; background-repeat: no-repeat; background-position: center; diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 65bd65045..75a15261b 100755 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,12 +3,20 @@ module ApplicationHelper def book_title book if book.name.downcase.starts_with?('book') - book.name + book.name.capitalize else "Book #{book.name}" end end + def case_title kase + if kase.case_name.downcase.starts_with?('case') + kase.case_name.capitalize + else + "Case #{kase.case_name}" + end + end + def display_judge_name judge judge.nil? ? 'anonymous' : judge.fullname end @@ -98,4 +106,33 @@ def form_with(model: nil, **options, &block) # Call the original `form_with` method with the modified options super end + + # Match the link to the core case url with the endpoint_url + # if we have one. Avoids a swap in the core application. + def link_to_core_case name, kase, try_number, options = {} + # Ensure options[:data] is set to { turbo_prefetch: false } + options[:data] ||= {} + options[:data][:turbo_prefetch] = false + + endpoint_url = kase.tries.first&.search_endpoint&.endpoint_url + protocol = nil + if endpoint_url + protocol = get_protocol_from_url(endpoint_url) + port = 443 if 'https' == protocol + end + path = case_core_url(kase, try_number, protocol: protocol, port: port) + + # Call the original link_to method with the modified options + link_to(name, path, options) + end + + def get_protocol_from_url url + parsed_url = URI.parse(url) + protocol = parsed_url.scheme # This gets the protocol (http, https, etc.) + protocol + rescue URI::InvalidURIError => e + # Handle the error (e.g., log it, return nil, or raise a custom error) + Rails.logger.error("Invalid URL for search endpoint: #{url} - Error: #{e.message}") + nil + end end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb index 695512274..998734aed 100644 --- a/app/helpers/home_helper.rb +++ b/app/helpers/home_helper.rb @@ -47,11 +47,7 @@ def greeting2 # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/MethodLength - def case_title kase - if kase.case_name.downcase.starts_with?('case') - kase.case_name - else - "Case #{kase.case_name}" - end + def strip_case_title kase + kase.case_name.sub(/^case\s+/i, '').capitalize end end diff --git a/app/views/analytics/tries_visualization/show.html.erb b/app/views/analytics/tries_visualization/show.html.erb index 2f6eeab76..8c24cc167 100644 --- a/app/views/analytics/tries_visualization/show.html.erb +++ b/app/views/analytics/tries_visualization/show.html.erb @@ -1,4 +1,4 @@ -<%= link_to 'Return to Case', case_core_path(@case) %> +<%= link_to_core_case 'Return to Case', @case, @case.last_try_number %>