Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to add before_filter from engine to application

Without a lot of specific code, this is just vague, but I'll provide what I can.

Given a rails engine, a basic engine and not a mountable engine in its own space, how do I make methods from the engine available to the application as a before_filter for the applications controllers?

I've been looking through the Devise code, because what I want to is similar from my experience with Devise, but I'll admit I don't understand a lot of what I'm going over.

I can do this if I put include Myengine::Mymodule in the apps controllers where I want to have the methods available for filtering, but I want it possible to just use the methods without having to include the modules.

This is the latest roadblock in trying to turn a rails application into an engine to be used by multiple rails apps, and any guidance on getting a handle on proper namespacing, module config, etc., is appreciated.

like image 762
blueblank Avatar asked Sep 06 '11 16:09

blueblank


1 Answers

If I understood you correctly you can use initializer, for example:

module MyEngine
    class Engine < Rails::Engine
        initializer  "myengine.load_helpers" do
            ActiveSupport.on_load(:action_controller) do
                include MyEngine::Helpers
            end
        end
    end
end
like image 83
Dmitry Maksimov Avatar answered Nov 15 '22 13:11

Dmitry Maksimov