myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [3100] branches/events: adding event model and he


From: noreply
Subject: [myexperiment-hackers] [3100] branches/events: adding event model and helper function
Date: Thu, 23 Aug 2012 13:33:22 +0000 (UTC)

Revision
3100
Author
dgc
Date
2012-08-23 13:33:21 +0000 (Thu, 23 Aug 2012)

Log Message

adding event model and helper function

Modified Paths

Added Paths

Diff

Modified: branches/events/app/helpers/application_helper.rb (3099 => 3100)


--- branches/events/app/helpers/application_helper.rb	2012-08-21 22:45:26 UTC (rev 3099)
+++ branches/events/app/helpers/application_helper.rb	2012-08-23 13:33:21 UTC (rev 3100)
@@ -544,6 +544,95 @@
     return "#{issn[0..3]}-#{issn[4..7]}"
   end
   
+
+  def activity_feed(opts = {})
+
+    sentences = {
+      "register"        => "$S joined #{Conf.sitename}.",
+      "create Workflow" => "$S uploaded $O.",
+      "create Blob"     => "$S uploaded $O.",
+      "create Pack"     => "$S created $O.",
+      "create Blog"     => "$S created $O."
+    }
+
+    conditions_expr     = []
+    conditions_operands = []
+
+    if opts[:subject]
+      conditions_expr     << "subject_type = ? AND subject_id = ?"
+      conditions_operands << opts[:subject].class.name
+      conditions_operands << opts[:subject].id
+    end
+
+    if opts[:object]
+      conditions_expr     << "objekt_type = ? AND objekt_id = ?"
+      conditions_operands << opts[:object].class.name
+      conditions_operands << opts[:object].id
+    end
+
+    unless conditions_expr.empty?
+      conditions_expr = conditions_expr.map do |cond| "(#{cond})" end.join(' AND ')
+      conditions = [conditions_expr] + conditions_operands
+    end
+
+    events = Event.find(:all, :order => 'created_at ASC', :conditions => conditions, :limit => 15)
+
+    markup = '<ol class="activity-feed">'
+
+    markup << events.map do |event|
+
+      if event.objekt
+        sentence = sentences["#{event.action} #{event.objekt_type}"]
+      else
+        sentence = sentences["#{event.action}"]
+      end
+
+      next unless sentence
+
+      if sentence.include?("$S")
+
+        lhs_url = send("#{Object.const_get(event.subject_type).table_name.singularize}_path".to_sym,
+              :id => event.subject_id)
+
+        if lhs_url == request.path
+          lhs = h(event.subject_label)
+        else
+          lhs = link_to(event.subject_label, lhs_url)
+        end
+
+        sentence = sentence.sub("$S", lhs)
+      end
+
+      if sentence.include?("$O")
+
+        if event.objekt_id
+
+          rhs_url = send("#{event.objekt_type.constantize.table_name.singularize}_path".to_sym,
+                :id => event.objekt_id)
+        end
+
+        if rhs_url == request.path
+          rhs = h(event.objekt_label)
+        else
+          rhs = link_to(event.objekt_label, rhs_url)
+        end
+
+        sentence = sentence.sub("$O", rhs)
+      end
+
+      if sentence.include?("$E")
+        sentence = sentence.sub("$E", event.extra)
+      end
+
+      "<li>#{sentence}</li>"
+
+    end.join
+
+    markup << '</ol>'
+
+    markup
+  end
+
   def news(contributor, restrict_contributor=true, before=Time.now, after=Time.now-1.week, limit=30)
     hash = {}
     

Added: branches/events/app/models/event.rb (0 => 3100)


--- branches/events/app/models/event.rb	                        (rev 0)
+++ branches/events/app/models/event.rb	2012-08-23 13:33:21 UTC (rev 3100)
@@ -0,0 +1,30 @@
+# myExperiment: app/models/event.rb
+#
+# Copyright (c) 2012 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class Event < ActiveRecord::Base
+
+  belongs_to :subject, :polymorphic => true
+  belongs_to :objekt,  :polymorphic => true
+
+  validates_presence_of :subject
+  validates_presence_of :action
+  validates_presence_of :subject_label
+  
+  before_validation do |e|
+
+    if e.subject && e.subject_label.nil?
+      e.subject_label = e.subject.label if e.subject.respond_to?(:label)
+      e.subject_label = e.subject.title if e.subject.respond_to?(:title)
+      e.subject_label = e.subject.name  if e.subject.respond_to?(:name)
+    end
+
+    if e.objekt && e.objekt_label.nil?
+      e.objekt_label = e.objekt.label if e.objekt.respond_to?(:label)
+      e.objekt_label = e.objekt.title if e.objekt.respond_to?(:title)
+      e.objekt_label = e.objekt.name  if e.objekt.respond_to?(:name)
+    end
+  end
+end
+

Modified: branches/events/app/models/user.rb (3099 => 3100)


--- branches/events/app/models/user.rb	2012-08-21 22:45:26 UTC (rev 3099)
+++ branches/events/app/models/user.rb	2012-08-23 13:33:21 UTC (rev 3100)
@@ -17,6 +17,8 @@
   
   has_many :jobs
 
+  has_many :events, :as => :subject, :dependent => :destroy
+
   has_many :taverna_enactors, :as => :contributor,
               :conditions => ["contributor_type = ?", "User"]
 

Added: branches/events/db/migrate/097_add_events.rb (0 => 3100)


--- branches/events/db/migrate/097_add_events.rb	                        (rev 0)
+++ branches/events/db/migrate/097_add_events.rb	2012-08-23 13:33:21 UTC (rev 3100)
@@ -0,0 +1,29 @@
+# myExperiment: db/migrate/097_add_events.rb
+#
+# Copyright (c) 2012 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class AddEvents < ActiveRecord::Migration
+  def self.up
+    create_table :events do |t|
+
+      t.string  :subject_type
+      t.integer :subject_id
+      t.string  :subject_label
+
+      t.string  :action
+
+      t.string  :objekt_type
+      t.integer :objekt_id
+      t.string  :objekt_label
+
+      t.string  :extra
+
+      t.datetime :created_at
+    end
+  end
+
+  def self.down
+    drop_table :events
+  end
+end

Modified: branches/events/db/schema.rb (3099 => 3100)


--- branches/events/db/schema.rb	2012-08-21 22:45:26 UTC (rev 3099)
+++ branches/events/db/schema.rb	2012-08-23 13:33:21 UTC (rev 3100)
@@ -9,7 +9,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 96) do
+ActiveRecord::Schema.define(:version => 97) do
 
   create_table "activity_limits", :force => true do |t|
     t.string   "contributor_type", :null => false
@@ -227,6 +227,18 @@
 
   add_index "downloads", ["contribution_id"], :name => "index_downloads_on_contribution_id"
 
+  create_table "events", :force => true do |t|
+    t.string   "subject_type"
+    t.integer  "subject_id"
+    t.string   "subject_label"
+    t.string   "action"
+    t.string   "objekt_type"
+    t.integer  "objekt_id"
+    t.string   "objekt_label"
+    t.string   "extra"
+    t.datetime "created_at"
+  end
+
   create_table "experiments", :force => true do |t|
     t.string   "title"
     t.text     "description"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]