← Back to Upcase

One instance variable sent to view


(Justin Gordon) #1

Regarding the coding style to set at most one instance variable in a controller action, what about having using controller filters (before_action) set instance variables?

I got that comment in response to whether or my Presenters pattern (https://github.com/justin808/fat-code-refactoring-techniques/pull/11) is justified.

Unless one uses such before_action filters to set other instance variables, what would be other alternatives than this “presenters” pattern?


(Geoff Harcourt) #2

If you need some persistent containers that can be referenced in controllers that might be acted upon in before filters, etc., but you’re only making use of one final instance variable in your views, the convention is to name the “temporary” instance variables with a leading underscore, so @_query_date would be something that gets set in a before filter, but is only used to build your final presenter that gets passed to the view.

I think that’s a decent compromise on Sandi Metz’ rule #4, but if you have a ton of that logic going on, you might want to look into service objects. Keep that controller skinny!