enterprises_block.rb
1.36 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
class EnterprisesBlock < ProfileListBlock
def default_title
n__('{#} enterprise', '{#} enterprises', profile_count)
end
def help
__('This block displays the enterprises where this user works.')
end
def self.description
__('Enterprises')
end
def footer
owner = self.owner
case owner
when Profile
lambda do
link_to s_('enterprises|View all'), :profile => owner.identifier, :controller => 'profile', :action => 'enterprises'
end
when Environment
lambda do
link_to s_('enterprises|View all'), :controller => 'search', :action => 'assets', :asset => 'enterprises'
end
else
''
end
end
def profile_count
if owner.kind_of?(Environment)
owner.enterprises.count(:conditions => { :visible => true })
else
owner.enterprises(:visible => true).count
end
end
def profile_finder
@profile_finder ||= EnterprisesBlock::Finder.new(self)
end
class Finder < ProfileListBlock::Finder
def ids
# FIXME when owner is an environment (i.e. listing enterprises globally
# this can become SLOW)
if block.owner.kind_of?(Environment)
block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => 'random()').map(&:id)
else
block.owner.enterprises.select(&:visible).map(&:id)
end
end
end
end