Where to put complex display logic for a Rails Model?
I have a Location#description
method that returns a (really) long string. This string is a human readable description and it's generated according to the values of the Location
开发者_如何学C attributes. However, this code is long and complex, and puts a lot of "not-business" logic in my model.
What is best to do? Bunch all this logic in helpers or create a LocationPresenter
class? If I create the presenter, what are some guidelines about doing so? Should I initialize it with a Location
or is it okay to use the attribues hash?
Consider refactoring it into a module and include it into the respective model - that's if it has any business logic components. Also look into helper_methods (which work at both the controller and view layers), if not presentation only macros can be added as helpers.
That fact that you've called it 'complex' and realised it need to move out of your model makes me wonder if it could do with some refactoring more inline with Rail's MVC paradigm.
精彩评论