I have bemoaned the lack of a ViewModel in Rails many times, and I prefer using Presenters to simulate a ViewModel. But it turns out there is an object that does represent the state of the view in Rails apps. Its an instance of ActionView::Base
that the controller calls view_context
. We don't have much control of this object, but it seems like a logical place to put our view-specific behavior.
This code is an attempt at creating Presenters using modules. The module will be mixed into the view_context
object. This is very similar to how a Decorator module will be mixed into a model object, only instead of being specific to the model is it specific to the view.
This means that testing your presenter is no different than testing any other module. So relying on dependencies such as other methods or instance variables can make testing difficult.
How is it different than standard Helpers?
Best,
Tute.