Commit 0e3b436f08f686a1594f08078fb15948c463c68f
Committed by
Gabriela Navarro
1 parent
13a802fa
Exists in
master
and in
5 other branches
Add relevance order by name and finality in software_search
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
2 changed files
with
27 additions
and
1 deletions
Show diff stats
lib/ext/search_controller.rb
| ... | ... | @@ -93,12 +93,37 @@ class SearchController |
| 93 | 93 | |
| 94 | 94 | def sort_communities_list communities_list |
| 95 | 95 | communities_list.sort!{|a, b| a.name <=> b.name} |
| 96 | + | |
| 96 | 97 | if params[:sort] && params[:sort] == "desc" |
| 97 | 98 | communities_list.reverse! |
| 99 | + elsif params[:sort] && params[:sort] == "relevance" | |
| 100 | + communities_list = sort_by_relevance(communities_list, params[:query]){ |community| [community.software_info.finality, community.name] } | |
| 98 | 101 | end |
| 99 | 102 | communities_list |
| 100 | 103 | end |
| 101 | 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 | + | |
| 102 | 127 | def prepare_software_search_page |
| 103 | 128 | prepare_software_infos_params |
| 104 | 129 | prepare_software_infos_message | ... | ... |
views/search/_mpog_search_form.html.erb