Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Rails engine's URL helpers in initializer

I'm trying to access the url helpers in my engine to set up rack-cors. Right now, I've hard-coded the strings for one of the urls in the rack-cors middleware configuration. I have read the order in which Rails initializers are run, and at this point in the load order I should have the engine routes available to me. I thought I would have them available at the event add_routing_paths, but I couldn't find the routes after digging around using pry. Another statement that leads me to think I'm doing this incorrectly is that the docs say: "Some parts of your application, notably routing, are not yet set up at the point where the after_initialize block is called." According to this list:

  1. require "config/boot.rb" to setup load paths
  2. require railties and engines
  3. Define Rails.application as "class MyApp::Application < Rails::Application"
  4. Run config.before_configuration callbacks
  5. Load config/environments/ENV.rb
  6. Run config.before_initialize callbacks
  7. Run Railtie#initializer defined by railties, engines and application. One by one, each engine sets up its load paths, routes and runs its config/initializers/* files.
  8. Custom Railtie#initializers added by railties, engines and applications are executed
  9. Build the middleware stack and run to_prepare callbacks
  10. Run config.before_eager_load and eager_load! if eager_load is true
  11. Run config.after_initialize callbacks

I'm trying to hook into (7), but perhaps routes aren't available until (11)?

module Zillow
  class Engine < ::Rails::Engine
    isolate_namespace Zillow

    # Rails.application.routes.url_helpers

    initializer "zillow.cors", after: :set_routes_reloader do |app|
      require 'pry'; binding.pry
      app.config.app_middleware.insert_before 0, Rack::Cors do
        allow do
          origins 'localhost:3000'
          resource '/zillow/search_results', methods: :get
        end
      end
    end
  end
end

Here is the output of my routes

zillow  /zillow Zillow::Engine

Routes for Zillow::Engine:
              api_deep_comps GET /api/deep_comps(.:format)               zillow/api#deep_comps
               api_zestimate GET /api/zestimate(.:format)                zillow/api#zestimate
          api_search_results GET /api/search_results(.:format)           zillow/api#search_results
api_updated_property_details GET /api/updated_property_details(.:format) zillow/api#updated_property_details
like image 787
Mieczysław Daniel Dyba Avatar asked Mar 06 '16 07:03

Mieczysław Daniel Dyba


1 Answers

You can fire your own event when routes are loaded and then subscribe to that event during initialization to get routes data. For that:

  1. Add this to the end of config/routes.rb file (outside of routes.draw block)
ActiveSupport::Notifications.instrument 'routes_loaded.application'
  1. Subscribe to this event in initialization code and use URL helpers!
ActiveSupport::Notifications.subscribe 'routes_loaded.application' do
  Rails.logger.info Rails.application.routes.url_helpers.home_path
end

For more information see:

  • Subscribing to an event
  • Creating custom events
like image 116
Anton Styagun Avatar answered Nov 18 '22 00:11

Anton Styagun