myexperiment-hackers
[Top][All Lists]
Advanced

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

[myexperiment-hackers] [1874] branches/event_logging/app/helpers/applica


From: noreply
Subject: [myexperiment-hackers] [1874] branches/event_logging/app/helpers/application_helper.rb: Work on news generation.
Date: Mon, 20 Oct 2008 06:44:17 -0400 (EDT)

Revision
1874
Author
alekses6
Date
2008-10-20 06:44:16 -0400 (Mon, 20 Oct 2008)

Log Message

Work on news generation. More object existence check code added.

Modified Paths

Diff

Modified: branches/event_logging/app/helpers/application_helper.rb (1873 => 1874)


--- branches/event_logging/app/helpers/application_helper.rb	2008-10-18 20:54:52 UTC (rev 1873)
+++ branches/event_logging/app/helpers/application_helper.rb	2008-10-20 10:44:16 UTC (rev 1874)
@@ -1458,7 +1458,7 @@
     
     # produce news from event list 
     rtn = []
-    news_item = nil
+    news_item = nil # contributo_news_from_log!() will return NIL if event is not to be added to the feed for the current user
     events.each do |e|
       news_item = contributor_news_from_log!(e)
       rtn << news_item unless news_item.nil?
@@ -1469,25 +1469,29 @@
   
   
   def contributor_news_from_log!(log_entry)
-    rtn = []
+    rtn = [] # despite this, NIL will be returned on errors / when news entry not to be shown for current user
     action = ""
     loggable_type = log_entry.activity_loggable_type
     timestamp = log_entry.created_at
     
-    # TODO: add basic check that culprit exists
+    
+    # basic check that a "culprit" still exists
+    # (further checks for other related objects are carried out in the respective sections for every ActivityLoggable type)
     unless ["Membership", "User"].include? loggable_type.to_s
       # for all "activity_loggables" apart from those above, the culprit is the "user";
       # if "name" helper return 'nil' - this means that the user not found
       # -> news item not to be shown then
       culprit_link = name(log_entry.culprit_id)
-      return rtn if culprit_link.nil?
+      return nil if culprit_link.nil?
     end
     
+    
     case loggable_type.to_s
       when "Workflow", "Blob", "Pack"
         case action
           when "create", "update"
             begin
+              # check if contributable can still be found
               object, object_path = evaluate_object_instance_and_path(log_entry.activity_loggable_type, log_entry.activity_loggable_id)
               object_visible_name = contributable_name_from_instance(object)
               
@@ -1498,54 +1502,71 @@
         end
       
       when "Membership"
-        # this membership record contains ID of the user who has performed the "accept" action;
-        # was that the new member or the "network" (i.e. the network admin - currently networks have only 1 admin)
-        if log_entry.culprit_type.downcase == "user"
-          user_id = log_entry.culprit_id
-          network_id = log_entry.referenced_id
-        else
-          user_id = log_entry.referenced_id
-          network_id = log_entry.culprit_id
-        end
-        
-        case action 
-          when "invite"
-            rtn << [timestamp, "#{name(user_id)} was invited to join the #{title(network_id)} Group."]
-          when "request"
-            rtn << [timestamp, "#{name(user_id)} requested to join the #{title(network_id)} Group."]
-          when "accept"
-            rtn << [timestamp, "#{name(user_id)} joined the #{title(network_id)} Group."]
-          when "reject"
-            if log_entry.culprit_type == "User"
-              rtn << [timestamp, "#{name(user_id)} rejected invitation to join the #{title(network_id)} Group."]
-            else
-              rtn << [timestamp, "#{name(user_id)}'s request to join the #{title(network_id)} Group was rejected."]
+        begin
+          # this membership record contains ID of the user who has performed the "accept" action;
+          # was that the new member or the "network" (i.e. the network admin - currently networks have only 1 admin)
+          if log_entry.culprit_type.downcase == "user"
+            user_id = log_entry.culprit_id
+            network_id = log_entry.referenced_id
+          else
+            user_id = log_entry.referenced_id
+            network_id = log_entry.culprit_id
+          end
+          
+          # check that both user & network still exist
+          # (get the name and the title if they do; exit otherwise)
+          user = User.find(user_id)
+          network = Network.find(network_id)
+          
+          # if no exception at this point, can generate the news item:
+          # (name() and title() will accept instance instead of ID of the object - to save an extra DB access)
+          
+          case action 
+            when "invite"
+              rtn << [timestamp, "#{name(user)} was invited to join the #{title(network)} Group."]
+            when "request"
+              rtn << [timestamp, "#{name(user)} requested to join the #{title(network)} Group."]
+            when "accept"
+              rtn << [timestamp, "#{name(user)} joined the #{title(network)} Group."]
+            when "reject"
+              if log_entry.culprit_type == "User"
+                rtn << [timestamp, "#{name(user)} rejected invitation to join the #{title(network)} Group."]
+              else
+                rtn << [timestamp, "#{name(user)}'s request to join the #{title(network)} Group was rejected."]
+              end
+            when "destroy"
+              if log_entry.culprit_type == "User"
+                rtn << [timestamp, "#{name(user)} has left the #{title(network)} Group."]
+              else
+                rtn << [timestamp, "#{name(user)} was removed from the list of members of the #{title(network)} Group."]
             end
