Commit 5954b0972f784a8d59bbe996e967349fd400328d

Authored by Rodrigo Souto
1 parent 00620c69

Concentrating find_by_contents on application controller

app/controllers/admin/region_validators_controller.rb
@@ -33,7 +33,7 @@ class RegionValidatorsController < AdminController @@ -33,7 +33,7 @@ class RegionValidatorsController < AdminController
33 def load_region_and_search 33 def load_region_and_search
34 @region = environment.regions.find(params[:id]) 34 @region = environment.regions.find(params[:id])
35 if params[:search] 35 if params[:search]
36 - @search = @region.search_possible_validators(params[:search]) 36 + @search = find_by_contents(Organization, params[:search])[:results].docs.reject {|item| @region.validator_ids.include?(item.id) }
37 end 37 end
38 end 38 end
39 39
app/controllers/application_controller.rb
@@ -154,4 +154,9 @@ class ApplicationController < ActionController::Base @@ -154,4 +154,9 @@ 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
  160 + end
  161 +
157 end 162 end
app/controllers/my_profile/cms_controller.rb
@@ -270,8 +270,7 @@ class CmsController < MyProfileController @@ -270,8 +270,7 @@ class CmsController < MyProfileController
270 270
271 def search 271 def search
272 query = params[:q] 272 query = params[:q]
273 - results = profile.files.published.find_by_contents(query)[:results]  
274 - # results = @plugins.first(:find_by_contents, profile.files.published, query)[:results] 273 + results = find_by_contents(profile.files.published, query)[:results]
275 render :text => article_list_to_json(results), :content_type => 'application/json' 274 render :text => article_list_to_json(results), :content_type => 'application/json'
276 end 275 end
277 276
app/controllers/public/profile_search_controller.rb
@@ -11,7 +11,7 @@ class ProfileSearchController < PublicController @@ -11,7 +11,7 @@ class ProfileSearchController < PublicController
11 if params[:where] == 'environment' 11 if params[:where] == 'environment'
12 redirect_to :controller => 'search', :query => @q 12 redirect_to :controller => 'search', :query => @q
13 else 13 else
14 - @results = @plugins.first(:find_by_contents, profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results] 14 + @results = find_by_contents(profile.articles.published, @q, {:per_page => 10, :page => params[:page]})[:results]
15 end 15 end
16 end 16 end
17 end 17 end
app/controllers/public/search_controller.rb
@@ -8,7 +8,6 @@ class SearchController < PublicController @@ -8,7 +8,6 @@ class SearchController < PublicController
8 before_filter :load_category 8 before_filter :load_category
9 before_filter :load_search_assets 9 before_filter :load_search_assets
10 before_filter :load_query 10 before_filter :load_query
11 - before_filter :load_search_engine  
12 11
13 # Backwards compatibility with old URLs 12 # Backwards compatibility with old URLs
14 def redirect_asset_param 13 def redirect_asset_param
@@ -58,7 +57,7 @@ class SearchController < PublicController @@ -58,7 +57,7 @@ class SearchController < PublicController
58 end 57 end
59 58
60 def articles 59 def articles
61 - if @search_engine && !@empty_query 60 + if !@empty_query
62 full_text_search 61 full_text_search
63 else 62 else
64 @searches[@asset] = {} 63 @searches[@asset] = {}
@@ -71,7 +70,7 @@ class SearchController < PublicController @@ -71,7 +70,7 @@ class SearchController < PublicController
71 end 70 end
72 71
73 def people 72 def people
74 - if @search_engine && !@empty_query 73 + if !@empty_query
75 full_text_search 74 full_text_search
76 else 75 else
77 @searches[@asset] = {} 76 @searches[@asset] = {}
@@ -80,7 +79,7 @@ class SearchController < PublicController @@ -80,7 +79,7 @@ class SearchController < PublicController
80 end 79 end
81 80
82 def products 81 def products
83 - if @search_engine 82 + if !@empty_query
84 full_text_search 83 full_text_search
85 else 84 else
86 @searches[@asset] = {} 85 @searches[@asset] = {}
@@ -89,7 +88,7 @@ class SearchController < PublicController @@ -89,7 +88,7 @@ class SearchController < PublicController
89 end 88 end
90 89
91 def enterprises 90 def enterprises
92 - if @search_engine && !@empty_query 91 + if !@empty_query
93 full_text_search 92 full_text_search
94 else 93 else
95 @filter_title = _('Enterprises from network') 94 @filter_title = _('Enterprises from network')
@@ -99,7 +98,7 @@ class SearchController < PublicController @@ -99,7 +98,7 @@ class SearchController < PublicController
99 end 98 end
100 99
101 def communities 100 def communities
102 - if @search_engine && !@empty_query 101 + if !@empty_query
103 full_text_search 102 full_text_search
104 else 103 else
105 @searches[@asset] = {} 104 @searches[@asset] = {}
@@ -123,7 +122,7 @@ class SearchController < PublicController @@ -123,7 +122,7 @@ class SearchController < PublicController
123 environment.events.by_day(@selected_day) 122 environment.events.by_day(@selected_day)
124 end 123 end
125 124
126 - if @search_engine && !@empty_query 125 + if !@empty_query
127 full_text_search 126 full_text_search
128 else 127 else
129 @searches[@asset] = {} 128 @searches[@asset] = {}
@@ -192,10 +191,6 @@ class SearchController < PublicController @@ -192,10 +191,6 @@ class SearchController < PublicController
192 end 191 end
193 end 192 end
194 193
195 - def load_search_engine  
196 - @search_engine = @plugins.first_plugin(:search_engine?)  
197 - end  
198 -  
199 FILTERS = %w( 194 FILTERS = %w(
200 more_recent 195 more_recent
201 more_active 196 more_active
@@ -261,7 +256,7 @@ class SearchController < PublicController @@ -261,7 +256,7 @@ class SearchController < PublicController
261 end 256 end
262 257
263 def full_text_search 258 def full_text_search
264 - @searches[@asset] = @plugins.first(:find_by_contents, asset_class(@asset), @query, paginate_options(params[:page]), {:category => @category}) 259 + @searches[@asset] = find_by_contents(asset_class(@asset), @query, paginate_options(params[:page]), {:category => @category})
265 end 260 end
266 261
267 private 262 private
app/models/region.rb
@@ -4,14 +4,6 @@ class Region < Category @@ -4,14 +4,6 @@ class Region < Category
4 4
5 require_dependency 'enterprise' # enterprises can also be validators 5 require_dependency 'enterprise' # enterprises can also be validators
6 6
7 - include Noosfero::Plugin::HotSpot  
8 -  
9 - # searches for organizations that could become validators for this region.  
10 - # <tt>search</tt> is passed as is to find_by_contents on Organization.  
11 - def search_possible_validators(search)  
12 - plugins.find_by_contents(Organization, search)[:results].docs.reject {|item| self.validator_ids.include?(item.id) }  
13 - end  
14 -  
15 def has_validator? 7 def has_validator?
16 validators.count > 0 8 validators.count > 0
17 end 9 end
app/views/layouts/application-ng.rhtml
@@ -69,13 +69,11 @@ @@ -69,13 +69,11 @@
69 <% end %> 69 <% end %>
70 70
71 </span> 71 </span>
72 - <% if @plugins.first(:search_engine?) %>  
73 - <form action="/search" class="search_form" method="get" class="clean">  
74 - <input name="query" size="15" title="<%=_('Search...')%>" onfocus="this.form.className='focused';" onblur="this.form.className=''" />  
75 - <div><%=_('Press <strong>Enter</strong> to send the search query.')%></div>  
76 - <%= javascript_tag 'jQuery("#user form input").hint();' %>  
77 - </form>  
78 - <% end %> 72 + <form action="/search" class="search_form" method="get" class="clean">
  73 + <input name="query" size="15" title="<%=_('Search...')%>" onfocus="this.form.className='focused';" onblur="this.form.className=''" />
  74 + <div><%=_('Press <strong>Enter</strong> to send the search query.')%></div>
  75 + <%= javascript_tag 'jQuery("#user form input").hint();' %>
  76 + </form>
79 </div><!-- end id="user" --> 77 </div><!-- end id="user" -->
80 78
81 <h1 id="site-title"> 79 <h1 id="site-title">
app/views/search/_profile.rhtml
1 <li class="search-profile-item"> 1 <li class="search-profile-item">
2 -<% if @empty_query || multiple_search? || !profile.enterprise? || !@search_engine %> 2 +<% if @empty_query || multiple_search? || !profile.enterprise? %>
3 <%= profile_image_link profile, :portrait, 'div', 3 <%= profile_image_link profile, :portrait, 'div',
4 @filter == 'more_recent' ? profile.send(@filter + '_label') + show_date(profile.created_at) : profile.send(@filter + '_label') %> 4 @filter == 'more_recent' ? profile.send(@filter + '_label') + show_date(profile.created_at) : profile.send(@filter + '_label') %>
5 <% else %> 5 <% else %>
app/views/search/category_index.rhtml
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 <% end %> 5 <% end %>
6 6
7 <%= search_page_title(_('Search Results'), @category) %> 7 <%= search_page_title(_('Search Results'), @category) %>
8 - <%= render :partial => 'search_form', :locals => { :hint => '' } if @search_engine %> 8 + <%= render :partial => 'search_form', :locals => { :hint => '' } %>
9 <%= category_context(@category, params) %> 9 <%= category_context(@category, params) %>
10 <%= display_results %> 10 <%= display_results %>
11 11
app/views/search/index.rhtml
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 <% end %> 5 <% end %>
6 6
7 <%= search_page_title(_('Search Results'), @category) %> 7 <%= search_page_title(_('Search Results'), @category) %>
8 - <%= render :partial => 'search_form', :locals => { :hint => '' } if @search_engine %> 8 + <%= render :partial => 'search_form', :locals => { :hint => '' } %>
9 <%= category_context(@category, params) %> 9 <%= category_context(@category, params) %>
10 <%= display_results(@asset) %> 10 <%= display_results(@asset) %>
11 11
lib/noosfero/plugin.rb
@@ -351,18 +351,11 @@ class Noosfero::Plugin @@ -351,18 +351,11 @@ class Noosfero::Plugin
351 nil 351 nil
352 end 352 end
353 353
354 - # -> Specifies plugin that works as a search engine  
355 - # returns = true/false  
356 - def search_engine?  
357 - false  
358 - end  
359 -  
360 # -> Finds objects by their contents 354 # -> Finds objects by their contents
361 # returns = {:results => [a, b, c, ...], ...} 355 # returns = {:results => [a, b, c, ...], ...}
362 # P.S.: The plugin might add other informations on the return hash for its 356 # P.S.: The plugin might add other informations on the return hash for its
363 # own use in specific views 357 # own use in specific views
364 def find_by_contents(klass, query, paginate_options={}, options={}) 358 def find_by_contents(klass, query, paginate_options={}, options={})
365 - {:results => []}  
366 end 359 end
367 360
368 def method_missing(method, *args, &block) 361 def method_missing(method, *args, &block)
lib/noosfero/plugin/manager.rb
@@ -39,7 +39,7 @@ class Noosfero::Plugin::Manager @@ -39,7 +39,7 @@ class Noosfero::Plugin::Manager
39 result = plugin.send(event, *args) 39 result = plugin.send(event, *args)
40 break if result.present? 40 break if result.present?
41 end 41 end
42 - result || Noosfero::Plugin.new.send(event, *args) 42 + result
43 end 43 end
44 44
45 def first_plugin(event, *args) 45 def first_plugin(event, *args)
plugins/solr/lib/solr_plugin.rb
@@ -12,10 +12,6 @@ class SolrPlugin &lt; Noosfero::Plugin @@ -12,10 +12,6 @@ class SolrPlugin &lt; Noosfero::Plugin
12 _("Uses Solr as search engine.") 12 _("Uses Solr as search engine.")
13 end 13 end
14 14
15 - def search_engine?  
16 - true  
17 - end  
18 -  
19 def find_by_contents(klass, query, paginate_options={}, options={}) 15 def find_by_contents(klass, query, paginate_options={}, options={})
20 category = options.delete(:category) 16 category = options.delete(:category)
21 solr_options = solr_options(class_asset(klass), category) 17 solr_options = solr_options(class_asset(klass), category)
test/unit/plugin_manager_test.rb
@@ -140,15 +140,5 @@ class PluginManagerTest &lt; ActiveSupport::TestCase @@ -140,15 +140,5 @@ class PluginManagerTest &lt; ActiveSupport::TestCase
140 assert_equal Plugin2, manager.first_plugin(:random_event) 140 assert_equal Plugin2, manager.first_plugin(:random_event)
141 end 141 end
142 142
143 - should 'use default value defined on the base plugin class when no plugin enabled for the first method' do  
144 - class Noosfero::Plugin  
145 - def random_event  
146 - 1  
147 - end  
148 - end  
149 -  
150 - assert_equal 1, manager.first(:random_event)  
151 - end  
152 -  
153 end 143 end
154 144