Commit 35733cf407089a2cb4f5b781d4d7de67df057776
1 parent
b13acb43
Exists in
master
and in
29 other branches
Fallback find_by_contens in case there is no search plugin
Showing
2 changed files
with
30 additions
and
41 deletions
Show diff stats
app/controllers/application_controller.rb
@@ -154,9 +154,23 @@ class ApplicationController < ActionController::Base | @@ -154,9 +154,23 @@ class ApplicationController < ActionController::Base | ||
154 | end | 154 | end |
155 | end | 155 | end |
156 | 156 | ||
157 | - def find_by_contents(klass, query, paginate_options={}, options={}) | ||
158 | - @plugins.first(:find_by_contents, klass, query, paginate_options, options)# || | ||
159 | - # Failback search using like | 157 | + def find_by_contents(scope, query, paginate_options={}, options={}) |
158 | + if query.blank? | ||
159 | + scope = scope.send(options[:filter]) if options[:filter] | ||
160 | + return {:results => scope.paginate(paginate_options)} | ||
161 | + end | ||
162 | + @plugins.first(:find_by_contents, scope, query, paginate_options, options) || | ||
163 | + fallback_find_by_contents(scope, query, paginate_options, options) | ||
164 | + end | ||
165 | + | ||
166 | + private | ||
167 | + | ||
168 | + def fallback_find_by_contents(scope, query, paginate_options, options) | ||
169 | + fields = scope.base_class::SEARCHABLE_FIELDS.keys.map(&:to_s) & scope.base_class.column_names | ||
170 | + conditions = fields.map do |field| | ||
171 | + "#{scope.base_class.table_name}.#{field} LIKE \"%#{query.downcase}%\"" | ||
172 | + end.join(' OR ') | ||
173 | + {:results => scope.send(options[:filter]).where(conditions).paginate(paginate_options)} | ||
160 | end | 174 | end |
161 | 175 | ||
162 | end | 176 | end |
app/controllers/public/search_controller.rb
@@ -57,12 +57,8 @@ class SearchController < PublicController | @@ -57,12 +57,8 @@ class SearchController < PublicController | ||
57 | end | 57 | end |
58 | 58 | ||
59 | def articles | 59 | def articles |
60 | - if !@empty_query | ||
61 | - full_text_search | ||
62 | - else | ||
63 | - @searches[@asset] = {} | ||
64 | - @searches[@asset][:results] = @environment.articles.public.send(@filter).paginate(paginate_options) | ||
65 | - end | 60 | + @scope = @environment.articles.public |
61 | + full_text_search | ||
66 | end | 62 | end |
67 | 63 | ||
68 | def contents | 64 | def contents |
@@ -70,40 +66,23 @@ class SearchController < PublicController | @@ -70,40 +66,23 @@ class SearchController < PublicController | ||
70 | end | 66 | end |
71 | 67 | ||
72 | def people | 68 | def people |
73 | - if !@empty_query | ||
74 | - full_text_search | ||
75 | - else | ||
76 | - @searches[@asset] = {} | ||
77 | - @searches[@asset][:results] = visible_profiles(Person).send(@filter).paginate(paginate_options) | ||
78 | - end | 69 | + @scope = visible_profiles(Person) |
70 | + full_text_search | ||
79 | end | 71 | end |
80 | 72 | ||
81 | def products | 73 | def products |
82 | - if !@empty_query | ||
83 | - full_text_search | ||
84 | - else | ||
85 | - @searches[@asset] = {} | ||
86 | - @searches[@asset][:results] = @environment.products.send(@filter).paginate(paginate_options) | ||
87 | - end | 74 | + @scope = @environment.products |
75 | + full_text_search | ||
88 | end | 76 | end |
89 | 77 | ||
90 | def enterprises | 78 | def enterprises |
91 | - if !@empty_query | ||
92 | - full_text_search | ||
93 | - else | ||
94 | - @filter_title = _('Enterprises from network') | ||
95 | - @searches[@asset] = {} | ||
96 | - @searches[@asset][:results] = visible_profiles(Enterprise, [{:products => :product_category}]).paginate(paginate_options) | ||
97 | - end | 79 | + @scope = visible_profiles(Enterprise, [{:products => :product_category}]) |
80 | + full_text_search | ||
98 | end | 81 | end |
99 | 82 | ||
100 | def communities | 83 | def communities |
101 | - if !@empty_query | ||
102 | - full_text_search | ||
103 | - else | ||
104 | - @searches[@asset] = {} | ||
105 | - @searches[@asset][:results] = visible_profiles(Community).send(@filter).paginate(paginate_options) | ||
106 | - end | 84 | + @scope = visible_profiles(Community) |
85 | + full_text_search | ||
107 | end | 86 | end |
108 | 87 | ||
109 | def events | 88 | def events |
@@ -122,12 +101,8 @@ class SearchController < PublicController | @@ -122,12 +101,8 @@ class SearchController < PublicController | ||
122 | environment.events.by_day(@selected_day) | 101 | environment.events.by_day(@selected_day) |
123 | end | 102 | end |
124 | 103 | ||
125 | - if !@empty_query | ||
126 | - full_text_search | ||
127 | - else | ||
128 | - @searches[@asset] = {} | ||
129 | - @searches[@asset][:results] = date_range ? environment.events.by_range(date_range) : environment.events | ||
130 | - end | 104 | + @scope = date_range ? environment.events.by_range(date_range) : environment.events |
105 | + full_text_search | ||
131 | 106 | ||
132 | events = @searches[@asset][:results] | 107 | events = @searches[@asset][:results] |
133 | @calendar = populate_calendar(date, events) | 108 | @calendar = populate_calendar(date, events) |
@@ -256,7 +231,7 @@ class SearchController < PublicController | @@ -256,7 +231,7 @@ class SearchController < PublicController | ||
256 | end | 231 | end |
257 | 232 | ||
258 | def full_text_search | 233 | def full_text_search |
259 | - @searches[@asset] = find_by_contents(asset_class(@asset), @query, paginate_options(params[:page]), {:category => @category}) | 234 | + @searches[@asset] = find_by_contents(@scope, @query, paginate_options, {:category => @category, :filter => @filter}) |
260 | end | 235 | end |
261 | 236 | ||
262 | private | 237 | private |