3

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.

Tim Baas
  • 6,035
  • 5
  • 45
  • 72
  • I'm interested in seeing the answers you get. I had the same issue with developing an engine. My 'solution' was to do something like `require 'my'` in the `application.rb` of my main_app (the host rails application, not the engine). This worked for me, but doesn't seem like it should be necessary. – Jacob Brown Jun 24 '13 at 15:46
  • Thanks for the tip, I'll try that for now.. But there must be a better way indeed.. – Tim Baas Jun 24 '13 at 16:13

1 Answers1

0

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.

Zach Colon
  • 177
  • 2
  • 11