Why hello there! Are you researching design patterns applied to ruby? Are you interested in learning the value of Presenters? Well then, you have come to the right place my friend. Presenters, in my opinion, are most useful when displaying information to users in your app’s User Interface (UI). Let’s check out the following Rails view code as a place rife with opportunity.
While this isn’t 100% awful..it is a tad messy and there is logic strewn about here and likely in the controller and even in a helper file. This means that you as a developer have to navigate around multiple file to understand how to render an order row in this table. That is not ideal. Let’s create a presenter and see if we can clean things up.
And there it is! Our beautiful little presenter object. As you can see, we abstracted away quite a lot of logic. This is really great for a few reasons. The first is for testing. Can you imagine writing integration/system tests to assert on the various states of this view? You’d be writing 4 minutes of tests just to do that! Moving the logic to a presenter allows you to unit test all of those conditions on the new presenter object, which is much easier to reason about and much faster to test. The second and last reason I think this presenter is awesome is because of how flexible it is. If you wanted to change how currency is formatted or update the css classes for your tags…there is just one file and a couple of methods to change.
Ok, ok..enough chatting, let’s use our new presenter to clean up that view!
That is a pretty sexy view if you ask me. We could of course pull @orders out and make it an array of presenter objects in our controller..but this transformation in the view works for our purpose: demonstration. As you can see, the rending of this view has become increasingly boring.
At the end of the day this is what applying design patterns is all about. They are used to enhance development and maintanability of code through simplification. After all boring code is often the easiest to work with.
Thank you so much for sticking around. Next week, we will be talking about Proxy objects. If you are interested please come back and read some more. Also, don’t be afraid to hop in the comments below and tell me what you thought of this article or point out those pesky mistakes that I can fix.