Created
June 17, 2009 19:54
-
-
Save brandon-beacher/131456 to your computer and use it in GitHub Desktop.
This file contains 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 ObjectsImporter | |
def self.import_objects(row_type) | |
ActiveRecord::Base.transaction do | |
import = Import.create!(:import_type => row_type.to_s) | |
row_type.find_in_batches do |rows| | |
rows.each do |row| | |
object = find_existing_or_initialize_new_object(import, row) | |
next unless object | |
object.import = import | |
if object.new_record? | |
before_create_object object | |
if object.save | |
report_new_save_success object | |
else | |
report_new_save_failure object | |
end | |
else | |
if object.save | |
report_existing_save_success object | |
else | |
report_existing_save_failure object | |
end | |
end | |
puts | |
end | |
end | |
end | |
end | |
private | |
def self.report_existing_save_failure(object) | |
puts "Unable to save existing object #{object.id}" | |
puts object.errors.full_messages.to_yaml | |
puts object.attributes.to_yaml | |
end | |
def self.report_existing_save_success(object) | |
puts "Saved existing object #{object.id}: #{object}" | |
end | |
def self.report_new_save_failure(object) | |
puts "Unable to save new object" | |
puts object.errors.full_messages.to_yaml | |
puts object.attributes.to_yaml | |
end | |
def self.report_new_save_success(object) | |
puts "Saved new object #{object.id}: #{object}" | |
end | |
def self.titleize(string) | |
string.blank? ? nil : string.titleize | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment