summaryrefslogtreecommitdiff
path: root/app/models/account.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/account.rb')
-rw-r--r--app/models/account.rb36
1 files changed, 24 insertions, 12 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index d13c929..e60a356 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -16,23 +16,26 @@ class Account
# Returns the user record so it can be used in views.
def self.create(attrs)
- @user = User.new(attrs)
- @user.save
- if @user.persisted?
- @identity = @user.identity
- @identity.user_id = @user.id
- @identity.save
- @identity.errors.each do |attr, msg|
- @user.errors.add(attr, msg)
+ identity = nil
+ user = nil
+ user = User.new(attrs)
+ user.save
+ if user.persisted?
+ identity = user.identity
+ identity.user_id = user.id
+ identity.save
+ identity.errors.each do |attr, msg|
+ user.errors.add(attr, msg)
end
end
rescue StandardError => ex
- @user.errors.add(:base, ex.to_s)
+ user.errors.add(:base, ex.to_s) if user
ensure
- if @user && @user.persisted? && (@identity.nil? || !@identity.persisted?)
- @user.destroy
+ if creation_problem?(user, identity)
+ user.destroy if user && user.persisted?
+ identity.destroy if identity && identity.persisted?
end
- return @user
+ return user
end
def update(attrs)
@@ -80,6 +83,15 @@ class Account
@new_identity.try(:save) && @old_identity.try(:save)
end
+ def self.creation_problem?(user, identity)
+ user.nil? ||
+ !user.persisted? ||
+ identity.nil? ||
+ !identity.persisted? ||
+ user.errors.any? ||
+ identity.errors.any?
+ end
+
# You can hook into the account lifecycle from different engines using
# ActiveSupport.on_load(:account) do ...
ActiveSupport.run_load_hooks(:account, self)