route_translator icon indicating copy to clipboard operation
route_translator copied to clipboard

Putting an engine in a localized block breaks engine routing method

Open rvasquez-flip4new opened this issue 7 years ago • 10 comments

Assume you have an engine called Blorgh. And you mount it like this:

Rails.application.routes.draw do
  localized do
    mount Blorgh::Engine,      at: '/blorgh'
  end
end

It is now impossible to call blorgh.unicorns_path in a view. Instead of this you have to append the locale like this:

blorgh_en.unicorns_path

I'm on rails 5.0/5.1 and ruby 2.3.x and 2.4.1 and route translator 5.4.0.

Any clues how to fix this?

Thanks in advance!

Bes tregards Roberto

rvasquez-flip4new avatar May 12 '17 07:05 rvasquez-flip4new

I worked around this adding a method to the application helper like this, but it does not feel right:

module ApplicationHelper
  def blorgh
    send("blorgh_#{I18n.locale}")
  end
end

rvasquez-flip4new avatar May 12 '17 07:05 rvasquez-flip4new

Refers to #154?

tagliala avatar May 12 '17 09:05 tagliala

I think so. I remember that discussion. It also had to do with #123 (which was openend by me too ;) )

rvasquez-flip4new avatar May 12 '17 09:05 rvasquez-flip4new

I'm running into the next problem, even with the "hack" I posted above, the generated urls are missing the locale for the main_app if any engines are mounted at '/'.

Example:

main_app.root_path should generate /de, but instead generates /

force_locale is set to true in that example, but it does not seem to matter.

rvasquez-flip4new avatar May 12 '17 09:05 rvasquez-flip4new

Feel free to submit a PR or start a wiki page explaining the limitation with engines

tagliala avatar May 12 '17 12:05 tagliala

I had the same problem, that my engine routing proxy was not available anymore and I think I've found a better solution to just add it in an ApplicationHelper (that worked not good enough for me, cause I found myself hacking this in multiple different classes, just to get this work). Rails defines these kind of helpers in a module called MountedHelpers in the https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb So we can just extend this class on the fly, so in my routes.rb after the route definitions I added something like this:

Rails.application.routes.mounted_helpers.class_eval do
  def blorgh
    public_send("blorgh_#{I18n.locale}")
  end
end

so this is really everywhere accessible where blorgh_en is for example.

Maybe route_translator can do this automatically while processing an engine mount? So this would address at least one of the problems.

aiomaster avatar Jan 13 '21 12:01 aiomaster

Maybe route_translator can do this automatically while processing an engine mount?

I'm not using Route Translator in production in any of my current projects, so a PR with tests and documentation would be very welcomed

PS: because of this issue, I have a development branch where the engine feature has been completely removed

tagliala avatar Jan 13 '21 17:01 tagliala

Relevant commit: f70f6f7c8ac96df1b8ec39d5947c2370fd260901

Commit message

The current engine implementation is buggy and incomplete.

We are going to drop the whole feature, waiting for a new contributor

tagliala avatar Jan 13 '21 17:01 tagliala

Release 10.0 will still have engine support

tagliala avatar Jan 16 '21 10:01 tagliala

I tried, but was not able to write a failing test at the top of the current master. It seems like there were no issues, at least I can not reproduce them in test. But I know we had the issue with the missing routing_proxy helper, but we are using rails 5 in production. Maybe it just works with rails 6?

aiomaster avatar Jan 20 '21 17:01 aiomaster