Commit 27e1092c92042be0ca3ecc3ae81416d068023bed

Authored by Luciano Prestes
Committed by Gabriela Navarro
1 parent 1be114f2
Exists in master and in 79 other branches add_sisp_to_chef, add_super_archives_plugin, api_for_colab, automates_core_packing, backup_not_prod, changes_in_buttons_on_content_panel, colab_automated_login, colab_spb_plugin_recipe, colab_widgets_settings, design_validation, dev_env_minimal, disable_email_dev, fix_breadcrumbs_position, fix_categories_software_link, fix_edit_institution, fix_edit_software_with_another_license, fix_get_license_info, fix_gitlab_assets_permission, fix_list_style_inside_article, fix_list_style_on_folder_elements, fix_members_pagination, fix_merge_request_url, fix_models_translations, fix_no_license, fix_software_api, fix_software_block_migration, fix_software_communities_translations, fix_software_communities_unit_test, fix_style_create_institution_admin_panel, fix_superarchives_imports, fix_sym_links_noosfero, focus_search_field_theme, gov-user-refactoring, gov-user-refactoring-rails4, header_fix, institution_modal_on_rating, kalibro-conf-refactoring, kalibro-processor-package, lxc_settings, margin_fix, mezuro_cookbook, prezento, refactor_download_block, refactor_software_communities, refactor_software_for_sisp, register_page, release-process, release-process-v2, remove-unused-images, remove_broken_theme, remove_secondary_email_from_user, remove_sisp_buttons, removing_super_archives_email, review_message, scope2method, signals_user_noosfero, sisp_catalog_header, sisp_colab_config, sisp_dev, sisp_dev_master, sisp_simple_version, software_as_organization, software_catalog_style_fix, software_communities_html_refactor, software_infos_api, spb_minimal_env, spb_to_rails4, spec_refactor, stable-4.1, stable-4.2, stable-4.x, temp_soft_comm_refactoring, theme_header, theme_javascript_refactory, thread_dropdown, thread_page, update_search_by_categories, update_software_api, update_softwares_boxes

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
... ...