Modified: branches/event_logging/app/controllers/application.rb (1921 => 1922)
--- branches/event_logging/app/controllers/application.rb 2008-11-04 12:42:14 UTC (rev 1921)
+++ branches/event_logging/app/controllers/application.rb 2008-11-04 15:41:33 UTC (rev 1922)
@@ -352,10 +352,10 @@
# =====================================
# Build a hash of every contributor to credit:
- # credited_contributors == { "UserID1" => ["User", ID1],
- # "UserID2" => ["User", ID2],
- # "NetworkID3" => ["User", ID3]
- # }
+ # new_credited_contributors == { "UserID1" => ["User", ID1],
+ # "UserID2" => ["User", ID2],
+ # "NetworkID3" => ["User", ID3]
+ # }
new_credited_contributors = {}
current_user_id = current_user.id
@@ -406,30 +406,54 @@
def update_attributions(attributable, params)
- # First delete old attributions:
- attributable.attributors.each do |a|
- a.userid_initiating_action = current_user.id
- a.destroy
- end
+ # =====================================
+ # Analyze new selection in the form
+ # =====================================
+
+ # Build a hash of every contributable to credit:
+ # new_attributed_items == { "WorkflowID1" => ["Workflow", ID1],
+ # "BlobID2" => ["Blob", ID2],
+ # "WorkflowID3" => ["Workflow", ID3]
+ # }
- # Then create new attributions:
+ new_attributed_items = {}
# Workflows
- attributor_workflow_ids = parse_comma_seperated_string(params[:attributions_workflows])
- attributor_type = 'Workflow'
- attributor_workflow_ids.each do |id|
- a = Attribution.new(:attributor_type => attributor_type, :attributor_id => id, :attributable_type => attributable.class.to_s, :attributable_id => attributable.id, :userid_initiating_action => current_user.id)
- a.save
+ workflow_ids = parse_comma_seperated_string(params[:attributions_workflows])
+ workflow_ids.each do |string_id|
+ id = string_id.to_i
+ new_attributed_items["Workflow" + string_id] = ["Workflow", id]
end
# Files
- attributor_file_ids = parse_comma_seperated_string(params[:attributions_files])
- attributor_type = 'Blob'
- attributor_file_ids.each do |id|
- a = Attribution.new(:attributor_type => attributor_type, :attributor_id => id, :attributable_type => attributable.class.to_s, :attributable_id => attributable.id, :userid_initiating_action => current_user.id)
- a.save
+ file_ids = parse_comma_seperated_string(params[:attributions_files])
+ file_ids.each do |string_id|
+ id = string_id.to_i
+ new_attributed_items["Blob" + string_id] = ["Blob", id]
end
+
+ # ============================
+ # Perform sync
+ # ============================
+
+ # Delete old attributions that were not ticked in the form
+ attributable.attributors.each do |a|
+ # if "ContributabletypeID" string combination not found in the new selection
+ # of attributed items, destroy the Attribution in the DB
+ unless new_attributed_items[a.attributor_type + a.attributor_id.to_s]
+ a.userid_initiating_action = current_user.id
+ a.destroy
+ end
+ end
+
+ # Create new attributions (if any selected ones are not yet present in the DB)
+ new_attributed_items.each do |item|
+ unless (Attribution.find(:first, :conditions => ["attributable_id = ? AND attributable_type = ? AND attributor_id = ? AND attributor_type = ?", attributable.id, attributable.class.to_s, item[1][1], item[1][0]]))
+ Attribution.create(:attributable_id => attributable.id, :attributable_type => attributable.class.to_s, :attributor_id => item[1][1], :attributor_type => item[1][0], :userid_initiating_action => current_user.id)
+ end
+ end
+
end
end