Modified: branches/event_logging/app/controllers/application.rb (1920 => 1921)
--- branches/event_logging/app/controllers/application.rb 2008-11-04 11:24:09 UTC (rev 1920)
+++ branches/event_logging/app/controllers/application.rb 2008-11-04 12:42:14 UTC (rev 1921)
@@ -347,70 +347,63 @@
def update_credits(creditable, params)
- # ===============================================================
- # Build an array of every contributor to credit
- # - credited_contributors == ["UserID1", "UserID2", "NetworkID3"]
- # ===============================================================
- new_credited_contributors = []
+ # =====================================
+ # Analyze new selection in the form
+ # =====================================
+
+ # Build a hash of every contributor to credit:
+ # credited_contributors == { "UserID1" => ["User", ID1],
+ # "UserID2" => ["User", ID2],
+ # "NetworkID3" => ["User", ID3]
+ # }
+
+ new_credited_contributors = {}
current_user_id = current_user.id
# Current user
if (params[:credits_me].downcase == 'true')
- new_credited_contributors << "User#{current_user_id}"
+ new_credited_contributors["User#{current_user_id}"] = ["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)
+ id = string_id.to_i
+ new_credited_contributors["User" + string_id] = ["User", 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)
+ id = string_id.to_i
+ new_credited_contributors["Network" + string_id] = ["Network", id]
end
- # ==========================================================
+ # ============================
+ # Perform sync
+ # ============================
+
# Delete old creditations that were not ticked in the form
- # ==========================================================
creditable.creditors.each do |c|
# 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)
+ unless new_credited_contributors[c.creditor_type + c.creditor_id.to_s]
c.userid_initiating_action = current_user.id
c.destroy
end
end
-
- # ==========================================================
- # Create new creditations (if any new ones are not yet
- # present in the DB)
- # ==========================================================
-
- # Current user
- if (params[:credits_me].downcase == 'true')
- c = Creditation.new(:creditor_type => 'User', :creditor_id => current_user.id, :creditable_type => creditable.class.to_s, :creditable_id => creditable.id, :userid_initiating_action => current_user.id)
- c.save
+
+ # Create new creditations (if any selected ones are not yet present in the DB)
+ new_credited_contributors.each do |new_creditor|
+ unless (Creditation.find(:first, :conditions => ["creditable_id = ? AND creditable_type = ? AND creditor_id = ? AND creditor_type = ?", creditable.id, creditable.class.to_s, new_creditor[1][1], new_creditor[1][0]]))
+ Creditation.create(:creditable_id => creditable.id, :creditable_type => creditable.class.to_s, :creditor_id => new_creditor[1][1], :creditor_type => new_creditor[1][0], :userid_initiating_action => current_user.id)
+ end
end
- # Friends + other users
- user_ids = parse_comma_seperated_string(params[:credits_users])
- user_ids.each do |id|
- c = Creditation.new(:creditor_type => 'User', :creditor_id => id, :creditable_type => creditable.class.to_s, :creditable_id => creditable.id, :userid_initiating_action => current_user.id)
- c.save
- end
-
- # Networks (aka Groups)
- network_ids = parse_comma_seperated_string(params[:credits_groups])
- network_ids.each do |id|
- c = Creditation.new(:creditor_type => 'Network', :creditor_id => id, :creditable_type => creditable.class.to_s, :creditable_id => creditable.id, :userid_initiating_action => current_user.id)
- c.save
- end
-
end
+
def update_attributions(attributable, params)
# First delete old attributions: