communities_block.rb
1.21 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
require_dependency 'communities_block'
class CommunitiesBlock
def profile_list
result = get_visible_profiles
list_without_software_and_institution = []
result.each do |profile|
if profile.class == Community && !profile.software? && !profile.institution?
list_without_software_and_institution << profile
end
end
result = list_without_software_and_institution
result.slice(0..get_limit-1)
end
def profile_count
profile_list.count
end
private
def get_visible_profiles
visible_profiles = profiles.visible.includes(
[:image,:domains,:preferred_domain,:environment]
)
if !prioritize_profiles_with_image
return visible_profiles.all(
:limit => get_limit,
:order => 'profiles.updated_at DESC'
).sort_by {rand}
elsif profiles.visible.with_image.count >= get_limit
return visible_profiles.with_image.all(
:limit => get_limit * 5,
:order => 'profiles.updated_at DESC'
).sort_by {rand}
else
return 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
end
end