diff options
author | elijah <elijah@riseup.net> | 2014-12-23 11:52:55 -0800 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2014-12-23 11:52:55 -0800 |
commit | 42ba688eabcdb428e0ce230175b72c357bea9cdb (patch) | |
tree | 00d1565b355907492165731a5e4414203144f7a0 | |
parent | 1d2c6dd8ea1e40e24b1ae958455c4297e0019d96 (diff) |
bugfix: ensure both user and identity documents are destroyed if there is a problem creating the account.
-rw-r--r-- | app/models/account.rb | 36 |
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) |