autoload HighVoltage::PageFinder to avoid uninitialized constant error #45

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
6 participants

The following error raises every time after changing some files (or even just touching them, e.g., touch app/controllers/application_controller.rb) in my project and then visiting any static page without restarting the server:

NameError in HighVoltage::PagesController#show
uninitialized constant HighVoltage::PageFinder

I tried the following code in Rails console:

1.9.3p194 :002 > HighVoltage::PagesController
 => HighVoltage::PagesController 
1.9.3p194 :003 > HighVoltage::PageFinder
 => HighVoltage::PageFinder 
1.9.3p194 :004 > reload!
Reloading...
 => true 
1.9.3p194 :005 > HighVoltage::PagesController
 => HighVoltage::PagesController 
1.9.3p194 :006 > HighVoltage::PageFinder
NameError: uninitialized constant HighVoltage::PageFinder
    from (irb):6
    from /home/frozenmouse/.rvm/gems/ruby-1.9.3-p194@<my-project>/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /home/frozenmouse/.rvm/gems/ruby-1.9.3-p194@<my-project>/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /home/frozenmouse/.rvm/gems/ruby-1.9.3-p194@<my-project>/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

It seems that PageFinder was not getting reloaded.

Using autoload in module HighVoltage works for me, and the require directive in pages_controller.rb is no longer needed. But I have no idea how to write test for this...

zben commented Oct 12, 2012

Just verified it. This is a problem with the master branch now. We will make the change soon.

+1

+1 Just ran into this problem...

@ghost ghost assigned harlow Oct 26, 2012

Collaborator

harlow commented Oct 26, 2012

@frozenmouse thanks for working with us on this. Historically we've shy'd away from autoload could we require high_voltage/page_finder explicitly?

require 'high_voltage/version'
require 'high_voltage/page_finder'

module HighVoltage
  #...
end

Let me know if that works for you. If you could update the PR I'll get it merged into master ASAP.

@harlow Can you explain why you don't like autoload? Just wondering...

Code-loading, including autoload, is not guaranteed to be thread-safe so far as I am aware.

Seems to be the case. Thanks for the explanation!

Collaborator

harlow commented Oct 29, 2012

Added the require to lib/high_voltage.rb, has been added to master:
https://github.com/thoughtbot/high_voltage/blob/master/lib/high_voltage.rb#L3

@harlow harlow closed this Oct 29, 2012

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