From a8fc2ba5086db946c3227a72a97a7796dbab39fc Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 31 Oct 2019 02:08:51 -0500 Subject: [PATCH] wiki pages: fix redirects to exact matches during search. Change wiki page search to redirect to exact matches only when using the quick search bar. Fixes searches sometimes unexpectedly redirecting when doing a regular (non-quick) search that happens to return a single result. Also remove the logic that tries to expand the search when no results are found. This will eventually be replaced with a smarter "did you mean?" search. --- app/controllers/wiki_pages_controller.rb | 15 +++++---------- app/helpers/application_helper.rb | 3 ++- app/views/layouts/_main_links.html.erb | 2 +- test/functional/wiki_pages_controller_test.rb | 6 ++++-- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/controllers/wiki_pages_controller.rb b/app/controllers/wiki_pages_controller.rb index 5530e2a4b..2c5a29c54 100644 --- a/app/controllers/wiki_pages_controller.rb +++ b/app/controllers/wiki_pages_controller.rb @@ -17,16 +17,11 @@ class WikiPagesController < ApplicationController def index @wiki_pages = WikiPage.paginated_search(params) - respond_with(@wiki_pages) do |format| - format.html do - if params[:page].nil? || params[:page].to_i == 1 - if @wiki_pages.length == 1 - redirect_to(wiki_page_path(@wiki_pages.first)) - elsif @wiki_pages.length == 0 && params[:search][:title].present? && params[:search][:title] !~ /\*/ - redirect_to(wiki_pages_path(:search => {:title => "*#{params[:search][:title]}*"})) - end - end - end + + if params[:redirect].to_s.truthy? && @wiki_pages.one? + redirect_to @wiki_pages.first + else + respond_with(@wiki_pages) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index a866fd4c6..e19ce5f1b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -164,7 +164,8 @@ module ApplicationHelper def quick_search_form_for(attribute, url, name, autocomplete: nil, &block) tag.li do search_form_for(url, classes: "quick-search-form one-line-form") do |f| - f.input attribute, label: false, placeholder: "Search #{name}", input_html: { id: nil, "data-autocomplete": autocomplete } + out = tag.input type: :hidden, name: :redirect, value: "1" + out += f.input attribute, label: false, placeholder: "Search #{name}", input_html: { id: nil, "data-autocomplete": autocomplete } end end end diff --git a/app/views/layouts/_main_links.html.erb b/app/views/layouts/_main_links.html.erb index 2cde12760..09de054ed 100644 --- a/app/views/layouts/_main_links.html.erb +++ b/app/views/layouts/_main_links.html.erb @@ -14,7 +14,7 @@ <%= nav_link_to("Implications", tag_implications_path) %> <% end %> <%= nav_link_to("Pools", gallery_pools_path) %> - <%= nav_link_to("Wiki", wiki_pages_path(:title => "help:home")) %> + <%= nav_link_to("Wiki", wiki_page_path("help:home")) %> <%= nav_link_to("Forum", forum_topics_path, :class => (CurrentUser.has_forum_been_updated? ? "forum-updated" : nil)) %> <% if CurrentUser.is_moderator? %> <%= nav_link_to("Dashboard", moderator_dashboard_path) %> diff --git a/test/functional/wiki_pages_controller_test.rb b/test/functional/wiki_pages_controller_test.rb index c93698590..b15310311 100644 --- a/test/functional/wiki_pages_controller_test.rb +++ b/test/functional/wiki_pages_controller_test.rb @@ -22,12 +22,14 @@ class WikiPagesControllerTest < ActionDispatch::IntegrationTest should "list all wiki_pages (with search)" do get wiki_pages_path, params: {:search => {:title => "abc"}} - assert_redirected_to(wiki_page_path(@wiki_page_abc)) + assert_response :success + assert_select "tr td:first-child", text: "abc" end should "list wiki_pages without tags with order=post_count" do get wiki_pages_path, params: {:search => {:title => "abc", :order => "post_count"}} - assert_redirected_to(wiki_page_path(@wiki_page_abc)) + assert_response :success + assert_select "tr td:first-child", text: "abc" end end