module QuoVadis::Model::ClassMethods

Public Instance Methods

authenticates(identifier: :email) click to toggle source
# 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: Proc.new { |m| m.qv_account }

        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}`
          END
        end

        define_method :qv_copy_identifier_to_account do
          qv_account.identifier = send identifier
        end

        after_update :qv_log_email_change, if: :saved_change_to_email?
        after_update :qv_notify_email_change, if: :saved_change_to_email?

        QuoVadis.register_model self.name, identifier
      end