Using Presenters
Explore how to implement presenters in Rails using SimpleDelegator or the draper gem to manage view logic more cleanly. Learn to convert helper methods into presenter methods and write simplified tests that run faster without Rails dependencies.
We'll cover the following...
View testing presenters
Testing helpers is handy, but if we have a lot of logic in the helpers, we recommend moving the logic into presenter objects. This is especially true if we have a series of helpers that take the same argument.
Ruby presenters
There’s nothing complicated about using presenters in Rails. We can use them using Ruby’s SimpleDelegator class. If we want a little more structure, we can use the draper gem.
We can convert the project helper to a project presenter. This version of the code uses SimpleDelegator and includes a method for converting a list of projects into a list of presenters. In a break from convention, we’ll show the code first:
The main ...