Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolated engine (doorkeeper) - use helper methods from the main_app

I want my doorkeeper views to use the application layout:

https://github.com/applicake/doorkeeper/wiki/Customizing-views

This contains routes and helper methods from the main application.

For the routes, I can prefix main_app to the path but for the helper method I get the following error:

undefined method `is_active?' for #<ActionDispatch::Routing::RoutesProxy:0xade808c>

<li class="<%= main_app.is_active?("high_voltage/pages", "api") %>"><%= link_to t('developers'), page_path('api') %></li>

Why is this? The helper is in app/helpers/application_helper.rb

like image 652
DanS Avatar asked Dec 26 '22 23:12

DanS


2 Answers

If you generated the views and they are placed in app/views/doorkeeper/** then the engine still uses doorkeeper controllers.

To fix this, you have to include your helper(s) into the engine's ApplicationController. Let's say you have something like this:

app/helpers/application_helper.rb

module ApplicationHelper
  def my_helper
    "hello"
  end
end

app/views/doorkeeper/applications/index.html.erb

<p>
  <%= my_helper %>
</p>

This won't work until you include your application helpers into doorkeeper controllers. So in config/application.rb:

class YourApp::Application < Rails::Application
  config.to_prepare do
    # include only the ApplicationHelper module
    Doorkeeper::ApplicationController.helper ApplicationHelper

    # include all helpers from your application
    Doorkeeper::ApplicationController.helper YourApp::Application.helpers
  end
end

this is similar configuration when you want to customize the layout.

like image 141
Felipe Elias Philipp Avatar answered May 01 '23 15:05

Felipe Elias Philipp


A helper method in application_helper.rb would not be a method for main_app.

The main_app variable is an object with a class/module of ActionDispatch::Routing::RoutesProxy.

main_app is a helper that gives you access to your application routes. main_app.page_path('api'), for example.

I'm assuming, with doorkeeper, you need to access the path you want; main_app.highvoltage_page_path('api').some_doorkeeper_active_method

This should hopefully, at least, send you in the right direction, see also:

http://edgeapi.rubyonrails.org/classes/Rails/Engine.html#label-Using+Engine%27s+routes+outside+Engine

Good luck.

like image 40
Jon-Paul Lussier Avatar answered May 01 '23 15:05

Jon-Paul Lussier