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
Explicit loading of ActiveStorage routes #51641
Conversation
I've been thinking about this, and I'd rather extract it to a method and call it, than use the file path. Since that also makes it easier to test 🙏 |
@zzak I pushed a concept. Were you thinking more along the lines of: module ActiveStorage
module Routes
delf self.apply(router)
router.instance_eval do
# add the routes here
end
end
end
end
# from routes.rb
ActiveStorage::Routes.apply(self) # or could have ActiveStorage.routes call this |
You shouldn't need I've only tested this in very limited context though, so some diligence is required 🙏 |
Unfortunately that calls https://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Resources.html#method-i-draw |
Another option is |
Can you also update the That should make the build green. |
If it doesn't make a difference maybe |
Can you squash your commits please? |
7cf7776
to
78dd8dc
Compare
Can you update the PR description to reflect the current state too, please? |
Done. Thanks for your patience! What's left? Change log? |
Yeah, I'm on the fence whether it should be documented at all. Let's see what others think if we can get a review 🙏 |
Saw this suggested in the other ticket, but I wonder if we should instead follow these conventions: Mainly: ActiveStorage::Engine.routes.draw do
# routes
end |
Won't work because the routes use the
You could get around that error by not isolating the engine ... but that'll open a whole other can of worms. |
Looks like I got it to work ... stay tuned. |
What do you think of this approach? https://github.com/rails/rails/compare/main...justinko:mount-activestorage?expand=1 The routes then look like this: Seems cleaner ... and I think |
After a cursory review, but I think that might be a breaking change. Though possibly having them isolated like that is semantically better. Just based on having to add I also noticed Last thing, for me, I don't want to put any logic inside |
78dd8dc
to
5daaaac
Compare
5daaaac
to
2712eba
Compare
Got some tests to fix. |
This approach isn't going to work because there's a hard dependency on I'm going to go with a different approach. What do you think of something like: # in routes.rb
Rails.application.routes.draw do
scope "/rails/active_storage" do
import ActiveStorage::Engine.routes, as: "rails"
end
end This would just be another way to integrate an engine's routes without mounting. If that's not a good idea then I'm going to move on from this issue. |
I assume you mean If we're trying to get the routes to show up isolated in the I appreciate you taking the time to investigate this, but if all we solve is the open ticket and move the routes into a proper method, then I am happy and will help get a review as I'm very interested in this. Thank you! |
Sorry, let me be more clear. The core problem is route load order, which is caused by an engine using It would be great for an engine to draw its routes within the engine, and let the user decide whether to import those routes into main app or mount them. So a user that wants to have control of when Active Storage routes are loaded, would do this: Rails.application.config.active_storage.draw_routes = false
Rails.application.routes.draw do
import ActiveStorage::Engine.routes
end I haven't looked into feasibility, but in theory it's just merging |
I spiked I just thought of another potential solution. Another spike commencing. |
Okay, I really like this solution: justinko@180a8cf No breaking changes and it allows All it does is create 2 separate sets of routes, one for If they choose to mount, then they'll need to add this to their controller: def rails_direct_uploads_url
active_storage.direct_uploads_url
end If you prefer it, then I'll clean it up and get a pull ready. |
If you prefer |
Starting over with a fresh |
Fixes #31228
Motivation / Background
Users are creating a "catchall" route in routes.rb that will always precede the ActiveStorage routes. Railtie routes that are loaded after the app's routes are getting caught in it.
Detail
Moving the ActiveStorage routes from
Rails.applications.routes
toActiveStorage::Engine.routes
ensures they precede any route defined inRails.application.routes
.If you a user wants explicit loading of the routes, they can do the following:
Additional information
Should we do this for other engines as well?
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]