0

So I see some code in a Rails 4.2 app like this the below.

1) What type of object is @template in kite_presenter.rb? Is it an instance of ActionView and that is how it has access to helpers such as image_tag? 2) If the answer to 1 is that is an instance of Actionview, in application_helper.rb, how does self know to refer to an ActionView object?

kite_presenter.rb

class KitePresenter < SimpleDelegator
  def initialize(kite, template)
    super(kite)
    @template = template
  end
  def tail_display
    h.image_tag("tail.png", class: 'gray')
  end
end

application_helper.rb

module ApplicationHelper

  def present(object, klass = nil)
    klass ||= "#{object.class}Presenter".constantize
    presenter = klass.new(object, self)
    yield presenter if block_given?
    presenter
  end

end

some_html.erb

   <% present(kite) do |kite_presenter| %>
     <%= kite_presenter.tail_display %>
   <% end %>
Nona
  • 5,302
  • 7
  • 41
  • 79
  • What joe woodward said is correct, it is indeed an instance of `ActionView`. I cannot find the section of code in the rails source that demonstrates this, but to your second question, the module is mixed in with the view. To be exact, its an `include`, which adds the methods in the module to the instance, which is why `self` points to `ActionView`. – Paul Richter May 02 '16 at 14:30

1 Answers1

0

Correct, it is the current view object itself.

I've written a blog post on PORO presenters which should help a little

http://joewoodward.me/i-take-it-back/

joewoodward
  • 263
  • 4
  • 10
  • 2
    Posting a link should not be considered as an answer. You should add link as a comment to the question OR provide answer on SO itself and can point to your blog, if needed. – RAJ Apr 28 '16 at 18:18