DEV Community

Cover image for Decorator in Ruby on Rails
Sergio Turpín
Sergio Turpín

Posted on

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
Enter fullscreen mode Exit fullscreen mode

2. Install Draper gem

> bundle add draper
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 %>
Enter fullscreen mode Exit fullscreen mode

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 🙏

🌎 https://sergioturpin.es
📷 Instagram
🐦 Twitter
🐙 GitHub

- 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
Enter fullscreen mode Exit fullscreen mode

2. Instalamos la gema Draper

> bundle add draper
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 %>
Enter fullscreen mode Exit fullscreen mode

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 🙏

🌎 https://sergioturpin.es
📷 Instagram
🐦 Twitter
🐙 GitHub

Discussion (2)

Collapse
kunashir profile image
Aleksandr Korolev

Thanks you for this article but could you please say me what advantages of using decorator vs subclassing/inheritance, and in which cases we should prefer it.

Collapse
sturpin profile image
Sergio Turpín Author

Hi Aleksandr,
Thanks for your comment, this is a good question ;)
Some time ago I found this website that explains it great:
Decorator VS Subclass.

I really liked your approach.

Hope helps you,
Sergio Turpín