所以我在 Rails 4.2 应用程序中看到了一些代码,如下所示。
1) kite_presenter.rb 中的@template 是什么类型的对象?它是 ActionView 的实例吗,这就是它如何访问诸如image_tag之类的助手的方式?2)如果1的答案是Actionview的一个实例,在application_helper.rb中,self怎么知道引用一个ActionView对象?
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 %>