You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by as...@apache.org on 2008/05/17 09:32:07 UTC

svn commit: r657304 - in /ode/sandbox/singleshot: app/models/activity.rb app/models/stakeholder.rb app/models/task.rb app/views/activities/index.html.erb db/schema.rb lib/tasks/database.rake

Author: assaf
Date: Sat May 17 00:32:07 2008
New Revision: 657304

URL: http://svn.apache.org/viewvc?rev=657304&view=rev
Log:
Improved the way activities are logged from the task and multiple actions combined for the same task/person.

Modified:
    ode/sandbox/singleshot/app/models/activity.rb
    ode/sandbox/singleshot/app/models/stakeholder.rb
    ode/sandbox/singleshot/app/models/task.rb
    ode/sandbox/singleshot/app/views/activities/index.html.erb
    ode/sandbox/singleshot/db/schema.rb
    ode/sandbox/singleshot/lib/tasks/database.rake

Modified: ode/sandbox/singleshot/app/models/activity.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/activity.rb?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/activity.rb (original)
+++ ode/sandbox/singleshot/app/models/activity.rb Sat May 17 00:32:07 2008
@@ -1,54 +1,43 @@
 class Activity < ActiveRecord::Base
 
-  class << self
-
-    def from_changes_to(task)
-      returning [] do |activities|
-        if task.changes['state']
-          from, to = *task.changes['state']
-          activities << new(:action=>'created the task', :person=>task.creator) if task.creator && from.nil? || from == 'reserved'
-          activities << new(:action=>'resumed the task') if from == 'suspended'
-          case to
-          when 'ready'
-          when 'active'
-            activities << new(:action=>'is now owner of the task', :person=>task.owner)
-          when 'suspended'
-            activities << new(:action=>'suspended the task')
-          when 'completed'
-            activities << new(:action=>'completed the task', :person=>task.owner)
-          when 'cancelled'
-            activities << new(:action=>'cancelled the task')
-          end
-        else
-          activities << new(:action=>'modified the task')
-        end
-      end
-    end
-
-  end
-
   belongs_to :person
   belongs_to :task
 
   attr_readonly :person, :task, :action
 
-  module GroupByDay
+  module Grouping
     def group_by_day
-      self.inject([]) { |days, activity|
+      self.inject([]) do |days, activity|
         created = activity.created_at.to_date
         day = days.last if days.last && days.last.first == created
         days << (day = [created, []]) unless day
         day.last << activity
         days
-      }
+      end
     end
   end
 
   named_scope :for_stakeholder,
     lambda { |person| { :joins=>'JOIN stakeholders AS involved ON involved.task_id=tasks.id',
       :conditions=>["involved.person_id=? AND involved.role != 'excluded'", person.id],
-      :include=>[:task, :person], :order=>'activities.created_at DESC', :extend=>GroupByDay } }
+      :include=>[:task, :person], :order=>'activities.created_at DESC', :extend=>Grouping } }
   named_scope :for_dates,
     lambda { |dates| { :conditions=>{ :created_at=>dates } } }
 
+  class << self
+
+    def log(task, modified_by)
+      activities = Hash.new
+      class << activities ; self ;end.send :define_method, :add do |*args|
+        person = Person === args.first ? args.shift : modified_by
+        self[person] = Array(self[person]).push(*args) if person
+      end
+      yield activities
+      activities.each do |person, actions|
+        task.activities.build :person=>person, :action=>actions.to_sentence
+      end
+    end
+
+  end
+
 end

Modified: ode/sandbox/singleshot/app/models/stakeholder.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/stakeholder.rb?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/stakeholder.rb (original)
+++ ode/sandbox/singleshot/app/models/stakeholder.rb Sat May 17 00:32:07 2008
@@ -93,7 +93,7 @@
       old_set = stakeholders.select { |sh| sh.role == role }
       stakeholders.delete old_set.reject { |sh| new_set.include?(sh.person) }
       (new_set - old_set.map(&:person)).each { |person| stakeholders.build :person=>person, :role=>role }
-      #changed_attributes[role] = old_set unless changed_attributes.has_key?(role)
+      changed_attributes[role] = old_set.first if SINGULAR_ROLES.include?(role.to_s) && !changed_attributes.has_key?(role)
     end
 
   end

Modified: ode/sandbox/singleshot/app/models/task.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/models/task.rb?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/models/task.rb (original)
+++ ode/sandbox/singleshot/app/models/task.rb Sat May 17 00:32:07 2008
@@ -180,22 +180,33 @@
 
   # --- Activities ---
  
-  has_many :activities, :include=>[:task, :person], :order=>'activities.created_at', :extend=>Activity::GroupByDay
+  has_many :activities, :include=>[:task, :person], :order=>'activities.created_at DESC', :extend=>Activity::Grouping
 
