Commit ab66733b3d0249a72d9213cabe6622eb7de989d0

Authored by Luciano Prestes
Committed by Gabriela Navarro
1 parent 8d0bfa0c

Refactor search dynamic text

Signed-off-by: Fabio Teixeira <fabio1079@gmail.com>
Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
features/software_catalog.feature
... ... @@ -37,7 +37,7 @@ Feature: Search software
37 37 @selenium
38 38 Scenario: Show software "One" when searching for "Software One"
39 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 41 And I keyup on selector "#search-input"
42 42 Then I should see "Software One"
43 43 Then I should not see "Software Two"
... ...
lib/ext/community.rb
... ... @@ -2,6 +2,12 @@ require_dependency &#39;community&#39;
2 2  
3 3 class Community
4 4  
  5 + SEARCHABLE_SOFTWARE_FIELDS = {
  6 + :name => 1,
  7 + :identifier => 2,
  8 + :nickname => 3,
  9 + }
  10 +
5 11 attr_accessible :visible
6 12  
7 13 has_one :software_info, :dependent=>:destroy
... ...
lib/ext/search_controller.rb
... ... @@ -26,6 +26,7 @@ class SearchController
26 26 results = results.paginate(:per_page => @per_page, :page => params[:page])
27 27 @searches[@asset] = {:results => results}
28 28 @search = results
  29 + @software_count = filter_software_infos_list.count
29 30 render :layout=>false if request.xhr?
30 31 end
31 32  
... ... @@ -65,7 +66,8 @@ class SearchController
65 66 end
66 67  
67 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 72 category_ids = get_filter_category_ids
71 73  
... ...
lib/software_info.rb
1 1 class SoftwareInfo < ActiveRecord::Base
2 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 28 SEARCH_FILTERS = []
5 29 SEARCH_DISPLAYS = %w[full]
6 30  
... ... @@ -41,7 +65,7 @@ class SoftwareInfo &lt; ActiveRecord::Base
41 65 # used on find_by_contents
42 66 scope :like_search, lambda{ |name|
43 67 joins(:community).where(
44   - "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?",
  68 + "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?",
45 69 "%#{name}%", "%#{name}%", "%#{name}%"
46 70 )
47 71 }
... ...
public/software-catalog.js
... ... @@ -95,20 +95,27 @@
95 95  
96 96  
97 97 function update_search_page_on_ajax(response) {
98   - close_loading();
99 98 response = $(response);
100 99 var search_list = $("#search-results");
101 100 var selected_categories_field = $("#filter-categories-select-catalog");
102 101 var pagination = $("#software-pagination");
  102 + var software_count = $("#software-count");
103 103  
104 104 var result_list = response.find("#search-results").html();
105 105 var result_categories = response.find("#filter-categories-select-catalog").html();
106 106 var result_pagination = response.find("#software-pagination").html();
  107 + var result_software_count = response.find("#software-count").html();
107 108  
108 109 search_list.html(result_list);
109 110 selected_categories_field.html(result_categories);
110 111 pagination.html(result_pagination);
  112 + software_count.html(result_software_count);
111 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 134  
128 135 function update_page_by_text_filter() {
129 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 150 function set_events() {
... ... @@ -142,7 +156,8 @@
142 156 $(".project-software").click(selectProjectSoftwareCheckbox);
143 157 $("#software_display").change(update_page_by_ajax_on_select_change);
144 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 11 <span class="formfield">
12 12 <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50, :placeholder=>_("Type words about the software_info you're looking for") %>
13 13 </span>
  14 +
  15 + <%= submit_button(:search, _('Filter')) %>
14 16 </div>
15 17 <%= render :partial => 'search_form_extra_fields' %>
16 18 <%= render :partial => 'catalog_filter' %>
17 19  
18 20 <!-- #display-options sera substituido pelo html passado pela equipe de design -->
19 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 24 </div>
23 25  
24 26 <div>
... ...