Commit a7a813a68df35fd1dd60eabe711b3d7b15253e29
Committed by
Luciano Prestes
1 parent
1657cdb0
Exists in
master
and in
79 other branches
Add Softwares Block and tests
Signed-off-by: Gabriela Navarro <navarro1703@gmail.com> Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Showing
3 changed files
with
107 additions
and
0 deletions
Show diff stats
lib/mpog_software_plugin.rb
... | ... | @@ -0,0 +1,64 @@ |
1 | +class SoftwaresBlock < CommunitiesBlock | |
2 | + | |
3 | + attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id, :resource_type | |
4 | + | |
5 | + def self.description | |
6 | + _('Softwares') | |
7 | + end | |
8 | + | |
9 | + def default_title | |
10 | + n_('{#} software', '{#} softwares', profile_count) | |
11 | + end | |
12 | + | |
13 | + def help | |
14 | + _('This block displays the softwares in which the user is a member.') | |
15 | + end | |
16 | + | |
17 | + def footer | |
18 | + owner = self.owner | |
19 | + case owner | |
20 | + when Profile | |
21 | + lambda do |context| | |
22 | + link_to s_('softwares|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities' | |
23 | + end | |
24 | + when Environment | |
25 | + lambda do |context| | |
26 | + link_to s_('softwares|View all'), :controller => 'search', :action => 'communities' | |
27 | + end | |
28 | + else | |
29 | + '' | |
30 | + end | |
31 | + end | |
32 | + | |
33 | + def profile_count | |
34 | + profile_list.count | |
35 | + end | |
36 | + | |
37 | + def profiles | |
38 | + owner.communities | |
39 | + end | |
40 | + | |
41 | + def profile_list | |
42 | + result = nil | |
43 | + visible_profiles = profiles.visible.includes([:image,:domains,:preferred_domain,:environment]) | |
44 | + if !prioritize_profiles_with_image | |
45 | + result = visible_profiles.all(:limit => get_limit, :order => 'profiles.updated_at DESC').sort_by{ rand } | |
46 | + elsif profiles.visible.with_image.count >= get_limit | |
47 | + result = visible_profiles.with_image.all(:limit => get_limit * 5, :order => 'profiles.updated_at DESC').sort_by{ rand } | |
48 | + else | |
49 | + 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 } | |
50 | + end | |
51 | + | |
52 | + list_with_software = [] | |
53 | + | |
54 | + visible_profiles.each do |p| | |
55 | + if p.class == Community and p.software? and !p.institution? | |
56 | + list_with_software << p | |
57 | + end | |
58 | + end | |
59 | + | |
60 | + result = list_with_software | |
61 | + | |
62 | + result.slice(0..get_limit-1) | |
63 | + end | |
64 | +end | ... | ... |
... | ... | @@ -0,0 +1,37 @@ |
1 | +require File.dirname(__FILE__) + '/../../../../test/test_helper' | |
2 | +require File.dirname(__FILE__) + '/plugin_test_helper' | |
3 | + | |
4 | +class SoftwaresBlockTest < ActiveSupport::TestCase | |
5 | + include PluginTestHelper | |
6 | + should 'inherit from ProfileListBlock' do | |
7 | + assert_kind_of ProfileListBlock, SoftwaresBlock.new | |
8 | + end | |
9 | + | |
10 | + should 'declare its default title' do | |
11 | + SoftwaresBlock.any_instance.stubs(:profile_count).returns(0) | |
12 | + assert_not_equal ProfileListBlock.new.default_title, SoftwaresBlock.new.default_title | |
13 | + end | |
14 | + | |
15 | + should 'describe itself' do | |
16 | + assert_not_equal ProfileListBlock.description, SoftwaresBlock.description | |
17 | + end | |
18 | + | |
19 | + should 'give empty footer on unsupported owner type' do | |
20 | + block = SoftwaresBlock.new | |
21 | + block.expects(:owner).returns(1) | |
22 | + assert_equal '', block.footer | |
23 | + end | |
24 | + | |
25 | + should 'list softwares' do | |
26 | + user = create_person("Jose_Augusto", "jose_augusto@email.com", "aaaaaaa", "aaaaaaa", "jose_silva@email.com", "DF", "Gama") | |
27 | + | |
28 | + software_info = create_software_info("new software") | |
29 | + software_info.community.add_member(user) | |
30 | + | |
31 | + block = SoftwaresBlock.new | |
32 | + block.expects(:owner).at_least_once.returns(user) | |
33 | + | |
34 | + assert_equivalent [software_info.community], block.profiles | |
35 | + end | |
36 | + | |
37 | +end | ... | ... |