Skip to content

Instantly share code, notes, and snippets.

@promisepreston
Created September 7, 2019 12:09
Show Gist options
  • Save promisepreston/bf48c9867402d32bea6ad535dedd4264 to your computer and use it in GitHub Desktop.
Save promisepreston/bf48c9867402d32bea6ad535dedd4264 to your computer and use it in GitHub Desktop.
# define a command class
class AuthenticateUser
# put SimpleCommand before the class' ancestors chain
prepend SimpleCommand
include ActiveModel::Validations
# optional, initialize the command with some arguments
def initialize(email, password)
@email = email
@password = password
end
# mandatory: define a #call method. its return value will be available
# through #result
def call
if user = User.find_by(email: @email)&.authenticate(@password)
return user
else
errors.add(:base, :failure)
end
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment