From 35733cf407089a2cb4f5b781d4d7de67df057776 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 28 Feb 2013 19:01:23 +0000 Subject: [PATCH] Fallback find_by_contens in case there is no search plugin --- app/controllers/application_controller.rb | 20 +++++++++++++++++--- app/controllers/public/search_controller.rb | 51 +++++++++++++-------------------------------------- 2 files changed, 30 insertions(+), 41 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 35df565..f5b39d3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -154,9 +154,23 @@ class ApplicationController < ActionController::Base end end - def find_by_contents(klass, query, paginate_options={}, options={}) - @plugins.first(:find_by_contents, klass, query, paginate_options, options)# || - # Failback search using like + def find_by_contents(scope, query, paginate_options={}, options={}) + if query.blank? + scope = scope.send(options[:filter]) if options[:filter] + return {:results => scope.paginate(paginate_options)} + end + @plugins.first(:find_by_contents, scope, query, paginate_options, options) || + fallback_find_by_contents(scope, query, paginate_options, options) + end + + private + + def fallback_find_by_contents(scope, query, paginate_options, options) + fields = scope.base_class::SEARCHABLE_FIELDS.keys.map(&:to_s) & scope.base_class.column_names + conditions = fields.map do |field| + "#{scope.base_class.table_name}.#{field} LIKE \"%#{query.downcase}%\"" + end.join(' OR ') + {:results => scope.send(options[:filter]).where(conditions).paginate(paginate_options)} end end diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index b0a0e0f..ca3ad8e 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -57,12 +57,8 @@ class SearchController < PublicController end def articles - if !@empty_query - full_text_search - else - @searches[@asset] = {} - @searches[@asset][:results] = @environment.articles.public.send(@filter).paginate(paginate_options) - end + @scope = @environment.articles.public + full_text_search end def contents @@ -70,40 +66,23 @@ class SearchController < PublicController end def people - if !@empty_query - full_text_search - else - @searches[@asset] = {} - @searches[@asset][:results] = visible_profiles(Person).send(@filter).paginate(paginate_options) - end + @scope = visible_profiles(Person) + full_text_search end def products - if !@empty_query - full_text_search - else - @searches[@asset] = {} - @searches[@asset][:results] = @environment.products.send(@filter).paginate(paginate_options) - end + @scope = @environment.products + full_text_search end def enterprises - if !@empty_query - full_text_search - else - @filter_title = _('Enterprises from network') - @searches[@asset] = {} - @searches[@asset][:results] = visible_profiles(Enterprise, [{:products => :product_category}]).paginate(paginate_options) - end + @scope = visible_profiles(Enterprise, [{:products => :product_category}]) + full_text_search end def communities - if !@empty_query - full_text_search - else - @searches[@asset] = {} - @searches[@asset][:results] = visible_profiles(Community).send(@filter).paginate(paginate_options) - end + @scope = visible_profiles(Community) + full_text_search end def events @@ -122,12 +101,8 @@ class SearchController < PublicController environment.events.by_day(@selected_day) end - if !@empty_query - full_text_search - else - @searches[@asset] = {} - @searches[@asset][:results] = date_range ? environment.events.by_range(date_range) : environment.events - end + @scope = date_range ? environment.events.by_range(date_range) : environment.events + full_text_search events = @searches[@asset][:results] @calendar = populate_calendar(date, events) @@ -256,7 +231,7 @@ class SearchController < PublicController end def full_text_search - @searches[@asset] = find_by_contents(asset_class(@asset), @query, paginate_options(params[:page]), {:category => @category}) + @searches[@asset] = find_by_contents(@scope, @query, paginate_options, {:category => @category, :filter => @filter}) end private -- libgit2 0.21.2