Created
September 7, 2019 12:09
-
-
Save promisepreston/bf48c9867402d32bea6ad535dedd4264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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