diff --git a/features/software_catalog.feature b/features/software_catalog.feature index 55010d2..4101469 100644 --- a/features/software_catalog.feature +++ b/features/software_catalog.feature @@ -37,7 +37,7 @@ Feature: Search software @selenium Scenario: Show software "One" when searching for "Software One" Given I go to /search/software_infos - And I fill in "search-input" with "Software One" + And I fill in "search-input" with "One" And I keyup on selector "#search-input" Then I should see "Software One" Then I should not see "Software Two" diff --git a/lib/ext/community.rb b/lib/ext/community.rb index 073ce4a..bb89180 100644 --- a/lib/ext/community.rb +++ b/lib/ext/community.rb @@ -2,6 +2,12 @@ require_dependency 'community' class Community + SEARCHABLE_SOFTWARE_FIELDS = { + :name => 1, + :identifier => 2, + :nickname => 3, + } + attr_accessible :visible has_one :software_info, :dependent=>:destroy diff --git a/lib/ext/search_controller.rb b/lib/ext/search_controller.rb index 4ccad2d..4d5134f 100644 --- a/lib/ext/search_controller.rb +++ b/lib/ext/search_controller.rb @@ -26,6 +26,7 @@ class SearchController results = results.paginate(:per_page => @per_page, :page => params[:page]) @searches[@asset] = {:results => results} @search = results + @software_count = filter_software_infos_list.count render :layout=>false if request.xhr? end @@ -65,7 +66,8 @@ class SearchController end def get_filtered_software_list - filtered_software_list = SoftwareInfo.like_search(params[:query]) + params[:query] ||= "" + filtered_software_list = SoftwareInfo.search_by_query(params[:query]) category_ids = get_filter_category_ids diff --git a/lib/software_info.rb b/lib/software_info.rb index 726b7ce..ea451bb 100644 --- a/lib/software_info.rb +++ b/lib/software_info.rb @@ -1,6 +1,30 @@ class SoftwareInfo < ActiveRecord::Base acts_as_having_settings :field => :settings + SEARCHABLE_SOFTWARE_FIELDS = { + :acronym => 1, + :finality => 2, + } + + scope :search_by_query, lambda {|query = ""| + filtered_query = query.gsub(/[\|\(\)\\\/\s\[\]'"*%&!:]/,' ').split.map{|w| w += ":*"}.join('|') + search_fields = SoftwareInfo.pg_search_plugin_fields + + if query.empty? + SoftwareInfo.all + else + joins(:community).where("to_tsvector('simple', #{search_fields}) @@ to_tsquery('#{filtered_query}')") + end + } + + def self.pg_search_plugin_fields + searchable_fields = Community::SEARCHABLE_SOFTWARE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(#{Community.table_name}.#{f}, '')"}.join(" || ' ' || ") + searchable_fields += " || ' ' || " + searchable_fields += self::SEARCHABLE_SOFTWARE_FIELDS.keys.map(&:to_s).sort.map {|f| "coalesce(#{table_name}.#{f}, '')"}.join(" || ' ' || ") + + searchable_fields + end + SEARCH_FILTERS = [] SEARCH_DISPLAYS = %w[full] @@ -41,7 +65,7 @@ class SoftwareInfo < ActiveRecord::Base # used on find_by_contents scope :like_search, lambda{ |name| joins(:community).where( - "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?", + "name ILIKE ? OR acronym ILIKE ? OR finality ILIKE ?", "%#{name}%", "%#{name}%", "%#{name}%" ) } diff --git a/public/software-catalog.js b/public/software-catalog.js index 2a27ca8..22d4e3d 100644 --- a/public/software-catalog.js +++ b/public/software-catalog.js @@ -95,20 +95,27 @@ function update_search_page_on_ajax(response) { - close_loading(); response = $(response); var search_list = $("#search-results"); var selected_categories_field = $("#filter-categories-select-catalog"); var pagination = $("#software-pagination"); + var software_count = $("#software-count"); var result_list = response.find("#search-results").html(); var result_categories = response.find("#filter-categories-select-catalog").html(); var result_pagination = response.find("#software-pagination").html(); + var result_software_count = response.find("#software-count").html(); search_list.html(result_list); selected_categories_field.html(result_categories); pagination.html(result_pagination); + software_count.html(result_software_count); show_head_message(); + + setTimeout(function(){ + console.log("fgdjfgdsh"); + close_loading(); + }, 1000); } @@ -127,10 +134,17 @@ function update_page_by_text_filter() { var text = this.value; + dispatch_search_ajax(update_search_page_on_ajax, false); + } - if (text.length >= 3) { - dispatch_search_ajax(update_search_page_on_ajax, false); - } + function search_input_keyup() { + var timer = null; + + $("#search-input").keyup(function() { + timer = setTimeout(update_page_by_text_filter, 400); + }).keydown(function() { + clearTimeout(timer); + }); } function set_events() { @@ -142,7 +156,8 @@ $(".project-software").click(selectProjectSoftwareCheckbox); $("#software_display").change(update_page_by_ajax_on_select_change); $("#sort").change(update_page_by_ajax_on_select_change); - $("#search-input").keyup(update_page_by_text_filter); + + search_input_keyup(); } diff --git a/views/search/_mpog_search_form.html.erb b/views/search/_mpog_search_form.html.erb index 18a7158..a2d1d60 100644 --- a/views/search/_mpog_search_form.html.erb +++ b/views/search/_mpog_search_form.html.erb @@ -11,14 +11,16 @@ <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50, :placeholder=>_("Type words about the software_info you're looking for") %> + + <%= submit_button(:search, _('Filter')) %> <%= render :partial => 'search_form_extra_fields' %> <%= render :partial => 'catalog_filter' %>
-
- <%= SoftwareInfo.count %> Softwares +
+ <%= "#{@software_count} Software(s)" %>
-- libgit2 0.21.2