Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
User Service
# service name hints that it does
class CreateUserService
# default attributes that all services should have
attr_reader :errors, :messages, :output
# custom attributes for what this service does
attr_reader :user, :send_email
# we receive all parameters in the initialization
def initialize(user:, send_email: true)
@user = user
@send_email = send_email
end
def perform
reinitialize
@user.token = TokenProvider.generate(@user.email) unless @user.token.present?
if @user.valid?
@user.save
UserMailer.welcome_message(user).deliver if @send_email
@messages << "User #{user.email} successfully created"
@output = @user
else
@errors << @user.errors.full_messages
end
@errors.empty?
end
private
# this just makes sure that each time perform is called the errors and
# messages are reinitialized
def reinitialize
@errors = []
@messages = []
@output = nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.