Decorator in Ruby on Rails

- English Version -

Decorators provide a flexible alternative to subclassing for extending functionality. Simple! 😉

There are several ways to implement it, but the easiest for me is using Draper gem.

1. Create Rails app

> rails new decorator-rails

2. Install Draper gem

> bundle add draper

Well, from this moment when we create a controller it will automatically create our decorator file in app/decorator folder.

3. Create user scaffold

> rails g scaffold User first_name last_name

Among other files, the file app/decorators/user_decorator.rb has been created automatically.

4. Add custom method

# app/decorators/user_decorator.rb

class UserDecorator < Draper::Decorator
  delegate_all

  def full_name
    object.first_name + ' ' + object.last_name
  end
end

That allows behavior to be added to an individual object without affecting the behavior of other objects from the same class.

5. Decorate object in controller

# app/controllers/users_controller.rb

def show
  @user = User.find(params[:id]).decorate
end

6. Show it in View

# app/views/users/show.html.erb

First name: <%= @user.first_name %>
Last name: <%= @user.last_name %>
Full name: <%= @user.full_name %>

And now your view logic has been abstracted and organized to where it should be. This will greatly help you to reduce the code in your view files.

Usually it is used with Presenters, but I prefer to do it in another post 😉

🔍 Recommended reading

Thank you for reading 🙏

- Versión en Español -

El patrón Decorator nos permite poder encapsular un objeto para que podamos extender su funcionalidad antes de su instanciación, así de simple! 😉

Existen muchas formas de implementarlo, pero una de las más sencillas es hacerlo con la gema Draper.

1. Creamos la app de Rails

> rails new decorator-rails

2. Instalamos la gema Draper

> bundle add draper

Bien, a partir de este momento cuando nosotros creemos un controlador, automáticamente se creará también su decorador en el directorio app/decorator.

3. Creamos el scaffold de usuario

> rails g scaffold User first_name last_name

Además de otros archivos, el fichero app/decorators/user_decorator.rb ha sido creado automáticamente.

4. Añadimos un método personalizado

# app/decorators/user_decorator.rb

class UserDecorator < Draper::Decorator
  delegate_all

  def full_name
    object.first_name + ' ' + object.last_name
  end
end

Esto nos permite agregar comportamiento a un objeto individual sin afectar al comportamiento de otros objetos de la misma clase.

5. Decoramos el objeto en el controlador

# app/controllers/users_controller.rb

def show
  @user = User.find(params[:id]).decorate
end

6. Lo mostramos en la Vista

# app/views/users/show.html.erb

First name: <%= @user.first_name %>
Last name: <%= @user.last_name %>
Full name: <%= @user.full_name %>

Y ahora su lógica de vista se ha abstraído y organizado donde debería estar. Esto le ayudará enormemente a reducir el código en sus archivos de vista.

Normalmente es usado conjuntamente con el patrón Presenters, pero he preferido hacerlo en otro post 😉

🔍 Lecturas recomendadas

Gracias por leer 🙏

12