Sha256: d2c82cb8e2e7ac120dc217eb6efd4c9f1d1186a947740265cb7ea4aaedb089a4

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

# The Uuid filter extracts an UUID segment from the beginning of the recognized
# path and exposes the page parameter as params[:page]. 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)

module RoutingFilter
  class Uuid < Filter
    UUID_SEGMENT = %r(^/?([a-z\d]{8}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{4}\-[a-z\d]{12})(/)?)
    
    def around_recognize(path, env, &block)
      uuid = extract_segment!(UUID_SEGMENT, path)
      yield.tap do |params|
        params[:uuid] = uuid if uuid
      end
    end

    def around_generate(params, &block)
      uuid = params.delete(:uuid)
      yield.tap do |result|
        prepend_segment!(result, uuid) if uuid
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
routing-filter-0.6.3 lib/routing_filter/filters/uuid.rb
routing-filter-0.6.2 lib/routing_filter/filters/uuid.rb
routing-filter-0.6.1 lib/routing_filter/filters/uuid.rb
routing-filter-0.6.0 lib/routing_filter/filters/uuid.rb
routing-filter-0.5.1 lib/routing_filter/filters/uuid.rb
routing-filter-0.4.0.1 lib/routing_filter/filters/uuid.rb
routing-filter-0.4.0 lib/routing_filter/filters/uuid.rb
routing-filter-0.5.0 lib/routing_filter/filters/uuid.rb
routing-filter-0.4.0.pre lib/routing_filter/filters/uuid.rb
routing-filter-0.3.1 lib/routing_filter/filters/uuid.rb
routing-filter-0.3.0 lib/routing_filter/filters/uuid.rb
routing-filter-0.3.0.beta lib/routing_filter/filters/uuid.rb