[RUBY] Rails delegate method

Use delegate method

This article is a continuation of Introducing Presenters.

This is the model presenter I made last time. I would like to add a delegate method to this presenter

model_presenter.rb


class ModelPresenter
  attr_reader :object, :view_context

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

The class method delegate defines an instance method with the name specified in the argument. And the function of that method is entrusted to the object returned by the method specified in the to option.

This time we will delegate a method called raw to the object returned by view_context

model_presenter.rb



class ModelPresenter
  attr_reader :object, :view_context
  delegate :raw, to: :view_context

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

By the way, the member_presenter created last time was like this.

member_presenter.rb


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

Using the delegate method made it cleaner.

member_presenter.rb


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

Furthermore, if you outsource arrested? To object

member_presenter.rb


class MemberPresenter < ModelPresenter
  delegate :arrested?, to: :object
  def arrested_mark
    arrested? ? raw("&#x2611") : raw("&#x2610")
  end
end

Since the arrested? method is a unique object of the member object, I defined it at the inheritance destination. I was able to refresh the presenter by using delegate like this.

That's all for today !!

Recommended Posts

Rails delegate method
rails method
[Rails] devise helper method
[Rails] devise introduction method
Method
about the where method (rails)
[Rails] require method and permit method
Rails "render method" and "redirect method"
About Rails scraping method Mechanize
[Rails] About helper method form_with [Basic]
[Rails 6] destroy using the resources method
[Ruby on Rails] Convenient helper method
[Rails g.error]
Rails Review 1
Rails API
Rails migration
Java method
to_i method
java (method)
[Rails] first_or_initialize
Rails enum Select prefecture by pull-down method
getRequestDispatcher () method
merge method
Rails 5 Code Reading Part 1 ~ ActiveRecord new Method ~
Ruby on Rails installation method [Mac edition]
About Rails 6
[Rails] How to use the map method
Rails foundation
Rails memorandum
Map method
include method
rails tutorial
Abstract method
rails tutorial
initialize method
rails tutorial
List method
puts method
Java method
Class method
save! method
getParameter method
[Java] method
[Rails] devise
rails tutorial
[Rails] Method summary for conversion / verification / search
private method
rails tutorial
Rails Tips
[Rails] How to use helper method, confimartion
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
[Java] method
[Order method] Set the order of data in Rails
[Rails 6] method :: delete cannot be used with link_to
[Rails] Difference between create method and new + save method
Rails error undefined method ʻimage_name'for nil: NilClass handling
Ruby on Rails address automatic input implementation method
[Ruby on Rails] How to use session method