Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

6 participants

@frozenmouse

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

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

@manuelmeurer

+1 Just ran into this problem...

@harlow harlow was assigned
@harlow
Collaborator

@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.

@manuelmeurer

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

@yfeldblum

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

@manuelmeurer

Seems to be the case. Thanks for the explanation!

@harlow
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  app/controllers/high_voltage/pages_controller.rb
@@ -1,5 +1,3 @@
-require 'high_voltage/page_finder'
-
class HighVoltage::PagesController < ApplicationController
unloadable
layout Proc.new { |_| HighVoltage.layout }
View
2  lib/high_voltage.rb
@@ -15,4 +15,6 @@ def self.setup
end
require 'high_voltage/engine' if defined?(Rails)
+
+ autoload :PageFinder, 'high_voltage/page_finder'
end
Something went wrong with that request. Please try again.