Commit 89830abf959cf4a438054c95c79e22e5516c7bed

Authored by Rodrigo Souto
1 parent 72bf3be8

gov-user: make search extensions through proper hotspots instead of hardcoded

Closes #399
src/noosfero-spb/gov_user/lib/ext/search_controller.rb
... ... @@ -1,44 +0,0 @@
1   -require_dependency 'search_controller'
2   -
3   -class SearchController
4   -
5   - def communities
6   - delete_communities = []
7   - valid_communities_string = Community.get_valid_communities_string
8   - Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)}
9   -
10   - @scope = visible_profiles(Community)
11   - @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty?
12   -
13   - full_text_search
14   - end
15   -
16   - def institutions
17   - @asset = "gov_user_plugin/institutions"
18   - @assets = [@asset]
19   - @titles["gov_user_plugin/institutions"] = _("Institution Catalog")
20   - results = filter_communities_list{|community| community.institution?}
21   - results = results.paginate(:per_page => 24, :page => params[:page])
22   - @searches[@asset] = {:results => results}
23   - @search = results
24   - end
25   -
26   - def filter_communities_list
27   - unfiltered_list = visible_profiles(Community)
28   -
29   - unless params[:query].nil?
30   - unfiltered_list = unfiltered_list.select do |com|
31   - com.name.downcase =~ /#{params[:query].downcase}/
32   - end
33   - end
34   -
35   - communities_list = []
36   - unfiltered_list.each do |profile|
37   - if profile.class == Community && !profile.is_template? && yield(profile)
38   - communities_list << profile
39   - end
40   - end
41   -
42   - communities_list
43   - end
44   -end
src/noosfero-spb/gov_user/lib/ext/search_helper.rb
... ... @@ -1,8 +0,0 @@
1   -require_dependency 'search_helper'
2   -
3   -module SearchHelper
4   -
5   - COMMON_PROFILE_LIST_BLOCK ||= []
6   - COMMON_PROFILE_LIST_BLOCK << "gov_user_plugin/institution"
7   -
8   -end
src/noosfero-spb/gov_user/lib/gov_user_plugin.rb
... ... @@ -251,6 +251,37 @@ class GovUserPlugin &lt; Noosfero::Plugin
251 251 }
252 252 end
253 253  
  254 + def filter_search_scope(scope, asset)
  255 + # Select only communities that are not related to any institution.
  256 + if asset.to_s == 'communities'
  257 + scope = scope.joins('LEFT OUTER JOIN gov_user_plugin_institutions as institutions
  258 + ON profiles.id = institutions.community_id').
  259 + where('institutions.community_id IS NULL')
  260 + return [scope, asset]
  261 + # Select only communities that are related to an institution.
  262 + elsif asset.to_s == 'gov_user_plugin/institutions'
  263 + scope = scope.joins('INNER JOIN gov_user_plugin_institutions as institutions
  264 + ON profiles.id = institutions.community_id')
  265 + return [scope, asset]
  266 + # Go with the flow.
  267 + else
  268 + return [scope, asset]
  269 + end
  270 + end
  271 +
  272 + def new_search_assets
  273 + class_name_underscored = "gov_user_plugin/institutions"
  274 + block = proc do
  275 + @scope = visible_profiles(Community)
  276 + @asset = class_name_underscored
  277 + @assets = [@asset]
  278 + @titles[class_name_underscored] = _("Institution Catalog")
  279 + params[:limit] = 24
  280 + end
  281 +
  282 + [{:name => 'institution', :block => block, :common_profile_list_block => class_name_underscored}]
  283 + end
  284 +
254 285 private
255 286  
256 287 def call_model_transaction(model,name)
... ...
src/noosfero-spb/gov_user/test/functional/search_controller_test.rb
... ... @@ -12,7 +12,7 @@ class SearchControllerTest &lt; ActionController::TestCase
12 12  
13 13 def setup
14 14 @environment = Environment.default
15   - @environment.enabled_plugins = ['SoftwareCommunitiesPlugin']
  15 + @environment.enabled_plugins = ['SoftwareCommunitiesPlugin', 'GovUserPlugin']
16 16 @environment.save
17 17  
18 18 @controller = SearchController.new
... ...
src/noosfero-spb/software_communities/lib/ext/search_controller.rb
... ... @@ -8,7 +8,7 @@ class SearchController
8 8 Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)}
9 9  
10 10 @scope = visible_profiles(Community)
11   - @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty?
  11 + @scope = @scope.where(["profiles.id NOT IN (?)", delete_communities]) unless delete_communities.empty?
12 12  
13 13 full_text_search
14 14 end
... ...