Search code examples
ruby-on-railsrails-engines

Accessing helpers and models from rails engine initializer


I'm trying to make a Ruby on Rails engine, and I want the initializer to be able to have access to the helpers and models.
I'll write below an example, part of the code, and the error that I have. It may not be the recommended way, because I can see that in some cases I'm repeating myself, but it's the first engine I make.

file lib/my_engine/engine.rb

module MyEngine
  require 'my_engine/functions'

  class Engine < ::Rails::Engine
    isolate_namespace MyEngine
    config.autoload_paths += %W( #{config.root}/lib )
  end

  class GlobalVars
    attr_accessor :foo
    def initialize
      @foo = MyEngine::Functions.new
    end
  end

  class << self
    mattr_accessor :GLOBAL
    mattr_accessor :USER_CONFIG

    self.GLOBAL = MyEngine::GlobalVars.new
    # add default values of more config vars here
    self.USER_CONFIG = 'default config'
  end

  def self.setup(&block)
    yield self
  end

end 

file lib/my_engine/functions.rb

module MyEngine
  require '../../app/helpers/my_engine/options_helper'

  class Functions
    include MyEngine::OptionsHelper

    attr_accessor :some_link

    def initialize
      @some_link = get_option('dummy')
    end
  end
end

There is also a controller named OptionsController in app/controllers/my_engine, and OptionsHelper in app/helpers/my_engine/options_helper.rb:

module MyEngine
  module OptionsHelper
    def get_option(name)
      MyEngine::Option.new
    end
  end
end 

When I try to run the dummy application, this error occurs:

/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)

If I change to just Option.new, I have this error:

/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::OptionsHelper::Option (NameError)

For ::MyEngine::Option.new, I have:

/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)

For ::Option.new, I have:

/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant Option (NameError)

The dummy application has nothing in it. All helpers and models defined above are in the engine.

Before this, I had other errors because it couldn't access the helper, or the Functions class. I had to add require and include to make it work even if they are placed in the same directory. Also, to work, I had to move GlobalVars from its own file inside engine.rb.

Can somebody show me what I'm doing wrong?


Solution

  • After I used required for every class, I ended with ActiveRecord::ConnectionNotEstablished, and it seems that not everything is loaded and available at that point when the GLOBAL object is created.

    So I moved the code that was using the models in a separate init method. Then, I added an after initialize event:

      config.after_initialize do
        MyEngine.GLOBAL.init
      end