routing-filter 围绕 Rails 路由系统的复杂性进行封装,从而为 Rails URL 识别和生成提供了无与伦比的灵活性和强大功能。
routing-filter 围绕 Rails 路由系统的复杂性进行封装,从而为 Rails URL 识别和生成提供了无与伦比的灵活性和强大功能。
Routing filters wrap around the complex beast that the Rails routing system is to allow for unseen flexibility and power in Rails URL recognition and generation.
As powerful and awesome the Rails' routes are, when you need to design your URLs in a manner that only slightly leaves the paved cowpaths of Rails conventions, you're usually unable to use all the goodness of helpers and convenience that Rails ships with.
This library comes with four more or less reusable filters and it is easy to implement custom ones. Maybe the most popular one is the Locale routing filter:
Locale - prepends the page's :locale (e.g. /de/products)Pagination - appends page/:num (e.g. /products/page/2)Uuid - prepends a uuid for authentication or other purposes (e.g. /d00fbbd1-82b6-4c1a-a57d-098d529d6854/products/1)Extension - appends an extension (e.g. /products.html)Latest routing-filter (~> 0.7.0) only works with Rails >= 6.1. It should
not be all too hard to get it working with plain Rack::Mount but I haven't had that
usecase, yet.
For older Rails versions you have to use respective older releases.
Just install the Gem:
$ gem install routing-filterThe Gem should work out of the box with Rails 6.1 after specifying it in your application's Gemfile.
# Gemfile
gem 'routing-filter'Once the Gem has loaded you can setup the filters in your routes file like this:
# in config/routes.rb
Rails.application.routes.draw do
filter :pagination, :uuid
endFilters can also accept options:
Rails.application.routes.draw do
filter :extension, :exclude => %r(^admin/)
filter :locale, :exclude => /^\/admin/
endThe locale filter may be configured to not include the default locale:
# in config/initializers/routing_filter.rb
# Do not include default locale in generated URLs
RoutingFilter::Locale.include_default_locale = false
# Then if the default locale is :de
# products_path(:locale => 'de') => /products
# products_path(:locale => 'en') => /en/productsRoutingFilter should not be enabled in functional tests by default since the Rails router gets bypassed for most testcases. Having RoutingFilter enabled in this setup can cause missing parameters in the test environment. Routing tests can/should re-enable RoutingFilter since the whole routing stack gets executed for these testcases.
To disable RoutingFilter in your test suite add the following to your test_helper.rb / spec_helper.rb:
RoutingFilter.active = false$ bundle install
$ bundle exec rake testYou can picture the way routing-filter wraps filters around your application as a russian puppet pattern. Your application sits in the center and is wrapped by a number of filters. An incoming request's path will be passed through these layers of filters from the outside in until it is passed to the regular application routes set. When you generate URLs on the other hand then the filters will be run from the inside out.
Filter order might be confusing at first. The reason for that is that the way rack/mount (which is used by Rails as a core routing engine) is confusing in this respect and Rails tries to make the best of it.
Suppose you have a filter :custom in your application routes.rb file and an engine that adds a :common filter. Then Rails makes it so that your application's routes file will be loaded first (basically route.rb files are loaded in reverse engine load order).
Thus routing-filter will make your :custom filter the inner-most filter, wrapping the application first. The :common filter from your engine will be wrapped around that onion and will be made the outer-most filter.
This way common base filters (such as the locale filter) can run first and do not need to know about the specifics of other (more specialized, custom) filters. Custom filters on the other hand can easily take into account that common filters might already have run and adjust accordingly.
For example implementations have a look at the existing filters in lib/routing_filter/filters
The following would be a skeleton of an empty filter:
module RoutingFilter
class Awesomeness
License: MIT暂无开放 Issues,或尚未同步最近议题。