module RoutingFilter
The Extension
filter chops a file extension off from the end of the recognized path. When a path is generated the filter re-adds the extension to the path accordingly.
incoming url: /products.xml filtered url: /products generated url: /products.xml
You can install the filter like this:
# in config/routes.rb Rails.application.routes.draw do filter :extension end
The Locale
filter extracts segments matching /:locale from the beginning of the recognized path and exposes the page parameter as params. When a path is generated the filter adds the segments to the path accordingly if the page parameter is passed to the url helper.
incoming url: /de/products filtered url: /products params: params[:locale] = 'de'
You can install the filter like this:
# in config/routes.rb Rails.application.routes.draw do filter :locale end
To make your named_route helpers or url_for add the locale segments you can use:
products_path(:locale => 'de') url_for(:products, :locale => 'de'))
The Pagination
filter extracts segments matching /page/:page from the end of the recognized url and exposes the page parameter as params. When a url is generated the filter adds the segments to the url accordingly if the page parameter is passed to the url helper.
incoming url: /products/page/1 filtered url: /products params: params[:page] = 1
You can install the filter like this:
# in config/routes.rb Rails.application.routes.draw do filter :pagination end
To make your named_route helpers or url_for add the pagination segments you can use:
products_path(:page => 1) url_for(:products, :page => 1)
The Uuid
filter extracts an UUID segment from the beginning of the recognized path and exposes the page parameter as params. When a path is generated the filter adds the segments to the path accordingly if the page parameter is passed to the url helper.
incoming url: /d00fbbd1-82b6-4c1a-a57d-098d529d6854/products filtered url: /products params: params[:uuid] = 'd00fbbd1-82b6-4c1a-a57d-098d529d6854'
You can install the filter like this:
# in config/routes.rb Rails.application.routes.draw do filter :uuid end
To make your named_route helpers or url_for add the uuid segment you can use:
products_path(:uuid => uuid) url_for(:products, :uuid => uuid)
Constants
- VERSION
Public Class Methods
# File lib/routing_filter.rb, line 17 def active=(active) @@active = active end
# File lib/routing_filter.rb, line 21 def active? defined?(@@active) ? @@active : @@active = true end
# File lib/routing_filter.rb, line 13 def build(name, options) const_get(name.to_s.camelize).new(options) end
Public Instance Methods
# File lib/routing_filter/filters/locale.rb, line 61 def around_generate(*args, &block) params = args.extract_options! # this is because we might get a call like forum_topics_path(forum, topic, :locale => :en) locale = params.delete(:locale) # extract the passed :locale option locale = I18n.locale if locale.nil? # default to I18n.locale when locale is nil (could also be false) locale = nil unless valid_locale?(locale) # reset to no locale when locale is not valid args << params yield.tap do |result| result.update prepend_segment(result.url, locale) if prepend_locale?(locale) && !excluded?(result.url) end
Protected Instance Methods
# File lib/routing_filter/filters/extension.rb, line 48 def append_extension!(url) url.replace url.sub(/(\?|$)/, ".#{extension}\\1") end
# File lib/routing_filter/filters/extension.rb, line 44 def append_extension?(url) !(blank?(url) || excluded?(url) || mime_extension?(url)) end
# File lib/routing_filter/filters/extension.rb, line 52 def blank?(url) url.blank? || !!url.match(%r(^/(\?|$))) end
# File lib/routing_filter/filters/extension.rb, line 56 def excluded?(url) case exclude when Regexp url =~ exclude when Proc exclude.call(url) end end
# File lib/routing_filter/filters/extension.rb, line 39 def extract_extension!(path) path.sub!(/\.#{extension}$/, '') $1 end
# File lib/routing_filter/filters/extension.rb, line 65 def mime_extension?(url) url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/ end