Using presenters to decouple models from views
In Chapter 1, Rails as a Web Application Framework, we discussed the basics of the MVC paradigm and how Ruby on Rails employs it. Specifically, we mentioned that in Rails, views read from models to render the UI. This naturally introduces a unidirectional connection between the two layers: views depend on models.
However, it’s possible to introduce a reverse dependency and make a model aware of (and, thus, responsible for) a particular UI feature. Such a dependency would violate the core principle of the layered architecture—not having upward dependencies (views belong to the presentation layer, the topmost one).
Now, let’s move from theory to practice and consider a couple of examples demonstrating such upward dependencies. In other words, let’s see how representation logic can leak into the domain layer by looking at the following User
model:
User
class User < ApplicationRecord def...