diff --git a/Gemfile.lock b/Gemfile.lock index fd9777c..0da190e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -517,7 +517,7 @@ GEM redis (~> 3.0, >= 3.0.4) responders (2.1.0) railties (>= 4.2.0, < 5) - riddle (2.1.0) + riddle (2.4.0) role_block_haml (0.2.4) haml (~> 4.0) route_downcaser (1.1.4) @@ -674,4 +674,4 @@ DEPENDENCIES whenever BUNDLED WITH - 1.16.1 + 1.16.6 diff --git a/app/assets/javascripts/_open_cook/search_line.js.coffee b/app/assets/javascripts/_open_cook/search_line.js.coffee index 6f78911..ea64da6 100644 --- a/app/assets/javascripts/_open_cook/search_line.js.coffee +++ b/app/assets/javascripts/_open_cook/search_line.js.coffee @@ -22,5 +22,5 @@ form[0].submit() false - $('@search_line').autocomplete - source: @source + $('@search_line').autocomplete(source: @source).data('ui-autocomplete')._renderItem = (ul, item) -> + $('
  • ').data('item.autocomplete', item).append('' + item.label).appendTo(ul) \ No newline at end of file diff --git a/app/assets/stylesheets/_open_cook/search_line.css.scss b/app/assets/stylesheets/_open_cook/search_line.css.scss index 4c4a220..fa71906 100644 --- a/app/assets/stylesheets/_open_cook/search_line.css.scss +++ b/app/assets/stylesheets/_open_cook/search_line.css.scss @@ -13,7 +13,13 @@ border-bottom: 1px solid #d3d3d3 !important; } -.ui-menu-item{ +.ui-menu .ui-menu-item img { + margin-right: 10px; + border-radius: 10px; +} + +.ui-menu-item { + font-size: 1.199em; background: #fdfff5; } diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index d647751..5c437dd 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,18 +1,16 @@ +# frozen_string_literal: true + class SearchController < ApplicationController before_action :define_hub_ids - def pub_types - [ Recipe, Post, Blog, Page, Interview ] - end - def autocomplete - @squery = params[:term].to_s.strip - to_search = Riddle::Query.escape @squery + @squery = params[:term].to_s.strip + riddle_query = Riddle::Query.escape @squery - res = ThinkingSphinx.search( - to_search, + search_result = ThinkingSphinx.search( + riddle_query, star: true, - classes: pub_types, + classes: SearchService::PUBLICATION_TYPES, field_weights: { title: 10, content: 5 @@ -20,17 +18,21 @@ def autocomplete per_page: 10 ) - render json: res.map(&:title) + attributes = search_result.map do |resource| + { + label: resource.title, + image: resource.main_image(:base) + } + end + + render json: attributes end def search - @squery = params[:squery].to_s.strip - to_search = Riddle::Query.escape @squery - @pubs = if @hub_ids.blank? - ThinkingSphinx.search(to_search, star: true, classes: pub_types, sql: { include: :hub }).pagination(params) - else - ThinkingSphinx.search(to_search, star: true, classes: pub_types, sql: { include: :hub }, with: { hub_id: @hub_ids }).pagination(params) + ThinkingSphinx.search(to_search, star: true, classes: pub_types, sql: { include: :hub }).pagination(params) + else + ThinkingSphinx.search(to_search, star: true, classes: pub_types, sql: { include: :hub }, with: { hub_id: @hub_ids }).pagination(params) end @search_results_size = @pubs.size @@ -39,6 +41,12 @@ def search render layout: 'ok2/layouts/application', template: 'ok2/search/index' end + def multiple_keyboard_layouts? + @multiple_keyboard_layouts + end + + helper_method :multiple_keyboard_layouts? + private def define_hub_ids @@ -54,5 +62,4 @@ def define_hub_ids @hub_ids << post_hub.self_and_descendants.select(:id).map(&:id).uniq end end - end diff --git a/app/services/search_service.rb b/app/services/search_service.rb new file mode 100644 index 0000000..da55a1c --- /dev/null +++ b/app/services/search_service.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module SearchService + PUBLICATION_TYPES = [ + Recipe, Post, Blog, Page, Interview + ].freeze + + LOWERCASE_DICTIONARY = { + 'q' => 'й', 'w' => 'ц', 'e' => 'у', 'r' => 'к', + 't' => 'е', 'y' => 'н', 'u' => 'г', 'i' => 'ш', + 'o' => 'щ', 'p' => 'з', 'a' => 'ф', 's' => 'ы', + 'd' => 'в', 'f' => 'а', 'g' => 'п', 'h' => 'р', + 'j' => 'о', 'k' => 'л', 'l' => 'д', ';' => 'ж', + "'" => 'э', 'z' => 'я', 'x' => 'ч', 'c' => 'с', + 'v' => 'м', 'b' => 'и', 'n' => 'т', 'm' => 'ь', + ',' => 'б', '.' => 'ю' + }.freeze + + UPPERCASE_DICTIONARY = { + 'Q' => 'Й', 'W' => 'Ц', 'E' => 'У', 'R' => 'К', + 'T' => 'Е', 'Y' => 'Н', 'U' => 'Г', 'I' => 'Ш', + 'O' => 'Щ', 'P' => 'З', 'A' => 'Ф', 'S' => 'Ы', + 'D' => 'В', 'F' => 'А', 'G' => 'П', 'H' => 'Р', + 'J' => 'О', 'K' => 'Л', 'L' => 'Д', ':' => 'Ж', + '"' => 'Э', 'Z' => 'Я', 'X' => 'Ч', 'С' => 'С', + 'V' => 'М', 'B' => 'И', 'N' => 'Т', 'M' => 'Ь', + '<' => 'Б', '>' => 'Ю' + }.freeze + + def call(query, hub_ids, params) + query = query.to_s.strip + riddle_query = Riddle::Query.escape(query) + + search_result = if hub_ids.blank? + ThinkingSphinx.search(riddle_query, star: true, classes: PUBLICATION_TYPES, sql: { include: :hub }).pagination(params) + else + ThinkingSphinx.search(riddle_query, star: true, classes: PUBLICATION_TYPES, sql: { include: :hub }, with: { hub_id: hub_ids }).pagination(params) + end + + if search_result.blank? + OpenStruct.new(search_result: search_by_multiple_keyboard_layouts(query, params), multiple_keyboard_layouts: true) + else + OpenStruct.new(search_result: search_result, multiple_keyboard_layouts: false) + end + end + + module_function :call + + def convert_to_another_keyboard_layout(query) + search_query = [] + + query.each_char do |char| + search_query.push(dictionary[char]) + end + + search_query.join + end + + module_function :convert_to_another_keyboard_layout + + private + + def search_by_multiple_keyboard_layouts(query, params) + ThinkingSphinx.search(Riddle::Query.escape(convert_to_another_keyboard_layout(query)), star: true, classes: PUBLICATION_TYPES, sql: { include: :hub }).pagination(params) + end + + module_function :search_by_multiple_keyboard_layouts + + def dictionary + LOWERCASE_DICTIONARY.merge(UPPERCASE_DICTIONARY) + end + + module_function :dictionary +end diff --git a/app/views/ok2/search/index.html.slim b/app/views/ok2/search/index.html.slim index 76fb6f9..96cf834 100644 --- a/app/views/ok2/search/index.html.slim +++ b/app/views/ok2/search/index.html.slim @@ -8,7 +8,10 @@ h1.tac.fs30.fwn.i.pt20.pb15.ffg(style='color:#024e9f') | Поиск: ' - = @squery + - if multiple_keyboard_layouts? && @pubs.present? + = SearchService.convert_to_another_keyboard_layout(params[:squery].to_s.strip) + - else + = params[:squery] -# MAIN CONTENT - if @pubs.blank?