Skip to content

Instantly share code, notes, and snippets.

@camwest
Forked from kalupa/gist:1352738
Created November 9, 2011 19:53
Show Gist options
  • Save camwest/1352743 to your computer and use it in GitHub Desktop.
Save camwest/1352743 to your computer and use it in GitHub Desktop.
require 'csv'
def transform_merge(csv_file)
CSV.open(@csv_file).read.each do |line|
end
end
def multiple_merge(inputs)
errors = []
inputs.each do |input|
begin
merge(input[0], input[1], input[2])
rescue RuntimeError => e
errors << e.message
end
end
errors
end
def merge(user1, user2, preferred_email)
user1.strip!
user2.strip!
raise "Email cannot be the same" if (user1 == user2)
u = User.unscoped.find_by_email(user1)
u2 = User.unscoped.find_by_email(user2)
raise "User1 not found" if u.nil?
raise "User2 not found" if u2.nil?
if u.alternate_ids.empty?
primary = u
victim = u2
else
primary = u2
victim = u
end
primary.takeover!(victim)
primary.update_attribute(:email, preferred_email)
primary.restore!
primary.employments.collect { |emp| emp.allstate_network_ids.collect(&:network_id) }.flatten
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment