Skip to content

Instantly share code, notes, and snippets.

@ryannealmes
Last active July 5, 2017 23:08
Show Gist options
  • Save ryannealmes/9c148a6c4b35aefaf505 to your computer and use it in GitHub Desktop.
Save ryannealmes/9c148a6c4b35aefaf505 to your computer and use it in GitHub Desktop.
How to managed services and dependencies ...
# adapted from
# http://adamniedzielski.github.io/blog/2014/11/25/my-take-on-services-in-rails/
class CreateUserAccount
attr_reader :user
def initialize(send_email_service: SendEmail.new, generate_token_service: GenerateToken.new)
@send_email_service = send_email_service
@generate_token_service = generate_token_service
end
def call
save_user
send_email
generate_token
end
private
def save_user
# save user
# set user attr_reader
end
def send_email
@send_email_service.call(user)
end
def generate_token
@generate_token_service.call(user)
end
end
class CreateUserAccount
class SendEmail
def call(user)
# send email
end
end
end
class CreateUserAccount
class GenerateToken
def initialize token_parser: TokenParser.new
@token_parser = token_parser
end
def call(user)
# do some logic
parse_token
end
private
def parse_token
@token_parser.call
end
end
end
class CreateUserAccount
class TokenParser
def call
# generate token for user
end
end
end
CreateUserAccount.new(generate_token_service: CreateUserAccount::GenerateToken.new(token_parser: CreateUserAccount::TokenParser.new)).call
@betosalvador
Copy link

Hello. Thanks for share your code.

I have one question: where you pass user params?

I think you will pass in CreateUserAccount#call(user_params), but I'm not sure.

Thanks in advance!

@alainbloch
Copy link

@betosalvador - you pass your params through the .new method. CreateUserAccount.new(user_params).call But make sure you are using the keywords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment