Skip to content

Instantly share code, notes, and snippets.

@chris-zwickilton
Last active December 23, 2015 10:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-zwickilton/6625578 to your computer and use it in GitHub Desktop.
Save chris-zwickilton/6625578 to your computer and use it in GitHub Desktop.
A flavor of Hexagonal Rails with factory created use cases
class UsersController < ApplicationController
def create
create_user = UserUseCaseFactory.create_user(Responder.new(self)
create_user.do(params[:email], params[:password[)
end
class Responder < SimpleDelegator
def success(user)
render json: UserSerializer.new(user)
end
def failure(errors)
render json: { errors: errors }, status: :unprocessable_entity
end
end
end
class UserUseCaseFactory
def create_user(responder)
return CreateUser.new(User, responder)
end
end
class CreateUser
def initialize(users_repository, responder)
@users_repository = users_repository
@responder = responder
end
def do(email, password)
user = @users_repository.new(email: email, password: password)
if user.save
@responder.success(user)
else
@responder.failure(user.errors.full_messages)
end
end
end
class UserSerializer
def initialize(user)
@user = user
end
def to_json(args = nil)
hash = {}
hash["id"] = @user.id
hash["email"] = @user.email
hash.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment