Commit 27e1092c92042be0ca3ecc3ae81416d068023bed

Authored by Luciano Prestes
Committed by Gabriela Navarro
1 parent 1be114f2

Add institution block and unit tests.

Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Signed-off-by: Parley Martins <parley@outlook.com>
lib/institutions_block.rb 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +class InstitutionsBlock < CommunitiesBlock
  2 +
  3 + def self.description
  4 + _('Institutions')
  5 + end
  6 +
  7 + def profile_count
  8 + profile_list.count
  9 + end
  10 +
  11 + def default_title
  12 + n_('{#} institution', '{#} institutions', profile_count)
  13 + end
  14 +
  15 + def help
  16 + _('This block displays the institutions in which the user is a member.')
  17 + end
  18 +
  19 + def footer
  20 + owner = self.owner
  21 + case owner
  22 + when Profile
  23 + lambda do |context|
  24 + link_to s_('institutions|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities'
  25 + end
  26 + when Environment
  27 + lambda do |context|
  28 + link_to s_('institutions|View all'), :controller => 'search', :action => 'communities'
  29 + end
  30 + else
  31 + ''
  32 + end
  33 + end
  34 +
  35 + def profile_list
  36 + result = nil
  37 + visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment])
  38 + if !prioritize_profiles_with_image
  39 + result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand }
  40 + elsif profiles.visible.with_image.count >= get_limit
  41 + result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand }
  42 + else
  43 + 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 }
  44 + end
  45 +
  46 + communities_with_institution_list = []
  47 +
  48 + result.each do |r|
  49 + if r.class == Community and r.institution?
  50 + communities_with_institution_list << r
  51 + end
  52 + end
  53 +
  54 + result = communities_with_institution_list
  55 +
  56 + result.slice(0..get_limit-1)
  57 + end
  58 +
  59 + def profiles
  60 + owner.communities
  61 + end
  62 +
  63 +end
... ...
lib/mpog_software_plugin.rb
... ... @@ -455,6 +455,12 @@ class MpogSoftwarePlugin &lt; Noosfero::Plugin
455 455 end
456 456 end
457 457  
  458 + def self.extra_blocks
  459 + {
  460 + InstitutionsBlock => {:type => [Environment, Person]}
  461 + }
  462 + end
  463 +
458 464 private
459 465  
460 466 # Add and remove the user from it's institutions communities
... ...
test/unit/institutions_block_test.rb 0 → 100644
... ... @@ -0,0 +1,37 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/plugin_test_helper'
  3 +
  4 +class InstitutionsBlockTest < ActiveSupport::TestCase
  5 + include PluginTestHelper
  6 + should 'inherit from Block' do
  7 + assert_kind_of Block, InstitutionsBlock.new
  8 + end
  9 +
  10 + should 'declare its default title' do
  11 + InstitutionsBlock.any_instance.stubs(:profile_count).returns(0)
  12 + assert_not_equal Block.new.default_title, InstitutionsBlock.new.default_title
  13 + end
  14 +
  15 + should 'describe itself' do
  16 + assert_not_equal Block.description, InstitutionsBlock.description
  17 + end
  18 +
  19 + should 'give empty footer on unsupported owner type' do
  20 + block = InstitutionsBlock.new
  21 + block.expects(:owner).returns(1)
  22 + assert_equal '', block.footer
  23 + end
  24 +
  25 + should 'list institutions' do
  26 + user = create_person("Jose_Augusto", "jose_augusto@email.com", "aaaaaaa", "aaaaaaa", "jose_silva@email.com", "DF", "Gama")
  27 +
  28 + institution = create_private_institution "inst", "00000000000000", "country", "state", "city"
  29 + institution.community.add_member(user)
  30 +
  31 + block = InstitutionsBlock.new
  32 + block.expects(:owner).at_least_once.returns(user)
  33 +
  34 + assert_equivalent [institution.community], block.profiles
  35 + end
  36 +
  37 +end
... ...