module Authenticatable::Models::Identifier

Public Instance Methods

find_by_identifier(value) click to toggle source

Search for a resource by the choosen identifier. find_by_identifier(‘foo@bar.com’) is equivalent to find_by(email: ‘foo@bar.com’)

# File lib/authenticatable/models/identifier.rb, line 19
def find_by_identifier(value)
  find_by("#{identifier_column}": normalize_identifier(value))
end
identifier() click to toggle source

Since the identifier_column can be set dynamically (to for example email or username) we’ll need a common attribute to access the authenticated resource identifier.

# File lib/authenticatable/models/identifier.rb, line 12
def identifier
  self[self.class.identifier_column]
end
identifier_column() click to toggle source

Identifying column to use when looking up an authenticatable record in the database. Can be for example email or a username. Default is email.

# File lib/authenticatable/models/identifier.rb, line 32
def identifier_column
  @identifier_column || Authenticatable.config.default_identifier
end
identify_by(column) click to toggle source

Convient method to update the identifier_column.

# File lib/authenticatable/models/identifier.rb, line 37
def identify_by(column)
  @identifier_column = column
end
normalize_identifier(identifier) click to toggle source

Ensure identifier is a downcase string without spaces. Example:

fOo@b aR.com => foo@bar.com
# File lib/authenticatable/models/identifier.rb, line 26
def normalize_identifier(identifier)
  identifier.to_s.downcase.gsub(/\s+/, "")
end