diff --git a/lib/institutions_block.rb b/lib/institutions_block.rb new file mode 100644 index 0000000..9f5db10 --- /dev/null +++ b/lib/institutions_block.rb @@ -0,0 +1,63 @@ +class InstitutionsBlock < CommunitiesBlock + + def self.description + _('Institutions') + end + + def profile_count + profile_list.count + end + + def default_title + n_('{#} institution', '{#} institutions', profile_count) + end + + def help + _('This block displays the institutions in which the user is a member.') + end + + def footer + owner = self.owner + case owner + when Profile + lambda do |context| + link_to s_('institutions|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities' + end + when Environment + lambda do |context| + link_to s_('institutions|View all'), :controller => 'search', :action => 'communities' + end + else + '' + end + end + + def profile_list + result = nil + visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment]) + if !prioritize_profiles_with_image + result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand } + elsif profiles.visible.with_image.count >= get_limit + result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } + else + result = 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 + + communities_with_institution_list = [] + + result.each do |r| + if r.class == Community and r.institution? + communities_with_institution_list << r + end + end + + result = communities_with_institution_list + + result.slice(0..get_limit-1) + end + + def profiles + owner.communities + end + +end diff --git a/lib/mpog_software_plugin.rb b/lib/mpog_software_plugin.rb index a99c693..5b51814 100644 --- a/lib/mpog_software_plugin.rb +++ b/lib/mpog_software_plugin.rb @@ -455,6 +455,12 @@ class MpogSoftwarePlugin < Noosfero::Plugin end end + def self.extra_blocks + { + InstitutionsBlock => {:type => [Environment, Person]} + } + end + private # Add and remove the user from it's institutions communities diff --git a/test/unit/institutions_block_test.rb b/test/unit/institutions_block_test.rb new file mode 100644 index 0000000..ca35f1d --- /dev/null +++ b/test/unit/institutions_block_test.rb @@ -0,0 +1,37 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/plugin_test_helper' + +class InstitutionsBlockTest < ActiveSupport::TestCase + include PluginTestHelper + should 'inherit from Block' do + assert_kind_of Block, InstitutionsBlock.new + end + + should 'declare its default title' do + InstitutionsBlock.any_instance.stubs(:profile_count).returns(0) + assert_not_equal Block.new.default_title, InstitutionsBlock.new.default_title + end + + should 'describe itself' do + assert_not_equal Block.description, InstitutionsBlock.description + end + + should 'give empty footer on unsupported owner type' do + block = InstitutionsBlock.new + block.expects(:owner).returns(1) + assert_equal '', block.footer + end + + should 'list institutions' do + user = create_person("Jose_Augusto", "jose_augusto@email.com", "aaaaaaa", "aaaaaaa", "jose_silva@email.com", "DF", "Gama") + + institution = create_private_institution "inst", "00000000000000", "country", "state", "city" + institution.community.add_member(user) + + block = InstitutionsBlock.new + block.expects(:owner).at_least_once.returns(user) + + assert_equivalent [institution.community], block.profiles + end + +end -- libgit2 0.21.2