From 89830abf959cf4a438054c95c79e22e5516c7bed Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 22 Oct 2015 16:22:15 -0300 Subject: [PATCH] gov-user: make search extensions through proper hotspots instead of hardcoded --- src/noosfero-spb/gov_user/lib/ext/search_controller.rb | 44 -------------------------------------------- src/noosfero-spb/gov_user/lib/ext/search_helper.rb | 8 -------- src/noosfero-spb/gov_user/lib/gov_user_plugin.rb | 31 +++++++++++++++++++++++++++++++ src/noosfero-spb/gov_user/test/functional/search_controller_test.rb | 2 +- src/noosfero-spb/software_communities/lib/ext/search_controller.rb | 2 +- 5 files changed, 33 insertions(+), 54 deletions(-) delete mode 100644 src/noosfero-spb/gov_user/lib/ext/search_controller.rb delete mode 100644 src/noosfero-spb/gov_user/lib/ext/search_helper.rb diff --git a/src/noosfero-spb/gov_user/lib/ext/search_controller.rb b/src/noosfero-spb/gov_user/lib/ext/search_controller.rb deleted file mode 100644 index 7335fbc..0000000 --- a/src/noosfero-spb/gov_user/lib/ext/search_controller.rb +++ /dev/null @@ -1,44 +0,0 @@ -require_dependency 'search_controller' - -class SearchController - - def communities - delete_communities = [] - valid_communities_string = Community.get_valid_communities_string - Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} - - @scope = visible_profiles(Community) - @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty? - - full_text_search - end - - def institutions - @asset = "gov_user_plugin/institutions" - @assets = [@asset] - @titles["gov_user_plugin/institutions"] = _("Institution Catalog") - results = filter_communities_list{|community| community.institution?} - results = results.paginate(:per_page => 24, :page => params[:page]) - @searches[@asset] = {:results => results} - @search = results - end - - def filter_communities_list - unfiltered_list = visible_profiles(Community) - - unless params[:query].nil? - unfiltered_list = unfiltered_list.select do |com| - com.name.downcase =~ /#{params[:query].downcase}/ - end - end - - communities_list = [] - unfiltered_list.each do |profile| - if profile.class == Community && !profile.is_template? && yield(profile) - communities_list << profile - end - end - - communities_list - end -end diff --git a/src/noosfero-spb/gov_user/lib/ext/search_helper.rb b/src/noosfero-spb/gov_user/lib/ext/search_helper.rb deleted file mode 100644 index 45f9aea..0000000 --- a/src/noosfero-spb/gov_user/lib/ext/search_helper.rb +++ /dev/null @@ -1,8 +0,0 @@ -require_dependency 'search_helper' - -module SearchHelper - - COMMON_PROFILE_LIST_BLOCK ||= [] - COMMON_PROFILE_LIST_BLOCK << "gov_user_plugin/institution" - -end diff --git a/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb b/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb index c441926..07bfe74 100644 --- a/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb +++ b/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb @@ -251,6 +251,37 @@ class GovUserPlugin < Noosfero::Plugin } end + def filter_search_scope(scope, asset) + # Select only communities that are not related to any institution. + if asset.to_s == 'communities' + scope = scope.joins('LEFT OUTER JOIN gov_user_plugin_institutions as institutions + ON profiles.id = institutions.community_id'). + where('institutions.community_id IS NULL') + return [scope, asset] + # Select only communities that are related to an institution. + elsif asset.to_s == 'gov_user_plugin/institutions' + scope = scope.joins('INNER JOIN gov_user_plugin_institutions as institutions + ON profiles.id = institutions.community_id') + return [scope, asset] + # Go with the flow. + else + return [scope, asset] + end + end + + def new_search_assets + class_name_underscored = "gov_user_plugin/institutions" + block = proc do + @scope = visible_profiles(Community) + @asset = class_name_underscored + @assets = [@asset] + @titles[class_name_underscored] = _("Institution Catalog") + params[:limit] = 24 + end + + [{:name => 'institution', :block => block, :common_profile_list_block => class_name_underscored}] + end + private def call_model_transaction(model,name) diff --git a/src/noosfero-spb/gov_user/test/functional/search_controller_test.rb b/src/noosfero-spb/gov_user/test/functional/search_controller_test.rb index 542b9a6..89fc20a 100644 --- a/src/noosfero-spb/gov_user/test/functional/search_controller_test.rb +++ b/src/noosfero-spb/gov_user/test/functional/search_controller_test.rb @@ -12,7 +12,7 @@ class SearchControllerTest < ActionController::TestCase def setup @environment = Environment.default - @environment.enabled_plugins = ['SoftwareCommunitiesPlugin'] + @environment.enabled_plugins = ['SoftwareCommunitiesPlugin', 'GovUserPlugin'] @environment.save @controller = SearchController.new diff --git a/src/noosfero-spb/software_communities/lib/ext/search_controller.rb b/src/noosfero-spb/software_communities/lib/ext/search_controller.rb index e96cd63..f422855 100644 --- a/src/noosfero-spb/software_communities/lib/ext/search_controller.rb +++ b/src/noosfero-spb/software_communities/lib/ext/search_controller.rb @@ -8,7 +8,7 @@ class SearchController Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} @scope = visible_profiles(Community) - @scope = @scope.where(["id NOT IN (?)", delete_communities]) unless delete_communities.empty? + @scope = @scope.where(["profiles.id NOT IN (?)", delete_communities]) unless delete_communities.empty? full_text_search end -- libgit2 0.21.2