12

I'm currently experimenting with moving functionality into engines. It works nicely so far, but I'm a bit confused why certain parts of the engine are always automatically reloaded when something changed, and some are not.

Specifically, when adding a helper method, I have to restart the Rails server, otherwise it is not seen by Rails. Is this normal behavior? Here's the relevant part of my engine:

components/iq_list.rb

# encoding: utf-8
require 'iq_list/engine'

# Load IqList Modules
module IqList
  extend ActiveSupport::Autoload
  autoload :Helpers
  autoload :Models
  autoload :Controllers
end

components/iq_list/engine.rb

module IqList
  class Engine < ::Rails::Engine
  end
end

components/iq_list/helpers.rb

module IqList
  module Helpers
    extend ActiveSupport::Autoload
    autoload :IqListHelper
  end
end

components/iq_list/helpers/iq_list_helper.rb

module IqList
  module Helpers
    module IqListHelper
      def some_method
        # ...
      end
    end
  end
end

I'm still very new to engines, and a lot of the code above I have taken from somebody else's work, so please be patient with me. Any hint into the right direction is highly appreciated.

Kara
  • 6,115
  • 16
  • 50
  • 57
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
  • 1
    http://stackoverflow.com/questions/4713066/how-can-i-force-my-plugin-to-reload-with-each-request – apneadiving Aug 30 '12 at 08:17
  • Why is your code underneath a `components` directory? What is making you do that? Components are an ancient feature of Rails (1.2). – Ryan Bigg Aug 30 '12 at 08:28
  • Where would you place it? As said, I've taken the code from somebody else, and I think it's because within `app/components` stuff is getting reloaded properly. Where would you place an engine you're heavily developing? – Joshua Muheim Aug 30 '12 at 09:35
  • 3
    The engines are normally placed under `vendor/engines` directory and it should get loaded automatically. Please refer (the documentation)[http://guides.rubyonrails.org/engines.html] on how to get started with engines – leenasn Jul 08 '14 at 08:07

2 Answers2

1

It appears that you may be barking up the wrong tree with Engines. If you're trying to simply achieve separation of concerns, you probably just want to make some plain old ruby classes and stick them in lib/ (in an organized way of course).

An Engine would be developed separately from your 'current' project at likely brought in through a gem. Changes in included gems would necessitate restarting your server AFAIK.

Robert Pitts
  • 78
  • 1
  • 5
1

If you need code from your engine reloaded on every request you need to place it in the to_prepare block of the engines intialization code

module IqList
  class Engine < ::Rails::Engine
    config.to_prepare do
     ApplicationController.helper(IqListHelper)
    end
  end
end

Code in the to_prepare block is guaranteed to run once in production and every time in development.

see the rails guides as well as What does this Rails Engine code mean: config.to_prepare &method(:activate).to_proc

and

http://robots.thoughtbot.com/tips-for-writing-your-own-rails-engine

Community
  • 1
  • 1
hraynaud
  • 681
  • 7
  • 15
  • If you use the module notation and your Engine has its own ApplicationController, you should use `::ApplicationController.helper(IqListHelper)` instead. – Sebastian vom Meer Aug 23 '16 at 08:20