Mercurial > hg > soundsoftware-site
annotate app/helpers/activities_helper.rb @ 1011:f44860e089c5 browsing
Refactor into activities helper
author | Chris Cannam <chris.cannam@soundsoftware.ac.uk> |
---|---|
date | Mon, 12 Nov 2012 14:38:17 +0000 |
parents | |
children | b98f60a6d231 |
rev | line source |
---|---|
chris@1011 | 1 |
chris@1011 | 2 module ActivitiesHelper |
chris@1011 | 3 |
chris@1011 | 4 def busy_projects(events, count) |
chris@1011 | 5 # Transform events list into hash from project id to number of |
chris@1011 | 6 # occurrences of project in list (there is surely a tidier way |
chris@1011 | 7 # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8) |
chris@1011 | 8 phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id } |
chris@1011 | 9 phash = phash.merge(phash) { |k,v| v.length } |
chris@1011 | 10 threshold = phash.values.sort.last(count).first |
chris@1011 | 11 busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count) |
chris@1011 | 12 busy.map { |pid| Project.find(pid) } |
chris@1011 | 13 end |
chris@1011 | 14 |
chris@1011 | 15 def busy_institutions(events, count) |
chris@1011 | 16 authors = events.map { |e| e.event_author unless !e.respond_to?(:event_author) }.compact |
chris@1011 | 17 institutions = authors.map { |a| a.ssamr_user_detail.institution_name } |
chris@1011 | 18 insthash = institutions.compact.sort.group_by { |i| i } |
chris@1011 | 19 insthash = insthash.merge(insthash) { |k,v| v.length } |
chris@1011 | 20 threshold = insthash.values.sort.last(count).first |
chris@1011 | 21 insthash.keys.select { |k| insthash[k] >= threshold }.sample(count) |
chris@1011 | 22 end |
chris@1011 | 23 |
chris@1011 | 24 end |