From 449ea30f10f1a8bab798638d441e750f400d05a5 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 27 Oct 2015 12:33:23 -0300 Subject: [PATCH] gov-user: make communities_block extensions through proper hotspots --- src/noosfero-spb/gov_user/lib/ext/communities_block.rb | 45 --------------------------------------------- src/noosfero-spb/gov_user/lib/ext/community.rb | 16 ---------------- src/noosfero-spb/gov_user/lib/gov_user_plugin.rb | 51 +++++++++++++++++++++++++++++++++------------------ src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb | 38 -------------------------------------- src/noosfero-spb/gov_user/test/unit/communities_block.rb | 23 +++++++++++++++++++++++ src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb | 27 +++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 117 deletions(-) delete mode 100644 src/noosfero-spb/gov_user/lib/ext/communities_block.rb create mode 100644 src/noosfero-spb/gov_user/test/unit/communities_block.rb diff --git a/src/noosfero-spb/gov_user/lib/ext/communities_block.rb b/src/noosfero-spb/gov_user/lib/ext/communities_block.rb deleted file mode 100644 index 4a5fcaa..0000000 --- a/src/noosfero-spb/gov_user/lib/ext/communities_block.rb +++ /dev/null @@ -1,45 +0,0 @@ -require_dependency 'communities_block' - -class CommunitiesBlock - - def profile_list - result = get_visible_profiles - result.slice(0..get_limit-1) - end - - def profile_count - profile_list.count - end - - private - - def get_visible_profiles - visible_profiles = profiles.visible.includes( - [:image,:domains,:preferred_domain,:environment] - ) - - delete_communities = [] - valid_communities_string = Community.get_valid_communities_string - Community.all.each{|community| delete_communities << community.id unless eval(valid_communities_string)} - - visible_profiles = visible_profiles.where(["profiles.id NOT IN (?)", delete_communities]) unless delete_communities.empty? - - if !prioritize_profiles_with_image - return visible_profiles.all( - :limit => get_limit, - :order => 'profiles.updated_at DESC' - ).sort_by {rand} - elsif profiles.visible.with_image.count >= get_limit - return visible_profiles.with_image.all( - :limit => get_limit * 5, - :order => 'profiles.updated_at DESC' - ).sort_by {rand} - else - visible_profiles = visible_profiles.with_image.sort_by {rand} + - visible_profiles.without_image.all( - :limit => get_limit * 5, :order => 'profiles.updated_at DESC' - ).sort_by {rand} - return visible_profiles - end - end -end diff --git a/src/noosfero-spb/gov_user/lib/ext/community.rb b/src/noosfero-spb/gov_user/lib/ext/community.rb index e87a630..dca2013 100644 --- a/src/noosfero-spb/gov_user/lib/ext/community.rb +++ b/src/noosfero-spb/gov_user/lib/ext/community.rb @@ -6,20 +6,4 @@ class Community def institution? return !institution.nil? end - - def remove_of_community_search_institution? - return institution? - end - - def self.get_valid_communities_string - remove_of_communities_methods = Community.instance_methods.select{|m| m =~ /remove_of_community_search/} - valid_communities_string = "!(" - remove_of_communities_methods.each do |method| - valid_communities_string += "community.send('#{method}') || " - end - valid_communities_string = valid_communities_string[0..-5] - valid_communities_string += ")" - - valid_communities_string - end 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 07bfe74..0a36554 100644 --- a/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb +++ b/src/noosfero-spb/gov_user/lib/gov_user_plugin.rb @@ -252,18 +252,10 @@ 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. + return [filter_communities(scope), asset] 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. + return [filter_institutions(scope), asset] else return [scope, asset] end @@ -282,6 +274,16 @@ class GovUserPlugin < Noosfero::Plugin [{:name => 'institution', :block => block, :common_profile_list_block => class_name_underscored}] end + def filter_profile_list_block_scope(scope, klass) + if klass == CommunitiesBlock + return [filter_communities(scope), klass] + elsif klass == GovUserPlugin::InstitutionsBlock + return [filter_institutions(scope), klass] + else + return [scope, klass] + end + end + private def call_model_transaction(model,name) @@ -349,15 +351,28 @@ class GovUserPlugin < Noosfero::Plugin } end - def update_user_institutions(user) - context.params[:user][:institution_ids].each do |institution_id| - institution = GovUserPlugin::Institution.find institution_id - user.institutions << institution + def update_user_institutions(user) + context.params[:user][:institution_ids].each do |institution_id| + institution = GovUserPlugin::Institution.find institution_id + user.institutions << institution - if institution.community.admins.blank? - institution.community.add_admin(user.person) - end + if institution.community.admins.blank? + institution.community.add_admin(user.person) end - user.save unless user.institution_ids.empty? end + user.save unless user.institution_ids.empty? + end + + # Select only communities that are not related to any institution. + def filter_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') + end + + # Select only communities that are related to an institution. + def filter_institutions(scope) + scope.joins('INNER JOIN gov_user_plugin_institutions as institutions + ON profiles.id = institutions.community_id') + end end diff --git a/src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb b/src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb index 6c73dd3..0df3013 100644 --- a/src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb +++ b/src/noosfero-spb/gov_user/lib/gov_user_plugin/institutions_block.rb @@ -3,10 +3,6 @@ class GovUserPlugin::InstitutionsBlock < CommunitiesBlock _('Institutions') end - def profile_count - profile_list.count - end - def default_title n_('{#} institution', '{#} institutions', profile_count) end @@ -33,38 +29,4 @@ class GovUserPlugin::InstitutionsBlock < CommunitiesBlock '' end end - - def profile_list - result = get_visible_profiles - - result = result.select { |p| p.class == Community && p.institution? } - - result.slice(0..get_limit-1) - end - - def profiles - owner.communities - end - - private - - def get_visible_profiles - include_list = [:image,:domains,:preferred_domain,:environment] - visible_profiles = profiles.visible.includes(include_list) - - if !prioritize_profiles_with_image - visible_profiles.all(:limit => get_limit, - :order => 'profiles.updated_at DESC' - ).sort_by{ rand } - elsif profiles.visible.with_image.count >= get_limit - visible_profiles.with_image.all(:limit => get_limit * 5, - :order => 'profiles.updated_at DESC' - ).sort_by{ rand } - else - visible_profiles.with_image.sort_by{ rand } + - visible_profiles.without_image.all(:limit => get_limit * 5, - :order => 'profiles.updated_at DESC' - ).sort_by{ rand } - end - end end diff --git a/src/noosfero-spb/gov_user/test/unit/communities_block.rb b/src/noosfero-spb/gov_user/test/unit/communities_block.rb new file mode 100644 index 0000000..511d18f --- /dev/null +++ b/src/noosfero-spb/gov_user/test/unit/communities_block.rb @@ -0,0 +1,23 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../helpers/institution_test_helper' + +class CommunitiesBlockTest < ActiveSupport::TestCase + def setup + environment = Environment.default + environment.enable_plugin(GovUserPlugin) + end + + should 'not list instutitions' do + person = create_user('some-user').person + institution = InstitutionTestHelper.create_private_institution("inst name", "IN", "country", "state", + "city", "00.111.222/3333-44") + community = fast_create(Community) + institution.community.add_member(person) + community.add_member(person) + + block = CommunitiesBlock.new + block.expects(:owner).at_least_once.returns(person) + + assert_equivalent block.profiles, [community] + end +end diff --git a/src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb b/src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb index d967147..c1aa323 100644 --- a/src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb +++ b/src/noosfero-spb/gov_user/test/unit/gov_user_plugin/institutions_block_test.rb @@ -2,6 +2,11 @@ require File.dirname(__FILE__) + '/../../../../../test/test_helper' require File.dirname(__FILE__) + '/../../helpers/plugin_test_helper' class GovUserPlugin::InstitutionsBlockTest < ActiveSupport::TestCase + def setup + environment = Environment.default + environment.enable_plugin(GovUserPlugin) + end + include PluginTestHelper should 'inherit from Block' do assert_kind_of Block, GovUserPlugin::InstitutionsBlock.new @@ -48,4 +53,26 @@ class GovUserPlugin::InstitutionsBlockTest < ActiveSupport::TestCase assert_equivalent [institution.community], block.profiles end + + should 'not list communities' do + user = create_person("Jose_Augusto", + "jose_augusto@email.com", + "aaaaaaa", + "aaaaaaa", + 'jose@secondary.com', + "DF", + "Gama" + ) + institution = InstitutionTestHelper.create_private_institution("inst name", "IN", "country", "state", + "city", "00.111.222/3333-44") + community = fast_create(Community) + institution.community.add_member(user) + community.add_member(user) + + block = GovUserPlugin::InstitutionsBlock.new + block.expects(:owner).at_least_once.returns(user) + + assert_equivalent block.profiles, [institution.community] + end + end -- libgit2 0.21.2