Commit 74295e2ef3df74d9f7b33e6461c6372121537c8b
Committed by
Gabriela Navarro
1 parent
0e3b436f
Exists in
master
and in
5 other branches
Refactor sort_by_relevance and move to search_helper
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
2 changed files
with
28 additions
and
22 deletions
Show diff stats
lib/ext/search_controller.rb
| ... | ... | @@ -102,28 +102,6 @@ class SearchController |
| 102 | 102 | communities_list |
| 103 | 103 | end |
| 104 | 104 | |
| 105 | - def sort_by_relevance list, text | |
| 106 | - queries = text.split | |
| 107 | - | |
| 108 | - list.sort! do |a, b| | |
| 109 | - found_in_a, found_in_b = 1, 1 | |
| 110 | - | |
| 111 | - relevance_list_a = yield(a) | |
| 112 | - relevance_list_b = yield(b) | |
| 113 | - | |
| 114 | - queries.each do |q| | |
| 115 | - relevance_list_a.count.times do |i| | |
| 116 | - found_in_a = (i * -1) if relevance_list_a[i].downcase.include?(q.downcase) | |
| 117 | - found_in_b = (i * -1) if relevance_list_b[i].downcase.include?(q.downcase) | |
| 118 | - end | |
| 119 | - end | |
| 120 | - | |
| 121 | - found_in_a <=> found_in_b | |
| 122 | - end | |
| 123 | - | |
| 124 | - list | |
| 125 | - end | |
| 126 | - | |
| 127 | 105 | def prepare_software_search_page |
| 128 | 106 | prepare_software_infos_params |
| 129 | 107 | prepare_software_infos_message | ... | ... |
lib/ext/search_helper.rb
| ... | ... | @@ -6,4 +6,32 @@ module SearchHelper |
| 6 | 6 | COMMON_PROFILE_LIST_BLOCK << :software_infos |
| 7 | 7 | COMMON_PROFILE_LIST_BLOCK << :institutions |
| 8 | 8 | |
| 9 | + def sort_by_relevance list, text | |
| 10 | + text_splited = text.split | |
| 11 | + | |
| 12 | + relevance_map = {} | |
| 13 | + list.each do |element| | |
| 14 | + relevance_map[element] = yield(element) | |
| 15 | + end | |
| 16 | + | |
| 17 | + list.sort! do |a, b| | |
| 18 | + found_in_a, found_in_b = 1, 1 | |
| 19 | + | |
| 20 | + relevance_list_a = relevance_map[a] | |
| 21 | + relevance_list_b = relevance_map[b] | |
| 22 | + | |
| 23 | + text_splited.each do |q| | |
| 24 | + relevance_list_a.count.times do |i| | |
| 25 | + relevance = i * -1 | |
| 26 | + found_in_a = relevance if relevance_list_a[i].downcase.include?(q.downcase) | |
| 27 | + found_in_b = relevance if relevance_list_b[i].downcase.include?(q.downcase) | |
| 28 | + end | |
| 29 | + end | |
| 30 | + | |
| 31 | + found_in_a <=> found_in_b | |
| 32 | + end | |
| 33 | + | |
| 34 | + list | |
| 35 | + end | |
| 36 | + | |
| 9 | 37 | end | ... | ... |