Introduce Rails Presenter

Introduce a presenter

The presenter is responsible for refining the HTML code of the logic in the View part. The point is, let's make the view part refreshing. Presenters are also called decorators.

It seems that Draper, Cells, etc. are used in Gem, We will implement it without using Gem this time.

Is the helper red?

Since it is a method used in the view, it is natural to define it as a helper method. But helper has its advantages and disadvantages that are globally defined. As the project grows, the risk of name conflicts increases.

This time, we will separate the logic part included in the following code. The logic of the criminal list. If "arrested?" Is true, add ☑️ and add ☑️. If not, it will be a blank □.

<% @members.each do |m| %>
  <%= m.arrested? ? raw("&#x2611;") : raw("&#x2610;") %>

Make a presenter about Model

First, create the ModelPresenter class, which is the ancestor of all presenters. Call-only object and view_context attributes are defined.

app/presenters/model_presenter.rb


class ModelPresenter
  attr_reader :object, :view_context

  def initialize(object, view_context)
    @object = object
    @view_context = view_context
  end
end

Next, create a Member presenter class by inheriting the model_presenter class.

member_presenter.rb


class StaffMemberPresenter < ModelPresenter
end

Edit ERB template

Edit the ERB template using this class. Create an instance of the MemberPresenter class.

The first argument of the new method is the Member object. The pseudo variable self is specified in the second argument. self can use all the helper methods defined in Rails.

<% @members.each do |m| %>
  <% p = MemberPresenter.new(m, self) %>
    <%= m.arrested? ? raw("&#x2611;") : raw("&#x2610;") %>

Define a method in Presenter

Here, we will define the instance method in the MemberPresenter class created earlier.

member_presenter.rb


class MemberPresenter < ModelPresenter
  def arrested_mark
    object.arrested? ? 
      view_context.raw("&#x2611") : 
      view_context.raw("&#x2610")
  end
end

Rewrite ERB template

Make the view part refreshing by using the presenter so far


<% @members.each do |m| %>
  <% p = MemberPresenter.new(m, self) %>
    <%= p.arrested_mark %>

The following code is embedded in the changed part.

m.arrested? ? raw("&#x2611;") : raw("&#x2610;")

I was able to refresh the view like this. I would like to write the article again because using delegate makes it a little more concise. That's all for today.

Recommended Posts

Introduce Rails Presenter
[Rails] Introduce Carrierwave
Until you introduce fonts to Rails
How to introduce jQuery in Rails 6
[Rails g.error]
Rails Review 1
Rails API
Rails migration
I tried to introduce CircleCI 2.0 to Rails app
[Rails] first_or_initialize
Introduce two-factor authentication to your Rails application
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Preparing to introduce jQuery to Ruby on Rails
[Rails] devise
[Rails 5.x] How to introduce free fonts
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
Introduce dotenv to Docker + Rails to manage environment variables
[Rails] How to easily introduce slick (slider function)