-          when "destroy"
-            if log_entry.culprit_type == "User"
-              rtn << [timestamp, "#{name(user_id)} has left the #{title(network_id)} Group."]
-            else
-              rtn << [timestamp, "#{name(user_id)} was removed from the list of members of the #{title(network_id)} Group."]
-            end
+          end
+        rescue ActiveRecord::RecordNotFound
+          # do nothing, but don't display the news entry for missing group / member
         end
       
       when "Friendship"
+        # check that "referenced" user still exists
+        referenced_user = User.find(log_entry.referenced_id)
+        
+        # seems to be existent, as no exception thrown;
+        # name() will accept instance of the User to save an extra DB access
         case action 
           when "create"
-            rtn << [timestamp, "#{culprit_link} requested friendship with #{name(log_entry.referenced_id)}."]
+            rtn << [timestamp, "#{culprit_link} requested friendship with #{name(referenced_user)}."]
           when "accept"
-            rtn << [timestamp, "#{culprit_link} and #{name(log_entry.referenced_id)} became friends."]
+            rtn << [timestamp, "#{culprit_link} and #{name(referenced_user)} became friends."]
           when "reject"
-            rtn << [timestamp, "#{culprit_link} rejected a friendship request from #{name(log_entry.referenced_id)}."]
+            rtn << [timestamp, "#{culprit_link} rejected a friendship request from #{name(referenced_user)}."]
           when "destroy"
-            rtn << [timestamp, "#{culprit_link} removed #{name(log_entry.referenced_id)} from their friends list."]
+            rtn << [timestamp, "#{culprit_link} removed #{name(referenced_user)} from their friends list."]
         end
         
       when "Network"
         if action == "create"
           begin
             network = Network.find(log_entry.activity_loggable_id)
-            rtn << [timestamp, "#{culprit_link} created the #{link_to(h(network.title), group_path(network))} Group."]
+            rtn << [timestamp, "#{culprit_link} created the #{title(network)} Group."]
           rescue ActiveRecord::RecordNotFound
              # do nothing, but don't display the news entry for missing group
           end
@@ -1557,7 +1578,8 @@
             ann = GroupAnnouncement.find(log_entry.activity_loggable_id)
             group = Network.find(log_entry.referenced_id)
             
-            rtn << [timestamp, "#{culprit_link} made an announcement \"#{link_to ann.title, group_announcement_path(log_entry.referenced_id, ann.id)}\" for group #{link_to group.title, group_path(group)}."]
+            # title can't be blank for group announcements
+            rtn << [timestamp, "#{culprit_link} made an announcement \"#{link_to ann.title, group_announcement_path(log_entry.referenced_id, ann.id)}\" for group #{title(group)}."]
           rescue ActiveRecord::RecordNotFound
             # do nothing, but don't display the news entry for missing group announcement / group
           end
@@ -1698,8 +1720,11 @@
               case log_entry.referenced_type
                 when "User"
                   credited_whom = name(log_entry.referenced_id)
+                  return nil if credited_whom.nil?
                 when "Network"
-                  credited_whom = "the #{title(log_entry.referenced_id)} Group"
+                  network_title = title(log_entry.referenced_id)
+                  return nil if network_title.nil?
+                  credited_whom = "the #{network_title} Group"
                 else
                   credited_whom = "(#{log_entry.referenced_type},#{log_entry.referenced_id})"
               end
@@ -1741,21 +1766,52 @@
         end
       
       when "PictureSelection"
+        # TODO: how to check that picture selection is still valid?
         rtn << [timestamp, "#{culprit_link} selected a new profile picture #{link_to image_tag(avatar_url(log_entry.referenced_id, 50)), user_path(log_entry.culprit_id)}."]
+      
+      when "Profile"
+        case action
+          # in the news we're only interested in "update" action for profiles,
+          # as these are created / deleted along with the user account
+          when "update"
+            rtn << [timestamp, "#{culprit_link} has updated their #{link_to "profile", user_path(log_entry.culprit_id)}"]
+        end
         
       when "User"
         case action
           when "activate"
             rtn << [timestamp, "#{name(log_entry.activity_loggable.id)} joined #{link_to "myExperiment", "/"}."]
           when "update"
-            rtn << [timestamp, "#{name(log_entry.activity_loggable.id)} updated their #{link_to "account", user_path(log_entry.activity_loggable.id)}."]
+            rtn << [timestamp, "#{name(log_entry.activity_loggable.id)} updated their #{link_to "account", user_path(log_entry.activity_loggable.id)} settings."]
         end
       
-      else
-        return rtn[0]
+      when "Announcement"
+        if action == "create"
+          begin
+            # site announcement
+            ann = Announcement.find(log_entry.activity_loggable_id)
+            ann_path = announcement_path(ann.id)
+            if ann.title.blank?
+              ann_link = ann_path
+              ann_title_link = ""
+            else
+              ann_link = nil
+              ann_title_link = ": \"" + link_to(ann.title, ann_path) + "\""
+            end
+            
+            rtn << [timestamp, "#{culprit_link} made a #{link_to_unless ann_link.nil?, "site announcement", ann_link}#{ann_title_link}."]
+          rescue ActiveRecord::RecordNotFound
+            # do nothing, but don't display the news entry for missing site announcement
+          end
+        end
+      
+    else
+      # for any ActivityLoggable type that we are not showing in the news, return NIL
+      # (because "rtn" is initially set to []) 
+      return rtn[0]
     end
     
-    
+    # NIL
     return rtn[0]
     
   end

reply via email to

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