--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
For me its because method approach enforces method granularity, which makes the various behaviours of the object easier to test in isolation.
In rails controllers where you have many different actions (and each of their listener callbacks) all in one class this can get cumbersome and difficult to follow. I think you could get the best of both worlds there by creating the explicit methods on the controller but passing them in to the service object call using `public_method`
wdyt?
Cheers,
M
On 18 September 2013 19:35, Rafael George <george...@gmail.com> wrote:
@Mike, exactly that's my problem with the approach of having an explicit call back method inside the controller, but then again probably the issue here is that the controller itself is breaking SRP; probably it could be a good idea to have an action for each controller.
I'm with Kevin, I prefer explicit methods because it makes it far more testable.
By passing the controller in directly as the listener you do end up with a lot of callback methods on the controller.
One way I've used to deal with this is to decorate the controller with the callbacks, something like:
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
This gives me the ability to test the callbacks in isolation, and it keeps my controllers very simple.
The decorated controller gives you access to all of the controller methods you need. It doesn‘t allow you to set instance variables, but I don’t mind being explicit about what I pass through to the views in the locals
hash.
On Tuesday, September 17, 2013 3:53:17 PM UTC-4, Rafael George wrote:Hello guys,A friend of mine and myself were discussing the other day the way Matt presented Hexagonal Rails which is just great, but we think that having two methods for each action will increase the code inside the controller, I'm talking about the methods for handling success and failures. We came with the following solution:What do you think about this approach ?
--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--