diff --git a/lib/mpog_software_plugin.rb b/lib/mpog_software_plugin.rb index 18fc6a4..5e0d9ca 100644 --- a/lib/mpog_software_plugin.rb +++ b/lib/mpog_software_plugin.rb @@ -204,6 +204,12 @@ class MpogSoftwarePlugin < Noosfero::Plugin end end + def self.extra_blocks + { + SoftwaresBlock => {:type => [Environment, Person] } + } + end + def stylesheet? true end diff --git a/lib/softwares_block.rb b/lib/softwares_block.rb new file mode 100644 index 0000000..d3620d2 --- /dev/null +++ b/lib/softwares_block.rb @@ -0,0 +1,64 @@ +class SoftwaresBlock < CommunitiesBlock + + attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id, :resource_type + + def self.description + _('Softwares') + end + + def default_title + n_('{#} software', '{#} softwares', profile_count) + end + + def help + _('This block displays the softwares in which the user is a member.') + end + + def footer + owner = self.owner + case owner + when Profile + lambda do |context| + link_to s_('softwares|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities' + end + when Environment + lambda do |context| + link_to s_('softwares|View all'), :controller => 'search', :action => 'communities' + end + else + '' + end + end + + def profile_count + profile_list.count + end + + def profiles + owner.communities + 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 + + list_with_software = [] + + visible_profiles.each do |p| + if p.class == Community and p.software? and !p.institution? + list_with_software << p + end + end + + result = list_with_software + + result.slice(0..get_limit-1) + end +end diff --git a/test/unit/softwares_block_test.rb b/test/unit/softwares_block_test.rb new file mode 100644 index 0000000..caedce3 --- /dev/null +++ b/test/unit/softwares_block_test.rb @@ -0,0 +1,37 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/plugin_test_helper' + +class SoftwaresBlockTest < ActiveSupport::TestCase + include PluginTestHelper + should 'inherit from ProfileListBlock' do + assert_kind_of ProfileListBlock, SoftwaresBlock.new + end + + should 'declare its default title' do + SoftwaresBlock.any_instance.stubs(:profile_count).returns(0) + assert_not_equal ProfileListBlock.new.default_title, SoftwaresBlock.new.default_title + end + + should 'describe itself' do + assert_not_equal ProfileListBlock.description, SoftwaresBlock.description + end + + should 'give empty footer on unsupported owner type' do + block = SoftwaresBlock.new + block.expects(:owner).returns(1) + assert_equal '', block.footer + end + + should 'list softwares' do + user = create_person("Jose_Augusto", "jose_augusto@email.com", "aaaaaaa", "aaaaaaa", "jose_silva@email.com", "DF", "Gama") + + software_info = create_software_info("new software") + software_info.community.add_member(user) + + block = SoftwaresBlock.new + block.expects(:owner).at_least_once.returns(user) + + assert_equivalent [software_info.community], block.profiles + end + +end -- libgit2 0.21.2