diff --git a/app/controllers/admin/region_validators_controller.rb b/app/controllers/admin/region_validators_controller.rb index 801e03b..c7e9a19 100644 --- a/app/controllers/admin/region_validators_controller.rb +++ b/app/controllers/admin/region_validators_controller.rb @@ -33,7 +33,7 @@ class RegionValidatorsController < AdminController def load_region_and_search @region = environment.regions.find(params[:id]) if params[:search] - @search = find_by_contents(:organizations, Organization, params[:search])[:results].reject {|item| @region.validator_ids.include?(item.id) } + @search = find_by_contents(:organizations, environment, Organization, params[:search])[:results].reject {|item| @region.validator_ids.include?(item.id) } end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 6e8eace..b827994 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -18,7 +18,7 @@ class UsersController < AdminController end scope = scope.order('name ASC') @q = params[:q] - @collection = find_by_contents(:people, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results] + @collection = find_by_contents(:people, environment, scope, @q, {:per_page => per_page, :page => params[:npage]})[:results] end def set_admin_role diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4f36e96..42229c3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -164,11 +164,15 @@ class ApplicationController < ActionController::Base end end - def find_by_contents(asset, scope, query, paginate_options={:page => 1}, options={}) - plugins.dispatch_first(:find_by_contents, asset, scope, query, paginate_options, options) + include SearchTermHelper + + def find_by_contents(asset, context, scope, query, paginate_options={:page => 1}, options={}) + search = plugins.dispatch_first(:find_by_contents, asset, scope, query, paginate_options, options) + register_search_term(query, scope.count, search[:results].count, context, asset) + search end - def find_suggestions(asset, query, options={}) - plugins.dispatch_first(:find_suggestions, asset, query, options) + def find_suggestions(query, context, asset, options={}) + plugins.dispatch_first(:find_suggestions, query, context, asset, options) end end diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 557bd6b..dcc7c65 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -306,7 +306,7 @@ class CmsController < MyProfileController def search query = params[:q] - results = find_by_contents(:uploaded_files, profile.files.published, query)[:results] + results = find_by_contents(:uploaded_files, profile, profile.files.published, query)[:results] render :text => article_list_to_json(results), :content_type => 'application/json' end diff --git a/app/controllers/public/profile_search_controller.rb b/app/controllers/public/profile_search_controller.rb index 39515e0..32b7fec 100644 --- a/app/controllers/public/profile_search_controller.rb +++ b/app/controllers/public/profile_search_controller.rb @@ -11,7 +11,7 @@ class ProfileSearchController < PublicController if params[:where] == 'environment' redirect_to :controller => 'search', :query => @q else - @results = find_by_contents(:articles, profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results] + @results = find_by_contents(:articles, profile, profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results] end end end diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index c4ab956..8ddec6f 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -217,7 +217,7 @@ class SearchController < PublicController end def full_text_search - @searches[@asset] = find_by_contents(@asset, @scope, @query, paginate_options, {:category => @category, :filter => @filter}) + @searches[@asset] = find_by_contents(@asset, environment, @scope, @query, paginate_options, {:category => @category, :filter => @filter}) end private diff --git a/app/helpers/search_term_helper.rb b/app/helpers/search_term_helper.rb new file mode 100644 index 0000000..10bfa50 --- /dev/null +++ b/app/helpers/search_term_helper.rb @@ -0,0 +1,17 @@ +module SearchTermHelper + def register_search_term(term, total, indexed, context, asset='all') + normalized_term = normalize_term(term) + if normalized_term.present? + search_term = SearchTerm.find_or_create(normalized_term, context, asset) + SearchTermOccurrence.create!(:search_term => search_term, :total => total, :indexed => indexed) + end + end + + #FIXME For some reason the job is created but nothing is ran. + #handle_asynchronously :register_search_term + + #TODO Think better on how to normalize them properly + def normalize_term(search_term) + search_term + end +end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 1affcd3..ed67862 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -565,4 +565,14 @@ class ApplicationControllerTest < ActionController::TestCase assert_no_tag :tag => 'meta', :attributes => { :property => 'article:published_time' } assert_no_tag :tag => 'meta', :attributes => { :property => 'og:image' } end + + should 'register search_term occurrence on find_by_contents' do + controller = ApplicationController.new + controller.stubs(:environment).returns(Environment.default) + assert_difference 'SearchTermOccurrence.count', 1 do + controller.send(:find_by_contents, :people, Environment.default, Person, 'search_term', paginate_options={:page => 1}, options={}) + process_delayed_job_queue + end + end + end -- libgit2 0.21.2