Presenter and Service objects in Rails

Tram Ho

Original article Rails Design Patterns: Presenter & Service Objects

Why do we need to use design patterns?

The problem is that the structure of Rails, Model-View-Controller shows the basic structure for knowing where to write code.

But still not enough, the View will become large and full of logic when general purposes only represent information.

The controller contains details beyond what to do in the controller according to its basic task.

What is the solution?

In this article there are 2 solutions to the above problem as design patterns.

  • Use the presenter pattern
  • Use the service object pattern

Not everyone agrees on how to implement it, but in the article there will be a version to be functional. Let’s explore those patterns.

Use Presenter in Rails

Views to represent information mean HTML, CSS, and ERB (Embedded Ruby) files. Note that there should not be any ActiveRecord queries in the views.

All logic must be removed from the view if it wants to be clean and easy to work with. (The logic here means the if and the operator ?:

Now the solution to processing logic in that view is to use helpers.

Helpers are useful when we have a global format method that can be used in a number of views.

Example: Performing markdown format, displaying dates with a certain format, deleting specific words from text, etc.

How to use

We can put the code in the app/helpers directory and in the date_helper.rb file.

Advice

Always pass input to the helpers method as arguments instead of relying on instant variables. Because this will help avoid a lot of trouble

The Helpers method has a special limit if it is used for all purposes in the views. Because helpers tend to be constructive and lack of organization.

Replaces complex formatting methods and conditions

Imagine yourself having a view like this

The above code is short, right? But when reading the code, it is complicated with operators and code iteration. Bad practice

Let’s create a presenter class to deal with the following

Save the above code in the directory app/presenters/post_presenter.rb , create the presenters directory if not already.

The advantages here are:

  • Delete all logic from the view
  • Give meaningful names to the format and decision operators.
  • Reuse this class in other views without repeating code

Here’s how to use the Rails presenter

Service Subjects usage

The controller should only contain the to-do parts, they should not contain how to send a Tweet, charge a client fee or create a PDF file.

These handles should authorize a service object.

A service object is a Ruby module that encapsulates the logic to complete an action.

Example:

For convenience save the code under the app/services directory and the filename twitter_service.rb .

Why should we use the above method? Since Rails autoloads all from the app/ directory, the above code will be available in the controllers.

In the controller we can use the following

This is the behavior of the service object.

Conclude

In the article, we have introduced two Rails patterns to help us improve the quality of code in the project. Please apply it now.

Thank you for reading

Share the news now

Source : Viblo