Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing helpers from the parent app in an isolated Rails engine

I'm writing a configurable Rails engine. I have an authentication_helper configuration option to define which helper should be called in a before_action in all controllers needing authentication.

The problem is that I don't have access to the parent app's helpers from the engine's controllers. My understanding is that this happens because the engine is isolated.

I have considered using a block instead of a method name, but I'm not sure if that would work, or if I would be able to cleanly access the authorization logic from outside my controllers.

Active Admin, which I have used in the past, has a similar configuration option. I have noticed that their engine is not isolated, so perhaps I'm overrating the importance of engine isolation?

Is there an elegant way to have the benefits of engine isolation while also allowing this kind of customization? Or should I just forego isolation altogether?

EDIT #1

Brad Werth pointed my in the right direction, as this works with a regular controller inheriting from ApplicationController::Base:

module MyBigFancyEngine
  class Engine < Rails::Engine

    isolate_namespace MyBigFancyEngine  

    config.to_prepare do
      # Make the implementing application's helpers available to the engine.
      # This is required for the overriding of engine views and helpers to work correctly.
      MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
    end

  end
end

However, my engine's ApplicationController inherits from RocketPant::Base, which does not provide a helper method. I've tried to use a simple include (which works fine for regular controllers), but that doesn't work either (the controller can't find the helper).

Any ideas?

like image 991
Alessandro Desantis Avatar asked Aug 07 '15 12:08

Alessandro Desantis


2 Answers

You can expose the implementing application's helpers available to the engine by including the following code in your engine.rb file:

engine.rb

module MyBigFancyEngine
  class Engine < Rails::Engine

    isolate_namespace MyBigFancyEngine  

    config.to_prepare do
      # Make the implementing application's helpers available to the engine.
      # This is required for the overriding of engine views and helpers to work correctly.
      MyBigFancyEngine::ApplicationController.helper Rails.application.helpers
    end

  end
end
like image 173
Brad Werth Avatar answered Oct 01 '22 04:10

Brad Werth


The RailsAdmin Engine is also isolated, but they have the same configuration options as you would like to implement. They have configurable before_filters for both authentication and authorization. Have a look at this.

As far as I can tell, they just subclass the parent controller like this::ApplicationController or instead you can configure one (ref).

For your controller you could just create your own EngineController, that inherits from RocketPant::Base and maybe just create a method there that calls the configured authentication method directly via send on the parent controller.

As the RocketPant::Base Class does not inherit from ApplicationController::Base I guess you have to find some custom way around this and can't go the normal ways for Rails Engines. Maybe you could also try to file an issue to the rocket_pant repo, to add the helper method. As far as I read they soon want to inherit from ApplicationController::Base anyway in 2.0 (ref).

like image 35
smallbutton Avatar answered Oct 01 '22 04:10

smallbutton