Sha256: 6aafb7916e0b22bf28179faa1b9c08bfc10b1acad01cb373424d5bde54b0ed66

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Maquina
  module AuthenticateBy
    extend ActiveSupport::Concern
    included do
      add_authenticate_by
    end

    class_methods do
      def add_authenticate_by
        version = Gem::Version.new(Rails::VERSION::STRING)
        return if version.to_s.to_f >= 7.1

        Rails.logger.warn "[#{self}] Adding class method authenticate_by"
        self.class.define_method(:authenticate_by) do |attributes|
          passwords, identifiers = attributes.to_h.partition do |name, value|
            !has_attribute?(name) && has_attribute?("#{name}_digest")
          end.map(&:to_h)

          raise ArgumentError, "One or more password arguments are required" if passwords.empty?
          raise ArgumentError, "One or more finder arguments are required" if identifiers.empty?
          if (record = find_by(identifiers))
            record if passwords.count { |name, value| record.public_send(:"authenticate_#{name}", value) } == passwords.size
          else
            new(passwords)
            nil
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
maquina-0.5.2 app/models/concerns/maquina/authenticate_by.rb
maquina-0.5.1 app/models/concerns/maquina/authenticate_by.rb
maquina-0.4.0 app/models/concerns/maquina/authenticate_by.rb
maquina-0.3.0 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.5 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.4 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.3 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.2 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.1 app/models/concerns/maquina/authenticate_by.rb
maquina-0.2.0 app/models/concerns/maquina/authenticate_by.rb
maquina-0.1.0 app/models/concerns/maquina/authenticate_by.rb