Sha256: 687ac833f8b503bc07b8323c80d770358372e746e36b6e237a64ef49d3327009

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Authenticate
  module Model

    module Username
      extend ActiveSupport::Concern

      def self.required_fields(klass)
        [:username]
      end

      included do
        before_validation :normalize_username
        validates :username,
                  presence: true,
                  uniqueness: { allow_blank: true }
      end

      module ClassMethods
        def credentials(params)
          [params[:session][:username], params[:session][:password]]
        end

        def authenticate(credentials)
          user = find_by_credentials(credentials)
          user && user.password_match?(credentials[1]) ? user : nil
        end

        def find_by_credentials(credentials)
          username = credentials[0]
          find_by_username normalize_username(username)
        end

        def normalize_username(username)
          username.to_s.downcase.gsub(/\s+/, '')
        end
      end

      def normalize_username
        self.username = self.class.normalize_username(username)
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authenticate-0.1.0 lib/authenticate/model/username.rb