Class: Gluttonberg::Middleware::Rewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/middleware/rewriter.rb

Overview

This middleware is used to extract page info from current path. This helps gluttonberg pages controller/helpers to access page information easily. It also rewrites to public pages if valid page path found This middleware assumed that Locale Middleware has already performed its ‘call’ action

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rewriter

Returns a new instance of Rewriter.



8
9
10
# File 'lib/gluttonberg/middleware/rewriter.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Reads current path and if its not needs to be bypassed or rails route then tries to find a Gluttonberg CMS Page using this path, if it finds a page then redirect to Public::Page show action. It assigns page object env , env as as page path. It helps in debugging. it also tries to find it by previous_path, if it finds a page then it redirects permanently to new path of this page. Othwerise it simply returns back from this middleware

Parameters:

  • env (Hash)

    looking for PATH_INFO



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gluttonberg/middleware/rewriter.rb', line 21

def call(env)
  path = env['PATH_INFO']
  unless Gluttonberg::Middleware::Locales.bypass_path?(path, env)
    unless rails_route?(path)
      page = Gluttonberg::Page.find_by_path(path, env['GLUTTONBERG.LOCALE'] , env['HTTP_HOST'])
      unless page.blank?
        env['GLUTTONBERG.PAGE'] = page
        env['GLUTTONBERG.PATH_INFO'] = path # for debugging purpose
        if page.redirect_required?
          return redirect_param_array(page.redirect_url)
        elsif page.rewrite_required?
          env['PATH_INFO'] = page.generate_rewrite_path(path)
        else
          env['PATH_INFO'] = "/_public/page"
        end
      else
        page = Gluttonberg::Page.find_by_previous_path(path, env['GLUTTONBERG.LOCALE'] , env['HTTP_HOST'])
        unless page.blank?
          return redirect_param_array(page.public_path)
        end
      end
    end # rails route
  end

  @app.call(env)
end