News for hackers. Stuff that matters.
Re:Put Callback Logic Into Callback Objects [Rails] by JonWood on Thursday March 08, 2012 @07:58AM (#3679546)
This strikes me as being similar to extracting bits of a model out into modules that then get included back into that model. It looks like you're reducing the complexity of the model, but in fact you're just hiding it elsewhere, without making it any easier to stub things out.
Re:Put Callback Logic Into Callback Objects [Rails] by nagnatron on Thursday March 08, 2012 @08:36AM (#3679628)
You're right that it's similar to extracting it to a module since (in this example) HashGenerator.new is evaluated with the class and in my Request spec I can't exactly stub HashGenerator.new to return a double.

How would you do it?

Re:Put Callback Logic Into Callback Objects [Rails] by camwest on Thursday March 08, 2012 @08:39AM (#3679641)
Create a RequestFactory which is responsible for creating valid requests, put the hash generator in there. Then use the factory in the controller instead of directly creating the model.

That way you don't have the indirection of an ActiveRecord callback and you have your process of creating a request and assigning it a unique hash in a straightforward single process.

Re:Put Callback Logic Into Callback Objects [Rails] by nagnatron on Thursday March 08, 2012 @08:50AM (#3679672)
This makes sense. Thanks.
Re:Put Callback Logic Into Callback Objects [Rails] by camwest on Thursday March 08, 2012 @08:58AM (#3679694)
I've learned this the hard way. I have an app which has callback chains through 5+ models and it's basically impossible to figure out what's going on.

-C

Re:Put Callback Logic Into Callback Objects [Rails] by JonWood on Thursday March 08, 2012 @09:24AM (#3679785)
I'm starting to use service objects, which are responsible for coordinating things when multiple models are required to perform an operation, or complex business logic is involved.

James Golick has a great blog post on that approach at http://jamesgolick.com/2010/3/14/crazy-heretical-and-awesome...

Re:Put Callback Logic Into Callback Objects [Rails] by dbkbali on Thursday March 08, 2012 @08:37AM (#3679635)
Yes I am wondering whether it is that much different than including a concern module, at the end of the day you still have to test the code. Interesting approach though.