New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helpers aren't loaded #6

Closed
rchampourlier opened this Issue Apr 3, 2012 · 30 comments

Comments

Projects
None yet
@rchampourlier

rchampourlier commented Apr 3, 2012

I'm running some tests with Cucumber through spork with spork-rails 3.2.0 and spork 1.0.0rc2 and I cannot get my helper methods to be loaded.

  • Ruby is ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]
  • Rails is 3.0.12

Everything is fine when:

  • I run with spork 1.0.0rc2 only (removing spork-rails from the Gemfile). This is not acceptable since spork does not reload the controllers anymore.
  • I run without spork at all.

I'm using many gems (Devise, FactoryGirl, Mongoid - also using ActiveRecord), but not ActiveAdmin. I say this because this issue is like the issue #109 on spork. Nothing from this issue could help me, and now that spork-rails has been extracted, it seemed to me the issue belonged here.

Here is a gist of my configuration. For now, I will be running my features without Spork...

@rchampourlier

This comment has been minimized.

Show comment
Hide comment
@rchampourlier

rchampourlier Apr 23, 2012

Same issue when running integration/request specs within RSpec.

Any help? Thanks in advance... to anyone!

rchampourlier commented Apr 23, 2012

Same issue when running integration/request specs within RSpec.

Any help? Thanks in advance... to anyone!

@mreinsch

This comment has been minimized.

Show comment
Hide comment
@mreinsch

mreinsch May 2, 2012

Same here. Seems any helpers coming from gems are not being loaded. All is fine when running tests directly...

mreinsch commented May 2, 2012

Same here. Seems any helpers coming from gems are not being loaded. All is fine when running tests directly...

@mreinsch

This comment has been minimized.

Show comment
Hide comment
@mreinsch

mreinsch May 2, 2012

