draper: cannot access ApplicationHelper

I’ve recently updated Draper to 1.2.0 but now I can’t access ApplicationHelper methods

rails 3.2.11 draper 1.2.0

module ApplicationHelper
  def initials(text)
     [...]
  end
end
class AccountDecorator < ApplicationDecorator
    def officers_initials
        h.initials(officer.name)
    end
end
NoMethodError: undefined method `initials' for #<#<Class:0x000000081107f8>:0x00000009404620>
from /home/duke/.rvm/gems/ruby-1.9.3-p392@bigbang/gems/draper-1.2.0/lib/draper/helper_proxy.rb:29:in `block in define_proxy'

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 19 (12 by maintainers)

Commits related to this issue

Most upvoted comments

It is still not clear how to deal with this problem. We are facing the issue that once the mailer specs run, the controller specs are failing precisely because of this. Draper should be automatically configured so that it clears any cached variables after each spec. If someone wants to optimize the speed of the specs by enabling that, they should have the option, but at their own risk!

I am trying to make it work by adding this to my spec_helper.rb:

  [:decorator, :controller, :mailer].each do |type|
    config.before(:each, type: type) { Draper::ViewContext.clear! }
  end

But it still does not work

EDIT adding the after hook made it work now:

  [:decorator, :controller, :mailer].each do |type|
    config.before(:each, type: type) { Draper::ViewContext.clear! }
    config.after(:each, type: type) { Draper::ViewContext.clear! }
  end

It is still not clear how to deal with this problem. We are facing the issue that once the mailer specs run, the controller specs are failing precisely because of this. Draper should be automatically configured so that it clears any cached variables after each spec. If someone wants to optimize the speed of the specs by enabling that, they should have the option, but at their own risk!

I am trying to make it work by adding this to my spec_helper.rb:

  [:decorator, :controller, :mailer].each do |type|
    config.before(:each, type: type) { Draper::ViewContext.clear! }
  end

But it still does not work

EDIT adding the after hook made it work now:

  [:decorator, :controller, :mailer].each do |type|
    config.before(:each, type: type) { Draper::ViewContext.clear! }
    config.after(:each, type: type) { Draper::ViewContext.clear! }
  end

I just added Draper::ViewContext.clear! to the :feature test settings config.before type: :feature do ... and config.after type: :feature do ...

I had the same problem. In a project I work on, we are using a decorator within a model method (don’t ask me why). The problem occurs when certain mailer specs (which do not use ApplicationHelper) are run before this model’s spec. Adding Draper::ViewContext.clear! to the failing model spec fixes the problem.

@gaizka solution in https://github.com/drapergem/draper/pull/547 does not fix this problem, since the view context is set to the mailers view context without application helper, while it should be reset before the model spec (or after the mailer spec). Maybe we should run Draper::ViewContext.clear! both before and after each test? Or simply run Draper::ViewContext.clear! before each type of test?