Increased 'error resistance' in base importer class

This commit is contained in:
Régis Hanol 2015-12-01 15:38:21 +01:00
parent a8e69c2fd6
commit 26006f12a2

View File

@ -239,14 +239,18 @@ class ImportScripts::Base
elsif u[:email].present?
new_user = create_user(u, import_id)
if new_user.valid? && new_user.user_profile.valid?
if new_user && new_user.valid? && new_user.user_profile && new_user.user_profile.valid?
@lookup.add_user(import_id.to_s, new_user)
created += 1
else
failed += 1
puts "Failed to create user id: #{import_id}, username: #{new_user.username}, email: #{new_user.email}"
puts "user errors: #{new_user.errors.full_messages}"
puts "user_profile errors: #{new_user.user_profile.errors.full_messages}" if new_user.user_profile.present? && new_user.user_profile.errors.present?
puts "Failed to create user id: #{import_id}, username: #{new_user.try(:username)}, email: #{new_user.try(:email)}"
if new_user.try(:errors)
puts "user errors: #{new_user.errors.full_messages}"
if new_user.try(:user_profile).try(:errors)
puts "user_profile errors: #{new_user.user_profile.errors.full_messages}"
end
end
end
else
failed += 1
@ -292,6 +296,7 @@ class ImportScripts::Base
opts[:last_emailed_at] = opts.fetch(:last_emailed_at, Time.now)
u = User.new(opts)
(opts[:custom_fields] || {}).each { |k, v| u.custom_fields[k] = v }
u.custom_fields["import_id"] = import_id
u.custom_fields["import_username"] = opts[:username] if opts[:username].present?
u.custom_fields["import_avatar_url"] = avatar_url if avatar_url.present?
@ -313,9 +318,8 @@ class ImportScripts::Base
end
rescue => e
# try based on email
if e.record.errors.messages[:email].present?
existing = User.find_by(email: opts[:email].downcase)
if existing
if e.try(:record).try(:errors).try(:messages).try(:[], :email).present?
if existing = User.find_by(email: opts[:email].downcase)
existing.custom_fields["import_id"] = import_id
existing.save!
u = existing
@ -325,6 +329,7 @@ class ImportScripts::Base
raise e
end
end
post_create_action.try(:call, u) if u.persisted?
u # If there was an error creating the user, u.errors has the messages