
Security News
CISA’s 2025 SBOM Guidance Adds Hashes, Licenses, Tool Metadata, and Context
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
strikeroff-routing-filter
Advanced tools
This plugin is a wild hack that wraps 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 plugin comes with a locale routing filter that demonstrates the implementation of a custom filter.
The following would be a sceleton of an empty filter:
module RoutingFilter
class Awesomeness < Base
def around_recognize(route, path, env)
# Alter the path here before it gets recognized.
# Make sure to yield (calls the next around filter if present and
# eventually `recognize_path` on the routeset):
returning yield do |params|
# You can additionally modify the params here before they get passed
# to the controller.
end
end
def around_generate(controller, *args, &block)
# Alter arguments here before they are passed to `url_for`.
# Make sure to yield (calls the next around filter if present and
# eventually `url_for` on the controller):
returning yield do |result|
# You can change the generated url_or_path here. Make sure to use
# one of the "in-place" modifying String methods though (like sub!
# and friends).
end
end
end
end
You then have to specify the filter explicitely in your routes.rb:
ActionController::Routing::Routes.draw do |map|
map.filter 'awesomeness'
end
(I am not sure if it makes sense to provide more technical information than this because the usage of this plugin definitely requires some advanced knowledge about Rails internals and especially its routing system. So, I figure, anyone who could use this should also be able to read the code and figure out what it's doing much better then from any lengthy documentation.
If I'm mistaken on this please drop me an email with your suggestions.)
An early usecase from which this originated was the need to define a locale at the beginning of an URL in a way so that
You can read about this struggle and two possible, yet unsatisfying solutions here. The conclusion so far is that Rails itself does not provide the tools to solve this problem in a clean and dry way.
Another usecase that eventually spawned the manifestation of this plugin was the need to map an arbitrary count of path segments to a certain model instance. In an application that I've been working on recently I needed to map URL paths to a nested tree of models like so:
root
+ docs
+ api
+ wiki
E.g. the docs section should map to the path /docs
, the api section to
the path /docs/api
and so on. Furthermore, after these paths there need to be
more things to be specified. E.g. the wiki needs to define a whole Rails
resource with URLs like /docs/wiki/pages/1/edit
.
The only way to solve this problem with Rails' routing toolkit is to map
a big, bold /*everything
catch-all ("globbing") route and process the whole
path in a custom dispatcher.
This, of course, is a really unsatisfying solution because one has to reimplement everything that Rails routes are here to help with: regarding both URL recognition (like parameter mappings, resources, ...) and generation (url_helpers).
This plugin offers a solution that takes exactly the opposite route.
Instead of trying to change things between the URL recognition and generation stages to achieve the desired result it wraps around the whole routing system and allows to pre- and post-filter both what goes into it (URL recognition) and what comes out of it (URL generation).
This way we can leave everything else completely untouched.
So, even though the plugin itself is a blatant monkey-patch to one of the most complex area of Rails internals, this solution seems to be effectively less intrusive and pricey than others are.
Locale filter now has option :scip_locale_filter. In you routes.rb you can write this
map.change_locale "main/change_locale/:new_locale", :controller=>"main", :action=>"change_locale", :scip_locale_filter=>true
And locale filter wouldn't add locale to url while generate.
extract_locale! and prepend_locale! methods become class methods aka static. So you can use them outside of gem.
Usecase:
For example, to create on page language changer without requests to server. You need only change locale in url.
Example code: Current locale - :ru,but you need to have a link with locale = :en.
link_to("EN", url_with_locale(request.request_uri.dup, :en))
def url_with_locale(url,locale)
RoutingFilter::Locale.extract_locale! url
RoutingFilter::Locale.prepend_locale! url, locale
end
Authors: Sven Fuchs
License: MIT
FAQs
Unknown package
We found that strikeroff-routing-filter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.