Commit ab66733b3d0249a72d9213cabe6622eb7de989d0
Committed by
Gabriela Navarro
1 parent
8d0bfa0c
Exists in
master
and in
5 other branches
Refactor search dynamic text
Signed-off-by: Fabio Teixeira <fabio1079@gmail.com> Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
6 changed files
with
59 additions
and
10 deletions
Show diff stats
features/software_catalog.feature
@@ -37,7 +37,7 @@ Feature: Search software | @@ -37,7 +37,7 @@ Feature: Search software | ||
37 | @selenium | 37 | @selenium |
38 | Scenario: Show software "One" when searching for "Software One" | 38 | Scenario: Show software "One" when searching for "Software One" |
39 | Given I go to /search/software_infos | 39 | Given I go to /search/software_infos |
40 | - And I fill in "search-input" with "Software One" | 40 | + And I fill in "search-input" with "One" |
41 | And I keyup on selector "#search-input" | 41 | And I keyup on selector "#search-input" |
42 | Then I should see "Software One" | 42 | Then I should see "Software One" |
43 | Then I should not see "Software Two" | 43 | Then I should not see "Software Two" |
lib/ext/community.rb
@@ -2,6 +2,12 @@ require_dependency 'community' | @@ -2,6 +2,12 @@ require_dependency 'community' | ||
2 | 2 | ||
3 | class Community | 3 | class Community |
4 | 4 | ||
5 | + SEARCHABLE_SOFTWARE_FIELDS = { | ||
6 | + :name => 1, | ||
7 | + :identifier => 2, | ||
8 | + :nickname => 3, | ||
9 | + } | ||
10 | + | ||
5 | attr_accessible :visible | 11 | attr_accessible :visible |
6 | 12 | ||
7 | has_one :software_info, :dependent=>:destroy | 13 | has_one :software_info, :dependent=>:destroy |
lib/ext/search_controller.rb
@@ -26,6 +26,7 @@ class SearchController | @@ -26,6 +26,7 @@ class SearchController | ||
26 | results = results.paginate(:per_page => @per_page, :page => params[:page]) | 26 | results = results.paginate(:per_page => @per_page, :page => params[:page]) |
27 | @searches[@asset] = {:results => results} | 27 | @searches[@asset] = {:results => results} |
28 | @search = results | 28 | @search = results |
29 | + @software_count = filter_software_infos_list.count | ||
29 | render :layout=>false if request.xhr? | 30 | render :layout=>false if request.xhr? |
30 | end | 31 | end |
31 | 32 | ||
@@ -65,7 +66,8 @@ class SearchController | @@ -65,7 +66,8 @@ class SearchController | ||
65 | end | 66 | end |
66 | 67 | ||
67 | def get_filtered_software_list | 68 | def get_filtered_software_list |
68 | - filtered_software_list = SoftwareInfo.like_search(params[:query]) | 69 | + params[:query] ||= "" |
70 | + filtered_software_list = SoftwareInfo.search_by_query(params[:query]) | ||
69 | 71 | ||
70 | category_ids = get_filter_category_ids | 72 | category_ids = get_filter_category_ids |
71 | 73 |
lib/software_info.rb
1 | class SoftwareInfo < ActiveRecord::Base | 1 | class SoftwareInfo < ActiveRecord::Base |
2 | acts_as_having_settings :field => :settings | 2 | acts_as_having_settings :field => :settings |
3 | 3 | ||
4 | + SEARCHABLE_SOFTWARE_FIELDS = { | ||
5 | + :acronym => 1, | ||
6 | + :finality => 2, | ||
7 | + } | ||
8 | + | ||
9 | + scope :search_by_query, lambda {|query = ""| | ||
10 | + filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') | ||
11 | + search_fields = SoftwareInfo.pg_search_plugin_fields | ||
12 | + | ||
13 | + if query.empty? | ||
14 | + SoftwareInfo.all | ||
15 | + else | ||
16 | + joins(:community).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')") | ||
17 | + end | ||
18 | + } | ||
19 | + | ||
20 | + def self.pg_search_plugin_fields | ||
21 | + searchable_fields = Community::SEARCHABLE_SOFTWARE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(#{Community.table_name}.#{f}, '')"}.join(" || ' ' || ") | ||
22 | + searchable_fields += " || ' ' || " | ||
23 | + searchable_fields += self::SEARCHABLE_SOFTWARE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(#{table_name}.#{f}, '')"}.join(" || ' ' || ") | ||
24 | + | ||
25 | + searchable_fields | ||
26 | + end | ||
27 | + | ||
4 | SEARCH_FILTERS = [] | 28 | SEARCH_FILTERS = [] |
5 | SEARCH_DISPLAYS = %w[full] | 29 | SEARCH_DISPLAYS = %w[full] |
6 | 30 | ||
@@ -41,7 +65,7 @@ class SoftwareInfo < ActiveRecord::Base | @@ -41,7 +65,7 @@ class SoftwareInfo < ActiveRecord::Base | ||
41 | # used on find_by_contents | 65 | # used on find_by_contents |
42 | scope :like_search, lambda{ |name| | 66 | scope :like_search, lambda{ |name| |
43 | joins(:community).where( | 67 | joins(:community).where( |
44 | - "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?", | 68 | + "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?", |
45 | "%#{name}%", "%#{name}%", "%#{name}%" | 69 | "%#{name}%", "%#{name}%", "%#{name}%" |
46 | ) | 70 | ) |
47 | } | 71 | } |
public/software-catalog.js
@@ -95,20 +95,27 @@ | @@ -95,20 +95,27 @@ | ||
95 | 95 | ||
96 | 96 | ||
97 | function update_search_page_on_ajax(response) { | 97 | function update_search_page_on_ajax(response) { |
98 | - close_loading(); | ||
99 | response = $(response); | 98 | response = $(response); |
100 | var search_list = $("#search-results"); | 99 | var search_list = $("#search-results"); |
101 | var selected_categories_field = $("#filter-categories-select-catalog"); | 100 | var selected_categories_field = $("#filter-categories-select-catalog"); |
102 | var pagination = $("#software-pagination"); | 101 | var pagination = $("#software-pagination"); |
102 | + var software_count = $("#software-count"); | ||
103 | 103 | ||
104 | var result_list = response.find("#search-results").html(); | 104 | var result_list = response.find("#search-results").html(); |
105 | var result_categories = response.find("#filter-categories-select-catalog").html(); | 105 | var result_categories = response.find("#filter-categories-select-catalog").html(); |
106 | var result_pagination = response.find("#software-pagination").html(); | 106 | var result_pagination = response.find("#software-pagination").html(); |
107 | + var result_software_count = response.find("#software-count").html(); | ||
107 | 108 | ||
108 | search_list.html(result_list); | 109 | search_list.html(result_list); |
109 | selected_categories_field.html(result_categories); | 110 | selected_categories_field.html(result_categories); |
110 | pagination.html(result_pagination); | 111 | pagination.html(result_pagination); |
112 | + software_count.html(result_software_count); | ||
111 | show_head_message(); | 113 | show_head_message(); |
114 | + | ||
115 | + setTimeout(function(){ | ||
116 | + console.log("fgdjfgdsh"); | ||
117 | + close_loading(); | ||
118 | + }, 1000); | ||
112 | } | 119 | } |
113 | 120 | ||
114 | 121 | ||
@@ -127,10 +134,17 @@ | @@ -127,10 +134,17 @@ | ||
127 | 134 | ||
128 | function update_page_by_text_filter() { | 135 | function update_page_by_text_filter() { |
129 | var text = this.value; | 136 | var text = this.value; |
137 | + dispatch_search_ajax(update_search_page_on_ajax, false); | ||
138 | + } | ||
130 | 139 | ||
131 | - if (text.length >= 3) { | ||
132 | - dispatch_search_ajax(update_search_page_on_ajax, false); | ||
133 | - } | 140 | + function search_input_keyup() { |
141 | + var timer = null; | ||
142 | + | ||
143 | + $("#search-input").keyup(function() { | ||
144 | + timer = setTimeout(update_page_by_text_filter, 400); | ||
145 | + }).keydown(function() { | ||
146 | + clearTimeout(timer); | ||
147 | + }); | ||
134 | } | 148 | } |
135 | 149 | ||
136 | function set_events() { | 150 | function set_events() { |
@@ -142,7 +156,8 @@ | @@ -142,7 +156,8 @@ | ||
142 | $(".project-software").click(selectProjectSoftwareCheckbox); | 156 | $(".project-software").click(selectProjectSoftwareCheckbox); |
143 | $("#software_display").change(update_page_by_ajax_on_select_change); | 157 | $("#software_display").change(update_page_by_ajax_on_select_change); |
144 | $("#sort").change(update_page_by_ajax_on_select_change); | 158 | $("#sort").change(update_page_by_ajax_on_select_change); |
145 | - $("#search-input").keyup(update_page_by_text_filter); | 159 | + |
160 | + search_input_keyup(); | ||
146 | } | 161 | } |
147 | 162 | ||
148 | 163 |
views/search/_mpog_search_form.html.erb
@@ -11,14 +11,16 @@ | @@ -11,14 +11,16 @@ | ||
11 | <span class="formfield"> | 11 | <span class="formfield"> |
12 | <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50, :placeholder=>_("Type words about the software_info you're looking for") %> | 12 | <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50, :placeholder=>_("Type words about the software_info you're looking for") %> |
13 | </span> | 13 | </span> |
14 | + | ||
15 | + <%= submit_button(:search, _('Filter')) %> | ||
14 | </div> | 16 | </div> |
15 | <%= render :partial => 'search_form_extra_fields' %> | 17 | <%= render :partial => 'search_form_extra_fields' %> |
16 | <%= render :partial => 'catalog_filter' %> | 18 | <%= render :partial => 'catalog_filter' %> |
17 | 19 | ||
18 | <!-- #display-options sera substituido pelo html passado pela equipe de design --> | 20 | <!-- #display-options sera substituido pelo html passado pela equipe de design --> |
19 | <div id="display-options" class=""> | 21 | <div id="display-options" class=""> |
20 | - <div> | ||
21 | - <strong><%= SoftwareInfo.count %> Softwares</strong> | 22 | + <div id="software-count"> |
23 | + <strong><%= "#{@software_count} Software(s)" %> </strong> | ||
22 | </div> | 24 | </div> |
23 | 25 | ||
24 | <div> | 26 | <div> |