开发者

Where to put complex display logic for a Rails Model?

开发者 https://www.devze.com 2023-04-03 20:53 出处:网络
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 attribut

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号