How to include Draper Decorators in Rails Console?

为君一笑 提交于 2019-12-12 05:28:10

问题


I'd like to see the output of some of my Draper Decorators in Rails console (https://github.com/drapergem/draper and http://railscasts.com/episodes/286-draper). To do so, I was looking for a way to include a decorator and its methods similar to as we would an application helper:

 include ActionView::ApplicationHelper

A draper decorator inherits from an ApplicationDecorator, which inherits from Draper::Base

 class ApplicationDecorator < Draper::Base
 end

 class MaterialDecorator < ApplicationDecorator
    decorates :material
    #methods to decorate your material..
 end

I've tried include Draper::Base::MaterialDecorator, include ApplicationDecorator::MaterialDecorator, and some other similar variations with no luck.

How can I include a decorator such as the Material Decorator above in rails console?


回答1:


So, it turns out (unless someone can show otherwise) that this isn't the way you test draper decorator output in rails console..

Instead, you just do:

 material = Material.first
 material = MaterialDecorator.decorate(material)
 material.some_decorator_method



回答2:


Add app/decorators to the Rails autoload path:

in config/application.rb:

config.autoload_paths << Rails.root.join('app/decorators')

then, the decorator classes under that path should automatically be loaded, even in the rails console. Example:

Material.first.decorate.some_decorator_method # => "good stuff"


来源:https://stackoverflow.com/questions/13557550/how-to-include-draper-decorators-in-rails-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!