问题
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