百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

routing-filter

> 编程语言
开源

routing-filter 围绕 Rails 路由系统的复杂性进行封装,从而为 Rails URL 识别和生成提供了无与伦比的灵活性和强大功能。

462 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

routing-filter 围绕 Rails 路由系统的复杂性进行封装,从而为 Rails URL 识别和生成提供了无与伦比的灵活性和强大功能。

Routing Filter

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)

Requirements

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.

Installation

Just install the Gem:

$ gem install routing-filter

The Gem should work out of the box with Rails 6.1 after specifying it in your application's Gemfile.

ruby
# Gemfile
gem 'routing-filter'

Usage

Once the Gem has loaded you can setup the filters in your routes file like this:

ruby
# in config/routes.rb
Rails.application.routes.draw do
  filter :pagination, :uuid
end

Filters can also accept options:

ruby
Rails.application.routes.draw do
  filter :extension, :exclude => %r(^admin/)
  filter :locale,    :exclude => /^\/admin/
end

The 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/products

Testing

RoutingFilter 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:

ruby
RoutingFilter.active = false

Running the tests

$ bundle install
$ bundle exec rake test

Filter order

You 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.

Implementing your own filters

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:

ruby
module RoutingFilter
  class Awesomeness 
License: MIT

Issues· 23 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言