Skip to content

Instantly share code, notes, and snippets.

@manojmj92
Last active August 30, 2018 15:59
Show Gist options
  • Save manojmj92/69d2fe44d53e457574175f1f45fdecfc to your computer and use it in GitHub Desktop.
Save manojmj92/69d2fe44d53e457574175f1f45fdecfc to your computer and use it in GitHub Desktop.
Interactor Example
# Definition of the Interactor
class AuthenticateUser
include Interactor
def call
if user = User.authenticate(context.email, context.password)
context.user = user
context.token = user.secret_token
else
context.fail!(message: 'authenticate_user.failure')
end
end
end
# Invocation of the Interactor
class SessionsController < ApplicationController
def create
if user = AuthenticateUser.call(session_params) # session_params = { email: 'blah@example.com', password: 'blahblahblackheap'}
session[:user_token] = user.secret_token
redirect_to user
else
flash.now[:message] = 'Please try again.'
render :new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment