Created
June 17, 2009 19:58
-
-
Save brandon-beacher/131464 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Importers | |
| class PeopleImporter < ObjectsImporter | |
| private | |
| def self.import_type | |
| Person.class_name | |
| end | |
| def self.before_create_object(person) | |
| password = ActiveSupport::SecureRandom.hex 3 | |
| person.new_password = password | |
| person.new_password_confirmation = password | |
| if person.emails.empty? | |
| person.emails.build :address => Person.generate_interim_email(person.original_id) | |
| end | |
| end | |
| def self.find_existing_or_initialize_new_object(import, person_row) | |
| if original_person = Person.find_by_original_id(person_row.get_original_id) | |
| puts "Matched original id for #{original_person}" | |
| end | |
| duplicate_email_rows, email_rows = person_row.email_rows.partition { |email_row| email_row.duplicates_exist? } | |
| email_people = [] | |
| email_rows.each do |email_row| | |
| if email_person = Person.email_address(email_row.get_address) | |
| if original_person && original_person != email_person | |
| puts "Skipping email #{email_row.get_address} for #{original_person} because it matched #{email_person}" | |
| email_row.destroy | |
| else | |
| puts "Matched email #{email_row.get_address} to #{email_person}" | |
| email_people | [email_person] | |
| end | |
| end | |
| end | |
| duplicate_email_rows.each do |duplicate_email_row| | |
| if duplicate_email_person = Person.email_address(duplicate_email_row.get_address).first_name(person_row.get_first_name) | |
| if original_person && original_person != duplicate_email_person | |
| puts "Skipping email #{duplicate_email_row.get_address} for #{original_person} because it matched #{duplicate_email_person}" | |
| duplicate_email_row.destroy | |
| elsif email_people.present? && !email_people.include?(duplicate_email_person) | |
| possible_person = Person.email_address(duplicate_email_row.get_address).first_name(person_row.get_first_name) | |
| duplicate_email_row.destroy unless possible_person | |
| possible_people = Person.email_addresses(duplicate_email_rows.collect(&:get_address)).first_name(person_row.get_first_name) | |
| if possible_people.length == 1 | |
| puts "Matched email and first name for #{possible_people.first}" | |
| return possible_people.first | |
| end | |
| if possible_people.length > 1 | |
| puts "Unable to narrow down multiple email and first name matches" | |
| puts possible_people.to_yaml | |
| return nil | |
| end | |
| return original_person if original_person | |
| puts "Initializing new person for #{person_row}" | |
| Person.new | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment