Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorators vs Helpers in Rails?

I'm trying to understand decorator patterns.

As I understand it, Decorators are concerned with 'presenting' the data of a model. They will encompass methods that can be called in the Views.

Up until now, I would have been throwing methods like that in the Helpers or just in the Models (if they need to be called on model instances). Is the main difference between Decorators and Helpers that Decorators are tied to a specific model, whereas Helpers can be more generic utility methods (like Date helpers and so on)?

like image 302
Paul N Avatar asked Aug 02 '17 18:08

Paul N


People also ask

What are helpers in Rails?

What are helpers in Rails? A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words .

What are decorators in Rails?

Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects, called decorators. Using decorators you can wrap objects countless number of times since both target objects and decorators follow the same interface.

Can we use helper method in controller Rails?

In Rails 5, by using the new instance level helpers method in the controller, we can access helper methods in controllers.

What are Rails presenters?

What is presenter? Presenters are part of view objects. Presenters are used in rails to refactor the response rendering to the views. According to MVC in rails, For most conventional RESTful applications, the controller will receive the request, fetch or save data from a model, and use a view to create HTML output.


1 Answers

Rails' (built-in) way of organizing your code is: Fat models, skinny controllers, and throw the rest in Helpers (or Concerns, which are Helpers but for controllers/models).

Biggest problems with helpers (IMO):

  • they are accessible in any view. Yep, everything defined in every Helpers of your app is available in the views...
  • they are modules: they cannot be instantiated, therefore you call their methods only giving arguments. I prefer the OOP version: @user.full_name.

Decorators:

  • basically, they wrap your model's instance and provide methods for display purpose. A Decorator should not modify the data, just arrange it, pluralize, translate, add commas, display currency with the price, etc. It decorates the object and its data.
  • they are tied to a specific object (not only model instance, but you can also use a Decorator for a Plain-Old-Ruby-Object as well, such as UserRole or Country).

Using the Decorator pattern will reduce the amount of code in the fat models (imposed by Rails' built-in way to do things):

  • Your model, which is supposed to hold the business logic, is not polluted with display logic anymore.
  • Your Helpers are no longer big fat piles of methods available anywhere, but instead only defining really globally-helping methods like link_to_icon(icon_name, *args), hours_from_datetime(datetime, format = '24'), menu_link(name, path, *args), etc.
like image 165
MrYoshiji Avatar answered Sep 19 '22 09:09

MrYoshiji