ok, actually all helpers are not being available in the tests. If I disable the handling for reloading helpers (as in my branch at https://github.com/mreinsch/spork-rails), the helpers are fine.

Could this be an ordering issue where the helpers are somehow cleared again after getting loaded? I could confirm that they are being added, just not sure what happens then before the tests start.

mreinsch commented May 2, 2012

ok, actually all helpers are not being available in the tests. If I disable the handling for reloading helpers (as in my branch at https://github.com/mreinsch/spork-rails), the helpers are fine.

Could this be an ordering issue where the helpers are somehow cleared again after getting loaded? I could confirm that they are being added, just not sure what happens then before the tests start.

@evgenyneu

This comment has been minimized.

Show comment
Hide comment
@evgenyneu

evgenyneu Jun 5, 2012

I have the same problem. Devise helper devise_error_messages! is not available in tests and shows error:

undefined method `devise_error_messages!'

Devise includes this helper into DeviseController with method helper DeviseHelper (app/controllers/devise_controller.rb). spork-rails changes the helper method in app_framework/rails.rb, line 61. It them runs add_template_helper(mod) which does not work for DeviseHelper.

To make the Devise helper work, I needed to add the following code to spec_helper.rb

class DeviseController
  include DeviseHelper
  include ActionView::Helpers::TagHelper
  helper_method :devise_error_messages!
  helper_method :content_tag
end

evgenyneu commented Jun 5, 2012

I have the same problem. Devise helper devise_error_messages! is not available in tests and shows error:

undefined method `devise_error_messages!'

Devise includes this helper into DeviseController with method helper DeviseHelper (app/controllers/devise_controller.rb). spork-rails changes the helper method in app_framework/rails.rb, line 61. It them runs add_template_helper(mod) which does not work for DeviseHelper.

To make the Devise helper work, I needed to add the following code to spec_helper.rb

class DeviseController
  include DeviseHelper
  include ActionView::Helpers::TagHelper
  helper_method :devise_error_messages!
  helper_method :content_tag
end
@lidaobing

This comment has been minimized.

Show comment
Hide comment
@lidaobing

lidaobing commented Jun 27, 2012

same issue as sporkrb/spork#109

@tom-kuca

This comment has been minimized.

Show comment
Hide comment
@tom-kuca

tom-kuca Sep 7, 2012

Same issue with rails 3.2.8, spork 1.0.0rc3, spork-rails 3.2.0. I spent a few time trying to find out where the things went wrong.

Given a simple gem which defines module ApplicationHelper with method hello_world and given an application without ApplicationHelper in app/helpers/, the spec which tries to render view with hello_world method fails with undefined method hello_world.

Notes:
  • the gem does not include ApplicationHelper anywhere, it just defines it in it's lib directory
  • the spec passes without spork
  • the spec passes if there is even empty ApplicationHelper in app/helpers/application_helper.rb

I believe the problem is in the method helper. The spork version will raise an exception if ::ActiveSupport::Dependencies.search_for_file(filename) doesn't find the helper file in app/helpers while the original one from ruby AbstractController fallbacks to require 'application_helper'. Since the gem's lib directory is in $LOAD_PATH, the module from gem is loaded and the helper loads the helper methods.

Notes
  • maybe the gem should add helper's in some other way
  • when inheriting from ApplicationControlloer, the method helper is called with ApplicationHelper as an argument

tom-kuca commented Sep 7, 2012

Same issue with rails 3.2.8, spork 1.0.0rc3, spork-rails 3.2.0. I spent a few time trying to find out where the things went wrong.

Given a simple gem which defines module ApplicationHelper with method hello_world and given an application without ApplicationHelper in app/helpers/, the spec which tries to render view with hello_world method fails with undefined method hello_world.

Notes:
  • the gem does not include ApplicationHelper anywhere, it just defines it in it's lib directory
  • the spec passes without spork
  • the spec passes if there is even empty ApplicationHelper in app/helpers/application_helper.rb

I believe the problem is in the method helper. The spork version will raise an exception if ::ActiveSupport::Dependencies.search_for_file(filename) doesn't find the helper file in app/helpers while the original one from ruby AbstractController fallbacks to require 'application_helper'. Since the gem's lib directory is in $LOAD_PATH, the module from gem is loaded and the helper loads the helper methods.

Notes
  • maybe the gem should add helper's in some other way
  • when inheriting from ApplicationControlloer, the method helper is called with ApplicationHelper as an argument
@pedrobachiega

This comment has been minimized.

Show comment
Hide comment
@pedrobachiega

pedrobachiega Sep 11, 2012

same problem to load helpers here...

if I include each helper, it works, but I would know some clean way to do this

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda/matchers/integrations/rspec'
  include ApplicationHelper
  include GraphicsHelper
  ...

pedrobachiega commented Sep 11, 2012

same problem to load helpers here...

if I include each helper, it works, but I would know some clean way to do this

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'shoulda/matchers/integrations/rspec'
  include ApplicationHelper
  include GraphicsHelper
  ...
@vascoosx

This comment has been minimized.

Show comment
Hide comment
@vascoosx

vascoosx Oct 7, 2012

I had the same problem. The only workaround I found was calling those helpers with

ApplicationController.helpers.<helper name> 

vascoosx commented Oct 7, 2012

I had the same problem. The only workaround I found was calling those helpers with

ApplicationController.helpers.<helper name> 
@fweep

This comment has been minimized.

Show comment
Hide comment
@fweep

fweep Nov 23, 2012

This problem still exists. I was digging around the source, and I think it has something to do with how spork-rails overrides the AbstractController::Helpers::ClassMethods#helper(), but I'm not sure, as I wasn't able to fix it there. I did figure out that if I just use spork instead of spork-rails, everything seems to work fine. I'm wondering what the benefit of spork-rails is. It doesn't do too much; disables eager loading (not sure why), loads some Rails environment stuff, and overrides #helper(). Everything runs nice and fast with just plain spork, though.

fweep commented Nov 23, 2012

This problem still exists. I was digging around the source, and I think it has something to do with how spork-rails overrides the AbstractController::Helpers::ClassMethods#helper(), but I'm not sure, as I wasn't able to fix it there. I did figure out that if I just use spork instead of spork-rails, everything seems to work fine. I'm wondering what the benefit of spork-rails is. It doesn't do too much; disables eager loading (not sure why), loads some Rails environment stuff, and overrides #helper(). Everything runs nice and fast with just plain spork, though.

@krishnasrihari

This comment has been minimized.

Show comment
Hide comment
@krishnasrihari

krishnasrihari Dec 6, 2012

I got same issue in spork, I tried in spork-rails gem but same issue. Is there any progress for this issue?

krishnasrihari commented Dec 6, 2012

I got same issue in spork, I tried in spork-rails gem but same issue. Is there any progress for this issue?

@krishnasrihari

This comment has been minimized.

Show comment
Hide comment
@krishnasrihari

krishnasrihari Dec 6, 2012

To fix this issue add these below lines in Spork.prefork block

full_names = Dir["#{Rails.root}/app/helpers/*.rb"]
full_names.collect do |full_name|
    include Object.const_get(File.basename(full_name,'.rb').camelize)
end

krishnasrihari commented Dec 6, 2012

To fix this issue add these below lines in Spork.prefork block

full_names = Dir["#{Rails.root}/app/helpers/*.rb"]
full_names.collect do |full_name|
    include Object.const_get(File.basename(full_name,'.rb').camelize)
end
@pedrobachiega

This comment has been minimized.

Show comment
Hide comment
@pedrobachiega

pedrobachiega Dec 6, 2012

@krishnasrihari this works for me! thanks!

pedrobachiega commented Dec 6, 2012

@krishnasrihari this works for me! thanks!

@eostrom

This comment has been minimized.

Show comment
Hide comment
@eostrom

eostrom Dec 11, 2012

@krishnasrihari's workaround looks like it will cover helpers defined within the application, but not helpers in gems. It could be adapted, but really, my Spork configuration shouldn't have to go find all the gems with helpers in them.

I'm inclined to agree with @fweep's hunch that it has to do with the #helper override. The pull request for sporkrb/spork#140 fixes this problem by removing the override completely, but it was rejected because that caused other problems. (I.e., that code is there for a reason!)

The #helper override was added in sporkrb/spork@d08fee3. It looks like the goal was to ensure helpers are reloaded in each run, so you don't have to restart spork. Good idea, but is it somehow preventing helpers in gems from being loaded at all?

Oh hey, it looks like @tom-kuca's analysis has more details on this. Good job! Now all we need is a fix and an integration test.

eostrom commented Dec 11, 2012

@krishnasrihari's workaround looks like it will cover helpers defined within the application, but not helpers in gems. It could be adapted, but really, my Spork configuration shouldn't have to go find all the gems with helpers in them.

I'm inclined to agree with @fweep's hunch that it has to do with the #helper override. The pull request for sporkrb/spork#140 fixes this problem by removing the override completely, but it was rejected because that caused other problems. (I.e., that code is there for a reason!)

The #helper override was added in sporkrb/spork@d08fee3. It looks like the goal was to ensure helpers are reloaded in each run, so you don't have to restart spork. Good idea, but is it somehow preventing helpers in gems from being loaded at all?

Oh hey, it looks like @tom-kuca's analysis has more details on this. Good job! Now all we need is a fix and an integration test.

@nozpheratu

This comment has been minimized.

Show comment
Hide comment
@nozpheratu

nozpheratu Feb 13, 2013

I'm also having this problem...

nozpheratu commented Feb 13, 2013

I'm also having this problem...

@johnnyshields

This comment has been minimized.

Show comment
Hide comment
@johnnyshields

johnnyshields Feb 14, 2013

Me too. +1

@krishnasrihari's fix worked for me.

johnnyshields commented Feb 14, 2013

Me too. +1

@krishnasrihari's fix worked for me.

@nozpheratu

This comment has been minimized.

Show comment
Hide comment
@nozpheratu

nozpheratu Feb 14, 2013

The fixed that worked for me was:

AbstractController::Helpers::ClassMethods.module_eval do def helper(*args, &block); modules_for_helpers(args).each {|mod| add_template_helper(mod)}; _helpers.module_eval(&block) if block_given?; end end

I don't remember where I got this code, sorry for not giving any credit.

nozpheratu commented Feb 14, 2013

The fixed that worked for me was:

AbstractController::Helpers::ClassMethods.module_eval do def helper(*args, &block); modules_for_helpers(args).each {|mod| add_template_helper(mod)}; _helpers.module_eval(&block) if block_given?; end end

I don't remember where I got this code, sorry for not giving any credit.

@marcusg

This comment has been minimized.

Show comment
Hide comment
@marcusg

marcusg Apr 18, 2013

any progress here?

marcusg commented Apr 18, 2013

any progress here?

@yannp

This comment has been minimized.

Show comment
Hide comment
@yannp

yannp commented Jun 27, 2013

+1

1 similar comment
@epylinkn

This comment has been minimized.

Show comment
Hide comment
@epylinkn

epylinkn commented Aug 3, 2013

+1

@sahilm

This comment has been minimized.

Show comment
Hide comment
@sahilm

sahilm Sep 14, 2013

Contributor

Closing old bugs. If you think this issue needs resolution please open a new issue.

Contributor

sahilm commented Sep 14, 2013

Closing old bugs. If you think this issue needs resolution please open a new issue.

@sahilm sahilm closed this Sep 14, 2013

@johnnyshields

This comment has been minimized.

Show comment
Hide comment
@johnnyshields

johnnyshields Oct 14, 2013

@sahilm why did you close this issue? It's still not fixed, despite many people requesting it.

johnnyshields commented Oct 14, 2013

@sahilm why did you close this issue? It's still not fixed, despite many people requesting it.

@marcusg

This comment has been minimized.

Show comment
Hide comment
@marcusg

marcusg Oct 29, 2013

@sahilm can you reopen this, because it is not fixed. thanks!

marcusg commented Oct 29, 2013

@sahilm can you reopen this, because it is not fixed. thanks!

@brandonparsons

This comment has been minimized.

Show comment
Hide comment
@brandonparsons

brandonparsons Nov 18, 2013

Yes this is still borked.

brandonparsons commented Nov 18, 2013

Yes this is still borked.

@jpemberthy

This comment has been minimized.

Show comment
Hide comment
@jpemberthy

jpemberthy commented Nov 26, 2013

+1

1 similar comment
@jalada

This comment has been minimized.

Show comment
Hide comment
@jalada

jalada commented Dec 3, 2013

👍

@daniel-g

This comment has been minimized.

Show comment
Hide comment
@daniel-g

daniel-g Dec 13, 2013

Yes, it still doesn't work for me. I am using the following:

spork (1.0.0rc4)
spork-rails (4.0.0)

daniel-g commented Dec 13, 2013

Yes, it still doesn't work for me. I am using the following:

spork (1.0.0rc4)
spork-rails (4.0.0)
@johnnyshields

This comment has been minimized.

Show comment
Hide comment
@johnnyshields

johnnyshields Dec 13, 2013

Workaround: use Zeus.

johnnyshields commented Dec 13, 2013

Workaround: use Zeus.

@TheNaoX

This comment has been minimized.

Show comment
Hide comment
@TheNaoX

TheNaoX commented Dec 17, 2013

:(

@Arcolye

This comment has been minimized.

Show comment
Hide comment
@Arcolye

Arcolye Dec 27, 2013

I just installed Spork (+ guard) today, and I'm up against a bug that was first mentioned 2 years ago. Why closed?

Arcolye commented Dec 27, 2013

I just installed Spork (+ guard) today, and I'm up against a bug that was first mentioned 2 years ago. Why closed?

@mihaivinaga

This comment has been minimized.

Show comment
Hide comment
@mihaivinaga

mihaivinaga Mar 15, 2014

I found an easy fix, just add this in your spec_helper, Spork.prefork:

Dir[Rails.root.join("spec/helpers/*.rb")].each { |f| require f }

Also remove "require 'spec_helper'" from your helpers it will cause an error if it's there

mihaivinaga commented Mar 15, 2014

I found an easy fix, just add this in your spec_helper, Spork.prefork:

Dir[Rails.root.join("spec/helpers/*.rb")].each { |f| require f }

Also remove "require 'spec_helper'" from your helpers it will cause an error if it's there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment