Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 1, 2022 09:12 pm GMT

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.rbclass UserDecorator < Draper::Decorator  delegate_all  def full_name    object.first_name + ' ' + object.last_name  endend

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.rbdef show  @user = User.find(params[:id]).decorateend

6. Show it in View

# app/views/users/show.html.erbFirst 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

https://sergioturpin.es
Instagram
Twitter
GitHub

- Versin en Espaol -

El patrn Decorator nos permite poder encapsular un objeto para que podamos extender su funcionalidad antes de su instanciacin, as de simple!

Existen muchas formas de implementarlo, pero una de las ms 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, automticamente se crear tambin su decorador en el directorio app/decorator.

3. Creamos el scaffold de usuario

> rails g scaffold User first_name last_name

Adems de otros archivos, el fichero app/decorators/user_decorator.rb ha sido creado automticamente.

4. Aadimos un mtodo personalizado

# app/decorators/user_decorator.rbclass UserDecorator < Draper::Decorator  delegate_all  def full_name    object.first_name + ' ' + object.last_name  endend

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.rbdef show  @user = User.find(params[:id]).decorateend

6. Lo mostramos en la Vista

# app/views/users/show.html.erbFirst name: <%= @user.first_name %>Last name: <%= @user.last_name %>Full name: <%= @user.full_name %>

Y ahora su lgica de vista se ha abstrado y organizado donde debera estar. Esto le ayudar enormemente a reducir el cdigo en sus archivos de vista.

Normalmente es usado conjuntamente con el patrn Presenters, pero he preferido hacerlo en otro post

Lecturas recomendadas

Gracias por leer

https://sergioturpin.es
Instagram
Twitter
GitHub


Original Link: https://dev.to/sturpin/decorator-in-ruby-on-rails-14kk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To