了解 Rails Presenter 中的产量 [英] Understanding yield in rails presenter

查看:35
本文介绍了了解 Rails Presenter 中的产量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在阅读的Rails View"一书中,有关于演示者的代码

In 'Rails View' book that I'm reading, there is code about presenters

app/helpers/designers_helper.rb

module DesignerHelper
  def designer_status_for(designer = @designer)
    presenter = DesignerStatus.new(designer)
    if block_given?
      yield presenter
    else
      presenter 
    end
  end
end

然后在 show.html.erb

then in show.html.erb

<% designer_status_for do |status| %>
   some html using <%= status %> variable

<% end %>

我很难理解代码块如何获得分配的变量状态,为什么我们需要在辅助函数中使用if"语句,为什么不直接返回 Presenter 变量?

I'm having hard time understanding how the block of code gets the variable status assigned and why do we need 'if' statement in the helper function and why not just return the presenter variable directly?

推荐答案

好的,让我们一步一步来.

Ok, lets do this step by step.

首先,您定义一个辅助方法,该方法将 @designer 指定为默认值.这就是为什么您可以在没有任何参数的情况下调用它的原因:

Firstly, you define a helper-method which assigns @designer as default. This is the reason, why you can call it without any arguments in your view:

<% designer_status_for do |status| %>
  # more code

然后,DesignerStatus 是实际的presenter,它与Designer 是不同的类并创建一个全新的对象,但它有点基于@designer 对象.所以,presenter 是一个 DesignerStatus

Then, DesignerStatus is the actual presenter, which is a different class than Designer and creates a total new object, but which is sort of based on the @designer object. So, presenter is a new object of type DesignerStatus

block_given?,正如你可能猜到的,检查这个对象是否应该传递给一个块或者它是否应该作为一个普通对象工作.这很可能是您难以理解的棘手部分.所以让我换个角度来解释一下.你可以这样调用

block_given?, as you might guess, checks, if this object should be passed to a block or if it should work as a normal object. This is most likely the tricky part where you are having trouble understanding. So let me explain it the other way around. You can call something like this

designer_status_for.some_presenter_method

在您看来,它会起作用.它有效,因为没有给出块并且返回一个 designer 对象(换句话说,触发了 else 部分),您可以在其上调用 some_presenter_method 直接.所以,你可以在整个视图中使用它,你会没事的.

in your view, and it would work. It works, because no block is given and a designer object is returned (in other words, the else part is triggered), on which you can call some_presenter_method directly. So, you could use that all throughout your view, and you would be just fine.

然而,还有一个更好的选择,即yield这个对象变成一个块.yield 选项使这个代码片段

However, there is an even better option, namely yield this object into a block. The yield option is what makes this code-snippet

<% designer_status_for do |status| %>
  status.some_presenter_method
<% end %>

首先是可能的.这基本上是说获取该演示者对象,将其分配给一个名为 status 的变量,并使该变量可用于该块.

possible in the first place. What this basically says is "take that presenter object, assign it to a variable called status and make this variable available to the block.

因此,如果您现在很难解决 yield 问题,请不要太担心.只需调用 designer_status_for 本身的任何方法.使用块而不是对象本身的好处是,您的代码变得不那么脆弱,但这是另一个话题.

So, if you are having a hard time to wrap your head around yield right now, don't worry to much about it. Just call any method on designer_status_for itself. The benefit of using the block over the object itself is the fact, that your code becomes less brittle, but that's sort of another topic.

这篇关于了解 Rails Presenter 中的产量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