Commit 3ebea73abd24c00e218f1144c7288ea707f4c76c

Authored by Rodrigo Souto
1 parent b374f2c5

[search-improvements] Register search_term_occurrence on find_by_contents

app/controllers/admin/region_validators_controller.rb
... ... @@ -33,7 +33,7 @@ class RegionValidatorsController < AdminController
33 33 def load_region_and_search
34 34 @region = environment.regions.find(params[:id])
35 35 if params[:search]
36   - @search = find_by_contents(:organizations, Organization, params[:search])[:results].reject {|item| @region.validator_ids.include?(item.id) }
  36 + @search = find_by_contents(:organizations, environment, Organization, params[:search])[:results].reject {|item| @region.validator_ids.include?(item.id) }
37 37 end
38 38 end
39 39  
... ...
app/controllers/admin/users_controller.rb
... ... @@ -18,7 +18,7 @@ class UsersController < AdminController
18 18 end
19 19 scope = scope.order('name ASC')
20 20 @q = params[:q]
21   - @collection = find_by_contents(:people, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results]
  21 + @collection = find_by_contents(:people, environment, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results]
22 22 end
23 23  
24 24 def set_admin_role
... ...
app/controllers/application_controller.rb
... ... @@ -164,11 +164,15 @@ class ApplicationController < ActionController::Base
164 164 end
165 165 end
166 166  
167   - def find_by_contents(asset, scope, query, paginate_options={:page => 1}, options={})
168   - plugins.dispatch_first(:find_by_contents, asset, scope, query, paginate_options, options)
  167 + include SearchTermHelper
  168 +
  169 + def find_by_contents(asset, context, scope, query, paginate_options={:page => 1}, options={})
  170 + search = plugins.dispatch_first(:find_by_contents, asset, scope, query, paginate_options, options)
  171 + register_search_term(query, scope.count, search[:results].count, context, asset)
  172 + search
169 173 end
170 174  
171   - def find_suggestions(asset, query, options={})
172   - plugins.dispatch_first(:find_suggestions, asset, query, options)
  175 + def find_suggestions(query, context, asset, options={})
  176 + plugins.dispatch_first(:find_suggestions, query, context, asset, options)
173 177 end
174 178 end
... ...
app/controllers/my_profile/cms_controller.rb
... ... @@ -306,7 +306,7 @@ class CmsController < MyProfileController
306 306  
307 307 def search
308 308 query = params[:q]
309   - results = find_by_contents(:uploaded_files, profile.files.published, query)[:results]
  309 + results = find_by_contents(:uploaded_files, profile, profile.files.published, query)[:results]
310 310 render :text => article_list_to_json(results), :content_type => 'application/json'
311 311 end
312 312  
... ...
app/controllers/public/profile_search_controller.rb
... ... @@ -11,7 +11,7 @@ class ProfileSearchController < PublicController
11 11 if params[:where] == 'environment'
12 12 redirect_to :controller => 'search', :query => @q
13 13 else
14   - @results = find_by_contents(:articles, profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results]
  14 + @results = find_by_contents(:articles, profile, profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results]
15 15 end
16 16 end
17 17 end
... ...
app/controllers/public/search_controller.rb
... ... @@ -217,7 +217,7 @@ class SearchController < PublicController
217 217 end
218 218  
219 219 def full_text_search
220   - @searches[@asset] = find_by_contents(@asset, @scope, @query, paginate_options, {:category => @category, :filter => @filter})
  220 + @searches[@asset] = find_by_contents(@asset, environment, @scope, @query, paginate_options, {:category => @category, :filter => @filter})
221 221 end
222 222  
223 223 private
... ...
app/helpers/search_term_helper.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +module SearchTermHelper
  2 + def register_search_term(term, total, indexed, context, asset='all')
  3 + normalized_term = normalize_term(term)
  4 + if normalized_term.present?
  5 + search_term = SearchTerm.find_or_create(normalized_term, context, asset)
  6 + SearchTermOccurrence.create!(:search_term => search_term, :total => total, :indexed => indexed)
  7 + end
  8 + end
  9 +
  10 + #FIXME For some reason the job is created but nothing is ran.
  11 + #handle_asynchronously :register_search_term
  12 +
  13 + #TODO Think better on how to normalize them properly
  14 + def normalize_term(search_term)
  15 + search_term
  16 + end
  17 +end
... ...
test/functional/application_controller_test.rb
... ... @@ -565,4 +565,14 @@ class ApplicationControllerTest < ActionController::TestCase
565 565 assert_no_tag :tag => 'meta', :attributes => { :property => 'article:published_time' }
566 566 assert_no_tag :tag => 'meta', :attributes => { :property => 'og:image' }
567 567 end
  568 +
  569 + should 'register search_term occurrence on find_by_contents' do
  570 + controller = ApplicationController.new
  571 + controller.stubs(:environment).returns(Environment.default)
  572 + assert_difference 'SearchTermOccurrence.count', 1 do
  573 + controller.send(:find_by_contents, :people, Environment.default, Person, 'search_term', paginate_options={:page => 1}, options={})
  574 + process_delayed_job_queue
  575 + end
  576 + end
  577 +
568 578 end
... ...