Created
October 31, 2013 14:14
-
-
Save nicholasjhenry/7250531 to your computer and use it in GitHub Desktop.
Hexagonal Rails with Response Object: https://groups.google.com/d/msg/objects-on-rails/yY4k86n_i9s/dJcMiOYie34J
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
| class UsersController < ApplicationController | |
| def create | |
| CreateUser.new(User).call(params[:user], CreateResponse.new(self)) | |
| end | |
| class CreateResponse < SimpleDelegator | |
| def user_created(user) | |
| flash[:notice] ="The user was created successfully" | |
| redirect_to :index | |
| end | |
| def user_creation_failed(user) | |
| render 'new', locals: { user: user } | |
| end | |
| end | |
| end | |
| class CreateUser | |
| def initialize(users_repository) | |
| @users_repository = users_repository | |
| end | |
| def call(attributes: {}, listener) | |
| user = @users_repository.new attributes | |
| if user.save | |
| listener.user_created(user) | |
| else | |
| listener.user_creation_failed(user) unless user.save | |
| end | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Similar option to what I have been exploring recently, factory for use cases:
https://gist.github.com/chris-zwickilton/6625578
https://groups.google.com/d/msg/objects-on-rails/yY4k86n_i9s/lNzigifGL6AJ