使用带有继承资源的演示者
我在 Rails 应用程序中经常使用优秀的 Inherited Resources gem,但每当我想使用演示器时,我都会转而手动编写控制器。
是否有一个好的、干净的解决方案将继承资源与 Draper 或 delegate_presenter 之类的东西结合起来?
I use the excellent Inherited Resources gem quite a bit in my Rails apps, but whenever I want to use a presenter I fall back to writing controllers by hand.
Is there a good, clean solution for combining Inherited Resources with something like Draper or delegate_presenter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里是
delegate_presenter
的作者。delegate_presenter
应该可以很好地与inherited_resources
配合使用。现在我还没有在项目中实际使用inherited_resources,所以很多内容只是来自自述文件。无论如何,我总是在视图中创建演示者对象:例如,
todo_present = Present(@todo)
。如果您拥有来自公共源的模板(例如
LegalTodo
是Todo
的子类,出于某种未知的原因),并且您共享模板,则可以在你的模板:presenter = Present(resource)
resource
- 至少根据inherited_resources自述文件 - 是你正在查看的当前资源的帮助器(所以,@todo
或@legal_todo
)Present()
将查看对象的类并实例化适当的演示者对象。TodoPresenter
如果resource
返回一个Todo
对象,LegalTodoPresenter
如果resource
返回一个>LegalTodo
对象。然后,假设这些演示者是多态的,演示者将让您抽象出差异(“
LegalTodo
的名称是描述 + 沉积编号,但Todo
的名称> 项目应该只是描述”) - 即使您分享观点!Author of
delegate_presenter
here.delegate_presenter
should work just fine withinherited_resources
. Now I haven't actually used inherited_resources on a project, so a lot of this is just from the readme.What I always to do create the presenter object in the view anyway:
todo_present = Present(@todo)
, for example.If you are having templates from a common source (like
LegalTodo
is a subclass ofTodo
, for some bog-unknown reason), and you share templates, you could do this in your template:presenter = Present(resource)
resource
- at least according to the inherited_resources readme - is the helper for the current resource your looking at (so,@todo
, or@legal_todo
)Present()
will look at the class of the object and instantiate the appropriate presenter object.TodoPresenter
ifresource
returns aTodo
object,LegalTodoPresenter
ifresource
returns aLegalTodo
object.Then, assuming those presenters are polymorphic, the presenters will let you abstract the differences away ("The name of a
LegalTodo
be the description + deposition number, but the name of theTodo
item should just be description") - even if you share views!