members_block.rb
851 Bytes
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
class MembersBlock < ProfileListBlock
def self.description
_('A block that displays members.')
end
def title
_('Members')
end
def footer
profile = self.owner
lambda do
link_to _('All members'), :profile => profile.identifier, :controller => 'profile', :action => 'members'
end
end
def profile_finder
@profile_finder ||= MembersBlock::Finder.new(self)
end
# Finds random members, up to the limit.
class Finder
def initialize(block)
@block = block
end
attr_reader :block
def find
ids = block.owner.members.map(&:id)
result = []
[block.limit, ids.size].min.times do
i = pick_random(ids.size)
result << Profile.find(ids[i])
ids.delete_at(i)
end
result
end
def pick_random(top)
rand(top)
end
end
end