Revision 1015:52be96e83080 app/helpers

View differences:

app/helpers/activities_helper.rb
1

  
2
module ActivitiesHelper
3

  
4
  def busy_projects(events, count)
5
    # Transform events list into hash from project id to number of
6
    # occurrences of project in list (there is surely a tidier way
7
    # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8)
8
    phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id }
9
    phash = phash.merge(phash) { |k,v| v.length }
10
    threshold = phash.values.sort.last(count).first
11
    busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count)
12
    busy.map { |pid| Project.find(pid) }
13
  end
14

  
15
  def busy_institutions(events, count)
16
    authors = events.map do |e|
17
      e.event_author unless !e.respond_to?(:event_author) 
18
    end.compact
19
    institutions = authors.map do |a|
20
      if a.respond_to?(:ssamr_user_detail) and !a.ssamr_user_detail.nil?
21
        a.ssamr_user_detail.institution_name
22
      end
23
    end
24
    insthash = institutions.compact.sort.group_by { |i| i }
25
    insthash = insthash.merge(insthash) { |k,v| v.length }
26
    threshold = insthash.values.sort.last(count).first
27
    insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
28
  end
29

  
30
end

Also available in: Unified diff