myexperiment-hackers
[Top][All Lists]
Advanced

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

[myexperiment-hackers] [1919] branches/event_logging/app: Contributable


From: noreply
Subject: [myexperiment-hackers] [1919] branches/event_logging/app: Contributable update screen - work on permission sync ( done - implemented for user permissions; fixed for group permissions), attribution and creditation sync (just started).
Date: Mon, 3 Nov 2008 20:10:51 -0500 (EST)

Revision
1919
Author
alekses6
Date
2008-11-03 20:10:50 -0500 (Mon, 03 Nov 2008)

Log Message

Contributable update screen - work on permission sync (done - implemented for user permissions; fixed for group permissions), attribution and creditation sync (just started).

Modified Paths

Diff

Modified: branches/event_logging/app/controllers/application.rb (1918 => 1919)


--- branches/event_logging/app/controllers/application.rb	2008-11-03 16:06:49 UTC (rev 1918)
+++ branches/event_logging/app/controllers/application.rb	2008-11-04 01:10:50 UTC (rev 1919)
@@ -188,15 +188,28 @@
 
     if updating_class == "5"
       if params[:updating_somefriends]
-        # Delete old User permissions
-        policy.delete_all_user_permissions(current_user.id)
+        # Delete any user Permissions that were not checked in the form
+        policy.permissions.each do |p|
+          # if this is a user permission and the ID of a user to which it is assigned is not present
+          # in 'updating_somefriends' hash, delete this permission
+          if p.contributor_type == "User" && params[:updating_somefriends][p.contributor_id.to_s].nil?
+            p.userid_initiating_action = current_user.id
+            p.destroy
+          end
+        end
         
-        # Now create new User permissions, if required
+        # Now create new User permissions, if any new selections were made;
+        # in 'updating_somefriends' hash all present value pairs (which are "user_id" => "user_id")
+        # represent active selections -> so eventually there should be permission for every user with listed ID
         params[:updating_somefriends].each do |f|
-          Permission.new(:policy => policy,
-              :contributor => (User.find f[1].to_i),
-              :view => 1, :download => 1, :edit => 1, 
-              :userid_initiating_action => current_user.id).save
+          # only create new Permission if it doesn't already exist
+          friend_id = f[0].to_i 
+          unless (Permission.find(:first, :conditions => ["policy_id = ? AND contributor_type = ? AND contributor_id = ?", policy.id, 'User', friend_id]))
+            Permission.create(:policy => policy,
+                              :contributor => (User.find f[1].to_i),
+                              :view => 1, :download => 1, :edit => 1, 
+                              :userid_initiating_action => current_user.id)
+          end
         end
       else # none of the 'some of my friends' were selected, error
         # revert changes made to policy (however any permissions updated will preserve the state)
@@ -236,16 +249,18 @@
         if n[1][:id]
           
           n_id = n[1][:id].to_i
-          level = n[1][:level]
+          level = n[1][:level].to_i
           
           unless (perm = Permission.find(:first, :conditions => ["policy_id = ? AND contributor_type = ? AND contributor_id = ?", policy.id, 'Network', n_id]))
             # Only create new Permission if it doesn't already exist
             p = Permission.new(:policy => policy, :contributor => (Network.find(n_id)), :userid_initiating_action => current_user.id)
             p.set_level!(level) if level
-          else
-            # Update the 'level' on the existing permission
-            perm.userid_initiating_action = current_user.id
-            perm.set_level!(level) if level
+          else 
+            # Update the 'level' on the existing permission if it has changed
+            if perm.level() != level
+              perm.userid_initiating_action = current_user.id
+              perm.set_level!(level) if level
+            end
           end
           
         else
@@ -332,13 +347,47 @@
 
   def update_credits(creditable, params)
     
-    # First delete old creditations:
+    # ===============================================================
+    # Build an array of every contributor to credit
+    # - credited_contributors == ["UserID1", "UserID2", "NetworkID3"]
+    # ===============================================================
+    new_credited_contributors = []
+    current_user_id = current_user.id
+    
+    # Current user
+    if (params[:credits_me].downcase == 'true')
+      new_credited_contributors << "User#{current_user_id}"
+    end
+    
+    # Friends + other users
+    user_ids = parse_comma_seperated_string(params[:credits_users])
+    user_ids.each do |string_id|
+      new_credited_contributors << ("User" + string_id)
+    end
+    
+    # Networks (aka Groups)
+    network_ids = parse_comma_seperated_string(params[:credits_groups])
+    network_ids.each do |string_id|
+      new_credited_contributors << ("Network" + string_id)
+    end
+    
+    
+    # ==========================================================
+    #  Delete old creditations that were not ticked in the form
+    # ==========================================================
     creditable.creditors.each do |c|
-      c.userid_initiating_action = current_user.id
-      c.destroy
+      # if "ContributortypeID" string combination not found in the new selection
+      # of credited contributors, destroy the Creditation in the DB
+      unless new_credited_contributors.include? (c.creditor_type + c.creditor_id.to_s)
+        c.userid_initiating_action = current_user.id
+        c.destroy
+      end
     end
     
-    # Then create new creditations:
+    # ==========================================================
+    #  Create new creditations (if any new ones are not yet
+    #                 present in the DB)
+    # ==========================================================
     
     # Current user
     if (params[:credits_me].downcase == 'true')

Modified: branches/event_logging/app/models/permission.rb (1918 => 1919)


--- branches/event_logging/app/models/permission.rb	2008-11-03 16:06:49 UTC (rev 1918)
+++ branches/event_logging/app/models/permission.rb	2008-11-04 01:10:50 UTC (rev 1919)
@@ -60,17 +60,17 @@
   
   def set_level!(lvl)
     case lvl
-    when "2"
+    when 2
       self.view = true
       self.download = true
       self.edit = true
       self.save
-    when "1"
+    when 1
       self.view = true
       self.download = true
       self.edit = false
       self.save
-    when "0"
+    when 0
       self.view = true
       self.download = false
       self.edit = false

reply via email to

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