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,12 +93,37 @@ class SearchController | ||
93 | 93 | ||
94 | def sort_communities_list communities_list | 94 | def sort_communities_list communities_list |
95 | communities_list.sort!{|a, b| a.name <=> b.name} | 95 | communities_list.sort!{|a, b| a.name <=> b.name} |
96 | + | ||
96 | if params[:sort] && params[:sort] == "desc" | 97 | if params[:sort] && params[:sort] == "desc" |
97 | communities_list.reverse! | 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 | end | 101 | end |
99 | communities_list | 102 | communities_list |
100 | end | 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 | def prepare_software_search_page | 127 | def prepare_software_search_page |
103 | prepare_software_infos_params | 128 | prepare_software_infos_params |
104 | prepare_software_infos_message | 129 | prepare_software_infos_message |
views/search/_mpog_search_form.html.erb
@@ -37,7 +37,8 @@ | @@ -37,7 +37,8 @@ | ||
37 | options_for_select( | 37 | options_for_select( |
38 | [ | 38 | [ |
39 | [_("Name A-Z"), 'asc'], | 39 | [_("Name A-Z"), 'asc'], |
40 | - [_("Name Z-A"), 'desc'] | 40 | + [_("Name Z-A"), 'desc'], |
41 | + [_("Relevance"), 'relevance'] | ||
41 | ], :selected=>params[:sort]) | 42 | ], :selected=>params[:sort]) |
42 | ) %> | 43 | ) %> |
43 | </div> | 44 | </div> |