This project is archived and is in readonly mode.
[PATCH] Named Routes in ActionView Scope Don't Respect AC::Base#default_url_options
-
Michael Koziarski
OK, that fix is way too pessimistic. Turns off the optimisations for everything :)
Given that the model already does a check on defined?(default_url_options) I'm a little confused why it's not working for you.
the best bet is probably to either use a debugger, or logging, and see why / if the defined?(... guards are functioning correctly.
-
Tom Lea
Koz: The guard conditions don't fire because default_url_options is not defined on the action view.
The patch should just disable _url methods and not _path methods... but I agree, this does not feel ideal.
The only other alternative I can see is to add a default_url_options method to the action view (patch attached). I was initially reluctant to add the method directly to ActionView, as the only reason it is needed is because of the optimizations.
The named routes are created on the AV::Base at the same time as they are defined on the AC::Base, but AV::Base has no knowledge of default_url_options, before the optimization we would just ask the controller what to do.
So the third way, would be to ensure that AV::Base#foo_url would delegate to @controller#foo_url. This may be the cleanest option.
What are your thoughts?
If the third option is going to be preferred, let me know and I'll try and get a patch together tonight.
-
DHH
- Assigned user set to Michael Koziarski
-
Tom Lea
I got bored, so here is a possible implementation of delegating named routes away to the controller.
Patch attached.
-
Michael Koziarski
- Milestone cleared.
Sorry for the slow responses on this, been a little snowed under.
So from my understanding, the url_for in ActionView will fallback to the controller's default_url_options. Because of this the faster routes guard conditions don't fire, and the methods return the wrong values.
Does adding helper_method :default_url_options in your controller fix the issue?
-
Tom Lea
So from my understanding, the url_for in ActionView will fallback to the controller's default_url_options. Because of this the faster routes guard conditions don't fire, and the methods return the wrong values.
Correct, that seems to be what I am seeing in testing and production.
Does adding helper_method :default_url_options in your controller fix the issue?
Indeed it does, but it doesn't feel quite right.
How would you feel about extending the guard conditions to include
defined?(@controller.default_url_options) && !@controller.default_url_options.empty?amongst the rest.(once again, patch attached, it seems I just love patches)
-
Michael Koziarski
OK, This looks good to me, but could you tidy up those guard conditions. They're 99% the same now so should be defined in one place with the differences getting shifted on the end.
-
Tom Lea
Right, went a little further, built up an array and then &&'d them, felt more readable, all the requirements with a line each.
Final merged + rebased patch attached.
Thanks for the help.
