Sha256: 31f75275d0ba94ac344ddb96b9d36bcadcd8ce1259adbfc167456706016d1c34

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

# This middleware is necessary so that the engine can abstain from processing
# requests where no such slug exists. If this behavior were done in a
# controller, Rails would heavily mutate the request object and no additional
# controllers would have much luck processing the request. Specifically, the
# named parameters for the route are cached on the request, and downstream
# controllers would get the wrong params hash.

module Slug
  class Filter
    
    def initialize(app)
      @app = app
    end
  
    def call(env)
      # if a matching slug exists in the database, allow engine to process it
      if Permalink.where('slug = ?', slug(env)).exists?
        @app.call(env)
      else # force engine to abstain from processing this request
        [404, {"X-Cascade" => "pass"}, ["no such slug exists"]]
      end
    end

    # Extract the :slug value from the raw request
    def slug(env)
      slug = env['PATH_INFO']
      slug = slug.slice(1..-1) if slug[0] == 47 # has a leading '/'
      slug
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slug-engine-0.0.2 lib/slug/filter.rb
slug-engine-0.0.1 lib/slug/filter.rb