changeset 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 76a677c96bce
children bbca6f4eebc7 76cce7d14bd0
files app/controllers/projects_controller.rb app/helpers/activities_helper.rb app/views/activities/_busy.html.erb app/views/activities/_busy_institution.html.erb
diffstat 4 files changed, 28 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/projects_controller.rb	Mon Nov 12 14:22:39 2012 +0000
+++ b/app/controllers/projects_controller.rb	Mon Nov 12 14:38:17 2012 +0000
@@ -43,6 +43,8 @@
   helper :repositories
   include RepositoriesHelper
   include ProjectsHelper
+  include ActivitiesHelper
+  helper :activities
 
   # Lists visible projects. Paginator is for top-level projects only
   # (subprojects belong to them)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/helpers/activities_helper.rb	Mon Nov 12 14:38:17 2012 +0000
@@ -0,0 +1,24 @@
+
+module ActivitiesHelper
+
+  def busy_projects(events, count)
+    # Transform events list into hash from project id to number of
+    # occurrences of project in list (there is surely a tidier way
+    # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8)
+    phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id }
+    phash = phash.merge(phash) { |k,v| v.length }
+    threshold = phash.values.sort.last(count).first
+    busy = phash.keys.select { |k| phash[k] >= threshold }.sample(count)
+    busy.map { |pid| Project.find(pid) }
+  end
+
+  def busy_institutions(events, count)
+    authors = events.map { |e| e.event_author unless !e.respond_to?(:event_author) }.compact
+    institutions = authors.map { |a| a.ssamr_user_detail.institution_name }
+    insthash = institutions.compact.sort.group_by { |i| i }
+    insthash = insthash.merge(insthash) { |k,v| v.length }
+    threshold = insthash.values.sort.last(count).first
+    insthash.keys.select { |k| insthash[k] >= threshold }.sample(count)
+  end
+
+end
--- a/app/views/activities/_busy.html.erb	Mon Nov 12 14:22:39 2012 +0000
+++ b/app/views/activities/_busy.html.erb	Mon Nov 12 14:38:17 2012 +0000
@@ -1,5 +1,4 @@
 <% events = @events_by_day %>
-<% max = 5 %>
 <% if (events.nil?) 
      activity = Redmine::Activity::Fetcher.new(User.current)
      events = activity.events(Date.today - 14, Date.today + 1)
@@ -13,16 +12,7 @@
    <ul>
 
    <% 
-      # Transform events list into hash from project id to number of
-      # occurrences of project in list (there is surely a tidier way
-      # to do this, e.g. chunk() in Ruby 1.9 but not in 1.8)
-      phash = events.map { |e| e.project unless !e.respond_to?(:project) }.sort.group_by { |p| p.id }
-      phash = phash.merge(phash) { |k,v| v.length }
-      threshold = phash.values.sort.last(max).first
-      busy = phash.keys.select { |k| phash[k] >= threshold }.sample(max)
-
-      for id in busy
-        project = Project.find(id)
+      for project in busy_projects(events, 5)
    %>
 
    <li class="busy">
--- a/app/views/activities/_busy_institution.html.erb	Mon Nov 12 14:22:39 2012 +0000
+++ b/app/views/activities/_busy_institution.html.erb	Mon Nov 12 14:38:17 2012 +0000
@@ -1,5 +1,4 @@
 <% events = @events_by_day %>
-<% max = 5 %>
 <% if (events.nil?) 
      activity = Redmine::Activity::Fetcher.new(User.current)
      events = activity.events(Date.today - 14, Date.today + 1)
@@ -13,14 +12,7 @@
    <ul>
 
    <% 
-      authors = events.map { |e| e.event_author unless !e.respond_to?(:event_author) }.compact
-      institutions = authors.map { |a| a.ssamr_user_detail.institution_name }
-      insthash = institutions.compact.sort.group_by { |i| i }
-      insthash = insthash.merge(insthash) { |k,v| v.length }
-      threshold = insthash.values.sort.last(max).first
-      busy = insthash.keys.select { |k| insthash[k] >= threshold }.sample(max)
-
-      for institution in busy
+      for institution in busy_institutions(events, 5)
    %>
 
    <li class="busy">