Tim Baas
Tim Baas

Reputation: 6185

uninitialized constant in My::Engine after file change

I'm developing a gem/engine. The way I do this is by bundling it in a test RailsApp from source:

# Gemfile
gem 'my-engine', path: '../local/path/to/gem'

This works fine so far.

But, after I change a file in my gem (add a space or a break for example) the Engine is unloaded. Causing the following error:

uninitialized constant My::Engine

This error is thrown by the file that does the first call to My::Engine. ( I need to call that to get the root: My::Engine.root ) If I delete that line, there are no error thrown, but just an empty page is rendered, and this is happening because all my SQL's change and no content is loaded from the database. I think this is because the files in the lib dir are unloaded, because in these files I dynamically create active-record models..

I already checked out the autoload_paths and watchable_dirs:

# engine.rb
module My

  class Engine < Rails::Engine

    engine_name 'my-engine'

    initializer "my-engine.load_config" do |app|
      app.config.autoload_paths += %W(#{Engine.root}/lib) 
      app.config.watchable_dirs["#{Engine.root}/lib"] = [:rb]
    end

  end

end

I'm not sure if I'm implementing these the right way, but they don't seem to solve my problems the way I'm using them.

Upvotes: 3

Views: 2916

Answers (1)

Zach Colon
Zach Colon

Reputation: 177

I think you may need to require 'my/engine' before calling My::Engine.root, or change the order of your requires so that 'my/engine' is required prior to the file that makes a call to My::Engine.

Upvotes: 0

Related Questions