lib/quo_vadis/model.rb in quo_vadis-2.0.2 vs lib/quo_vadis/model.rb in quo_vadis-2.1.0

- old
+ new

@@ -12,15 +12,13 @@ def authenticates(identifier: :email) include InstanceMethodsOnActivation has_one :qv_account, as: :model, class_name: 'QuoVadis::Account', dependent: :destroy, autosave: true - before_validation :qv_build_account_and_password, on: :create - before_validation :qv_copy_identifier_to_account + before_validation :qv_copy_identifier_to_account, if: Proc.new { |m| m.qv_account } - # Enable a new-user form to set a password. - validate :qv_copy_password_errors, on: :create + validate :qv_copy_password_errors, if: Proc.new { |m| m.qv_account&.password } unless validators_on(identifier).any? { |v| ActiveRecord::Validations::UniquenessValidator === v } raise NotImplementedError, <<~END Missing uniqueness validation on #{name}##{identifier}. Try adding: `validates :#{identifier}, uniqueness: {case_sensitive: false}` @@ -42,26 +40,21 @@ module InstanceMethodsOnActivation attr_reader :password, :password_confirmation def password=(val) @password = val - self.qv_account ||= build_qv_account + build_qv_account unless qv_account raise PasswordExistsError if qv_account.password&.persisted? (qv_account.password || qv_account.build_password).password = val end def password_confirmation=(val) @password_confirmation = val - self.qv_account ||= build_qv_account + build_qv_account unless qv_account (qv_account.password || qv_account.build_password).password_confirmation = val end private - - def qv_build_account_and_password - self.qv_account ||= build_qv_account - qv_account.password || qv_account.build_password - end def qv_copy_password_errors qv_account.password.valid? # force qv_account.password to validate qv_account.password.errors[:password ].each { |message| errors.add :password, message } qv_account.password.errors[:password_confirmation].each { |message| errors.add :password_confirmation, message }