Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The gem breaks usage of Rails URL helpers when used outside views and controllers #9

Open
mechanicles opened this issue Oct 13, 2021 · 3 comments

Comments

@mechanicles
Copy link

Issue is mentioned here discourse/discourse#14581.

I wanted to use this gem in our application after following this Twitter thread but found above issue so I couldn't use it. I hope that issue will get resolved soon.

@technicalpickles
Copy link

We ran in the same thing. One workaround I'm trying now is:

# config/initializer/console.rb
Rails.application.console do
  Rails.application.routes.url_helpers.instance_eval do
    def method_missing(m, *args, &block)
      if m.to_s.end_with?('_path', '_url')
        RoutesLazyRoutes.eager_load!
        return public_send(m, *args, &block)
      end

      super
    end

    def respond_to_missing?(m, include_private = false)
      m.to_s.end_with?('_path', '_url') || super
    end
  end
end

I've confirmed helpers in Rails.application.routes.url_helpers work after that, but the main unknown for me is how that will interact with reload!.

@technicalpickles
Copy link

To revisit my earlier comment... that workaround didn't work. Partially, it's because the Rails URL helpers aren't normally available in the console from the top level object. The underlying problem I was trying to fix was ActionMailers failing when they used URL helpers.

It came up for someone today, and I had a realization for a fix. In your `config/console

Rails.application.console do
  # Make sure that routes are available to Mailers when running from a console
  if defined?(RoutesLazyRoutes)
    ActiveSupport.on_load(:action_mailer) do
      before_action { RoutesLazyRoutes.eager_load! }
    end
  end

It seems this should be feasible in the railtie, so I can try a patch for this.

@technicalpickles
Copy link

For those using sidekiq, you can update your Sidekiq.configure_server block to be like:

# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
  if defined?(RoutesLazyRoutes)
    Rails.application.config.after_initialize do
      RoutesLazyRoutes.eager_load!
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants