Module: QuoVadis::Model::ClassMethods

Defined in:
lib/quo_vadis/model.rb

Instance Method Summary collapse

Instance Method Details

#authenticates(identifier: :email) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/quo_vadis/model.rb', line 12

def authenticates(identifier: :email)
  include InstanceMethodsOnActivation

  has_one :qv_account, as: :model, class_name: 'QuoVadis::Account', dependent: :destroy, autosave: true

  before_validation :qv_copy_identifier_to_account, if: :has_authentication_account?

  validate :qv_copy_password_errors, if: Proc.new { |m| m.&.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}`
    END
  end

  define_method :qv_copy_identifier_to_account do
    .identifier = send identifier
  end

  after_update :qv_log_email_change,    if: [:has_authentication_account?, :saved_change_to_email?]
  after_update :qv_notify_email_change, if: [:has_authentication_account?, :saved_change_to_email?]

  QuoVadis.register_model self.name, identifier
end