Sha256: 1284d6cd5baff095bb0b8adb429a32c4d3d0070e503def30e21535808c1f324b

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# 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

module RoutingFilter
  class Extension < Filter
    attr_reader :extension, :exclude

    def initialize(*args)
      super
      @exclude   = options[:exclude]
      @extension = options[:extension] || 'html'
    end

    def around_recognize(path, env, &block)
      extract_extension!(path) unless excluded?(path)
      yield
    end

    def around_generate(params, &block)
      yield.tap do |result|
        result.update append_extension!(result.url) if append_extension?(result.url)
      end
    end

    protected

      def extract_extension!(path)
        path.sub!(/\.#{extension}$/, '')
        $1
      end

      def append_extension?(url)
        !(blank?(url) || excluded?(url) || mime_extension?(url))
      end

      def append_extension!(url)
        url.replace url.sub(/(\?|$)/, ".#{extension}\\1")
      end

      def blank?(url)
        url.blank? || !!url.match(%r(^/(\?|$)))
      end

      def excluded?(url)
        case exclude
        when Regexp
          url =~ exclude
        when Proc
          exclude.call(url)
        end
      end

      def mime_extension?(url)
        url =~ /\.#{Mime::EXTENSION_LOOKUP.keys.join('|')}(\?|$)/
      end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
routing-filter-codeur-0.7.1.2 lib/routing_filter/filters/extension.rb
routing-filter-codeur-0.7.1.1 lib/routing_filter/filters/extension.rb
routing-filter-codeur-0.7.1 lib/routing_filter/filters/extension.rb
routing-filter-0.7.0 lib/routing_filter/filters/extension.rb