Skip to content

Commit

Permalink
ship a first cut of being smarter (#1133)
Browse files Browse the repository at this point in the history
* Leverage the use of the helper to be smart about turbo, and use it everywhere

* Clean up the link creation titles for cases.
  • Loading branch information
epugh authored Dec 11, 2024
1 parent 2f10026 commit 912e455
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/froggy.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 38 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 2 additions & 6 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/analytics/tries_visualization/show.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>

<div id="tries_tree_view"></div>
<script type="text/javascript">
Expand Down
2 changes: 1 addition & 1 deletion app/views/books/_case.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= link_to kase.case_name, case_core_path(kase.id, kase.last_try_number) %>
<%= link_to_core_case case_title(kase), kase, kase.last_try_number %>
6 changes: 3 additions & 3 deletions app/views/home/_case.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tr>
<th scope="row"><%= kase.id %></th>
<th scope="row"><%= kase.case_name %></th>
<th scope="row"><%= strip_case_title(kase) %></th>
<td><%= kase.queries_count %></td>
<td>
<% unless kase.last_score.blank? %>
Expand All @@ -17,6 +17,6 @@
<%= kase.last_score.user.name %>
<% end %>
</td>

<td><%= link_to 'View', case_core_path(kase, kase.last_try_number), data: { turbo_prefetch: false }, class: 'btn btn-sm btn-primary', role: 'button' %></td>
<td><%= link_to_core_case 'View', kase, kase.last_try_number, class: 'btn btn-sm btn-primary', role: 'button' %>
</td>
</tr>
2 changes: 1 addition & 1 deletion app/views/home/_case_summary.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="col-12 col-md-6 mb-4 mb-lg-0 col-lg-3">
<div class="card h-100">
<h5 class="card-header text-truncate">
<%= link_to case_title(kase), case_core_path(kase, kase.last_try_number), data: { turbo_prefetch: false } %>
<%= link_to_core_case case_title(kase), kase, kase.last_try_number %>
</h5>

<div class="card-body">
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Inspired by https://dev.to/themesberg/tutorial-how-to-build-a-simple-admin-dashb
</tbody>
</table>
</div>
<%= link_to 'View all cases', cases_path(), class: 'btn btn-block btn-light', role: 'button' %>
<%= link_to 'View all Cases', cases_path(), class: 'btn btn-block btn-light', role: 'button' %>

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/judgements/_moar_judgements_needed.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<% else %>
Return to the linked Case
<% book.cases.each do |kase| %>
<%= link_to kase.case_name, case_core_path(kase, kase.last_try_number), class: 'alert-link' %>
<%= link_to_core_case case_title(kase), kase, kase.last_try_number, class: 'alert-link' %>
<% end %>
and using the <b><i class="bi bi-book-half"></i> Judgements</b> to populate this Book with query/doc pairs for judging.
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</li>
<li><hr class="dropdown-divider"></li>
<% set_recent_cases.each do |kase| %>
<li><%= link_to kase.case_name, case_core_path(kase, kase.last_try_number) %></li>
<li><%= link_to_core_case kase.case_name, kase, kase.last_try_number %></li>
<li><hr class="dropdown-divider"></li>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions app/views/ratings/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Ratings for Case <%= @case.case_name %></h1>
<h1 class="h2">Ratings for <%= case_title(@case) %></h1>
<div class="btn-toolbar mb-2 mb-md-0">
<div class="btn-group me-2">
<%= link_to "Back to Case #{@case.case_name}", case_core_path(@case), class: "btn btn-sm btn-outline-secondary" %>
<%= link_to_core_case "Back to #{case_title(@case)}", @case, @case.last_try_number, class: 'btn btn-sm btn-outline-secondary' %>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/search_endpoints/_case.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= link_to kase.case_name, case_core_path(kase.id) %>
<%= link_to_core_case kase.case_name, kase, kase.last_try_number %>
26 changes: 26 additions & 0 deletions test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,30 @@ class ApplicationHelperTest < ActionView::TestCase
safe_list_sanitizer.sanitize("Bold</b> no more! <a href='more.html'>See more here</a>...")
assert_equal '<b>Bold</b><i>Trailing italics</i>', safe_list_sanitizer.sanitize('<b>Bold</b><i>Trailing italics')
end

let(:random_case) { cases(:random_case) }
def test_link_to_core_case
try_number = 2
expected_link_text = 'View Case'
expected_path = case_core_url(random_case, try_number)

# Call the helper method
result = link_to_core_case(expected_link_text, random_case, try_number)

# Assertions
assert_includes result, expected_link_text
assert_includes result, expected_path
assert_includes result, "href=\"#{expected_path}\""
end

def test_link_to_core_case_with_options
try_number = 2
options = { class: 'btn btn-primary' }

# Call the helper method
result = link_to_core_case('View Case', random_case, try_number, options)

# Assertions for the options
assert_includes result, 'class="btn btn-primary"'
end
end

0 comments on commit 912e455

Please sign in to comment.