-  after_save do |task|
-    task.activities.push Activity.from_changes_to(task) unless task.state == 'reserved'
-  end
-
-  def save(person = nil)
-    super
-    activities.select(&:new_record?).each do |activity|
-      activity.task = self
-      activity.person ||= person
-      activity.save if activity.person
+  # Attribute recording person who created/modified this task.  Not persisted,
+  # but used to log activities associated with this task.
+  attr_accessor :modified_by
+
+  before_save :unless=>lambda { |task| task.state == 'reserved' } do |task|
+    Activity.log task, task.modified_by do |log|
+      if task.changes['state']
+        from, to = *task.changes['state']
+        log.add task.creator, 'created' if task.creator && (from.nil? || from == 'reserved')
+        log.add 'resumed' if from == 'suspended'
+        case to
+        when 'ready'
+          log.add task.changes['owner'].first, 'released' if from == 'active'
+        when 'active' then log.add task.owner, 'is owner of'
+        when 'suspended' then log.add 'suspended'
+        when 'completed' then log.add task.owner, 'completed'
+        when 'cancelled' then log.add 'cancelled'
+        end
+      else
+        log.add 'modified'
+      end
     end
   end
 
- 
+
   # --- Completion and cancellation ---
 
   validates_url :outcome_url, :if=>:outcome_url

Modified: ode/sandbox/singleshot/app/views/activities/index.html.erb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/app/views/activities/index.html.erb?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/app/views/activities/index.html.erb (original)
+++ ode/sandbox/singleshot/app/views/activities/index.html.erb Sat May 17 00:32:07 2008
@@ -6,7 +6,7 @@
       <% content_tag_for 'li', activity, :class=>'hentry entry-title' do %>
           <%= link_to h(activity.person.fullname), activity.person.identity %>
           <%= activity.action %>
-          <%= link_to h(truncate(activity.task.title, 100)), task_url(activity.task), :rel=>'bookmark' %>
+          the task <%= link_to h(truncate(activity.task.title, 100)), task_url(activity.task), :rel=>'bookmark' %>
         <% end %>
       <% end %>
     </li>

Modified: ode/sandbox/singleshot/db/schema.rb
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/db/schema.rb?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/db/schema.rb (original)
+++ ode/sandbox/singleshot/db/schema.rb Sat May 17 00:32:07 2008
@@ -30,10 +30,10 @@
     t.datetime "updated_at"
   end
 
-  add_index "people", ["access_key"], :name => "index_people_on_access_key", :unique => true
-  add_index "people", ["email"], :name => "index_people_on_email", :unique => true
-  add_index "people", ["fullname"], :name => "index_people_on_fullname"
   add_index "people", ["identity"], :name => "index_people_on_identity", :unique => true
+  add_index "people", ["fullname"], :name => "index_people_on_fullname"
+  add_index "people", ["email"], :name => "index_people_on_email", :unique => true
+  add_index "people", ["access_key"], :name => "index_people_on_access_key", :unique => true
 
   create_table "stakeholders", :force => true do |t|
     t.integer  "task_id",    :null => false
@@ -43,9 +43,9 @@
     t.datetime "updated_at"
   end
 
-  add_index "stakeholders", ["person_id", "role"], :name => "index_stakeholders_on_person_id_and_role"
-  add_index "stakeholders", ["task_id", "role"], :name => "index_stakeholders_on_task_id_and_role"
   add_index "stakeholders", ["task_id", "person_id", "role"], :name => "index_stakeholders_on_task_id_and_person_id_and_role", :unique => true
+  add_index "stakeholders", ["task_id", "role"], :name => "index_stakeholders_on_task_id_and_role"
+  add_index "stakeholders", ["person_id", "role"], :name => "index_stakeholders_on_person_id_and_role"
 
   create_table "tasks", :force => true do |t|
     t.string   "title",                                     :null => false

Modified: ode/sandbox/singleshot/lib/tasks/database.rake
URL: http://svn.apache.org/viewvc/ode/sandbox/singleshot/lib/tasks/database.rake?rev=657304&r1=657303&r2=657304&view=diff
==============================================================================
--- ode/sandbox/singleshot/lib/tasks/database.rake (original)
+++ ode/sandbox/singleshot/lib/tasks/database.rake Sat May 17 00:32:07 2008
@@ -18,11 +18,13 @@
     puts "Populating database for #{you.identity}"
     url = 'http://localhost:3001/sandwich'
     other = Person.identify('anon') || Person.create(:email=>'anon@apache.org')
+    Activity.delete_all
+    Stakeholder.delete_all
+    Task.delete_all
     create = lambda do |attributes|
       attributes = { :title=>Faker::Lorem.sentence, :description=>Faker::Lorem.paragraph,
-                     :frame_url=>url, :state=>'ready' }.merge(attributes || {})
-      task = Task.new(attributes)
-      task.save(you)
+                     :frame_url=>url, :state=>'ready', :modified_by=>you }.merge(attributes || {})
+      Task.create!(attributes)
     end
     #create = lambda { |attributes| { :title=>Faker::Lorem.sentence, :description=>Faker::Lorem.paragraph,
     #                                  :frame_url=>url, :state=>'ready' }.merge(attributes || {}) }
@@ -36,7 +38,7 @@
     # - observer
     # - admin
     create[:creator=>you]
-    create[:owner=>you]
+    create[:creator=>you, :owner=>you]
     create[:observers=>you]
     create[:admins=>you]
     # Tasks in which we are only or one of many potential owners.