Sha256: 6296109df8e11714aff4264745b1f82e601ce6d3901c8f05b1cd3f808f71ffdd

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 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/product/1
#   filtered url: /product/1
#   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:
#
#   product_path(:uuid => uuid)
#   url_for(product, :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_uuid!(path)
      yield.tap do |params|
        params[:uuid] = uuid if uuid
      end
    end

    def around_generate(*args, &block)
      uuid = args.extract_options!.delete(:uuid)
      yield.tap do |result|
        prepend_uuid!(result, uuid) if uuid
      end
    end

    protected

      def extract_uuid!(path)
        path.sub!(UUID_SEGMENT, '/')
        $1
      end

      def prepend_uuid!(result, uuid)
        url = result.is_a?(Array) ? result.first : result
        url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{uuid}#{$2}" }
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
routing-filter-0.1.3 lib/routing_filter/filters/uuid.rb
routing-filter-0.1.2 lib/routing_filter/filters/uuid.rb
routing-filter-0.1.1 lib/routing_filter/filters/uuid.rb
routing-filter-0.1.0 lib/routing_filter/filters/uuid.rb