Skip to content

Instantly share code, notes, and snippets.

@robertzx
Created October 18, 2011 17:29
Show Gist options
  • Save robertzx/1296044 to your computer and use it in GitHub Desktop.
Save robertzx/1296044 to your computer and use it in GitHub Desktop.
def lookup(reference, authenticable = true)
return reference if !reference or reference.is_a? User
base = self
if reference.is_a? Numeric or (reference.is_a? String and reference.match(/^\d+$/))
user = base.find(reference.to_i)
elsif reference.include? '@'
user = base.find_by_email(reference) ||
base.find_by_hashed_email(User.hash_email(reference)) || base.find_by_identifier(reference)
else
user = base.find_by_login(reference) || base.find_by_identifier(reference)
end
(user && (!authenticable || user.can_login?)) ? user : nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment