From 25b99b657036a018fca1bf398c0c9b7e16a85671 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 29 Jul 2016 16:32:54 +0200 Subject: [PATCH] fixed search [fixes SCI-261] --- app/controllers/search_controller.rb | 22 ++++++++++++++-------- app/views/search/index.html.erb | 2 +- config/locales/en.yml | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 6e15d288c..979e335ab 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -47,22 +47,28 @@ class SearchController < ApplicationController @search_category = params[:category] || '' @search_category = @search_category.to_sym @search_page = params[:page].to_i || 1 + @display_query = @search_query + # splits the search query to validate all entries + @splited_query = @search_query.split() - error = false - @search_query.split().each do |w| - if w.length < MIN_QUERY_CHARS - error = true + if @splited_query.first.length < MIN_QUERY_CHARS + flash[:error] = t'search.index.error.query_length', n: MIN_QUERY_CHARS + redirect_to :back + else + @splited_query.each_with_index do |w, i| + if @splited_query.length > 1 && w.length < MIN_QUERY_CHARS + @splited_query.slice!(i) + end end end - if error - flash[:error] = t'search.index.error.query_length', n: MIN_QUERY_CHARS - redirect_to :back - end + @search_query = @splited_query.join(' ') + if @search_page < 1 @search_page = 1 end end + # Initialize markdown parser def load_markdown if @search_category == :results diff --git a/app/views/search/index.html.erb b/app/views/search/index.html.erb index 4c2c6e2f3..5574f8c8e 100644 --- a/app/views/search/index.html.erb +++ b/app/views/search/index.html.erb @@ -1,6 +1,6 @@ <% provide(:head_title, t("search.index.head_title")) %> -

<%= t('search.index.results_title_html', query: @search_query) %>

+

<%= t('search.index.results_title_html', query: @display_query) %>

<%= form_tag search_path, method: :get do %> <%= hidden_field_tag :q, @search_query %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 705d77c55..ca0e9fa98 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -119,7 +119,7 @@ en: no_name: "(no name)" error: no_results: "No results for %{q}." - query_length: "Search query is too short. Each word should contain at least %{n} characters." + query_length: "Search query is too short. It should contain at least %{n} characters." max_length: "Search query is too long. The query is limited to %{n} characters." samples: sample: "Sample: "