Anyone who has done any Rails development knows that views gets complicated very fast. Lately we’ve been experimenting with Carriers ( also called view services ) to clean up views.
We wanted to start with simplest solution by making use of simple ruby objects that takes care of business logic away from views.
A complex view
Consider this user model
Here we have a view that displays appropriate profile link and changes css class based on the role of the user.
Extracting logic to Rails helper
In the above case we can extract the logic from the view to a helper.
After the extraction the code might look like this
Now the view is much simpler.
Why not to use Rails helpers?
Above solution worked. However in a large Rails application it will start creating problems.
UsersHelper is a module and it is mixed into
So if the Rails project has large number of helpers then all of them are
mixed into the
ApplicationHelper and sometimes there is a name collision.
For example let’s say that there is another helper called
ShowingHelper and this helper also has method
ApplicationHelper is mixing in both modules
One of those methods will be overridden and
we would not even know about it.
Another issue is that all the helpers are modules not classes. Because they are not classes it becomes difficult to refactor helpers later. If a module has 5 methods and if we refactor two of the methods into two separate methods then we end up with seven methods. Now out of those seven methods in the helper only five of them should be public and the rest two should be private. However since all the helpers are modules it is very hard to see which of them are public and which of them are private.
And lastly writing tests for helpers is possible but testing a module directly feel weird since most of the time we test a class.
Lets take a look at how we can extract the view logic using carriers.
In our controller
Now the view looks like this
No html markup in the carriers
Even though carriers are used for presentation we stay away from having any html markup in our carriers. That is because once we open the door to having html markups in our carriers then carriers quickly get complicated and it becomes harder to test them.
No link_to in the carriers
Since carriers are plain ruby objects,
there is no
link_to and other helper methods usually.
And we keep carriers that way.
because the job of the carrier is to present the data that
can be used in
complement the usage of
We believe that
link_to belongs to the ERB file.
However if we really need to have an abstraction over it then we can create
a regular Rails helper method.
We minimize usage of Rails helper, we do not avoid it altogether.
Overcoming Double Dots
Many times in our view we end up doing
This is a violation of
Law of Demeter .
We call it “don’t use Double Dots”. Meaning
Its just a matter of time before views code looks like this
Since carriers encapsulate objects into classes, we can overcome this “double dots” issue by delegating behavior to appropriate object.
After that refactoring we end up with cleaner views like.
Note that “Double dot” is allowed at other parts of the code. We do not allow it in views.
Since the carriers are simple ruby objects it’s easy to test them.
Carriers allow us to encapsulate complex business logic in simple ruby objects.
This helps us achieve clearer separation of concern, clean up our views and avoid skewed and complex views. Our views are free of “double dots” and we end up with simple tests which are easy to maintain.
We decided to call it a “carrier” and not “presenter” because the word “presenter” is overloaded and has many meanings.
We at BigBinary take a similar approach for extracting code from a fat controller or a fat model. You can find out more about it here.