softwares_block.rb
2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class SoftwaresBlock < CommunitiesBlock
settings_items :software_type
attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id, :resource_type, :software_type
def self.description
_('Softwares')
end
def default_title
if self.software_type == "Generic"
return n_('{#} generic software', '{#} generic softwares', profile_count)
elsif self.software_type == "Public"
return n_('{#} public software', '{#} public softwares', profile_count)
else
return n_('{#} software', '{#} softwares', profile_count)
end
end
def help
_('This block displays the softwares in which the user is a member.')
end
def footer
self.software_type ||= "All"
owner = self.owner
case owner
when Profile
lambda do |context|
link_to s_('softwares|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'communities', :type => 'Software'
end
when Environment
lambda do |context|
link_to s_('softwares|View all'), :controller => 'search', :action => 'communities', :type => 'Software'
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 = []
result.each do |profile|
if profile.class == Community and profile.software?
if self.software_type == "Public"
list_with_software << profile if profile.software_info.public_software?
elsif self.software_type == "Generic"
list_with_software << profile if !profile.software_info.public_software?
else
list_with_software << profile
end
end
end
result = list_with_software
result.slice(0..get_limit-1)
end
end