Class: Integral::Middleware::AliasRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/integral/middleware/alias_router.rb

Overview

Handles dynamic page paths and category slug routing.

Checks all GET requests to see if the PATH matches an Integral::Page path or category slug If a match is found the PATH is rewritten from human readable into a Rails route i.e. ‘/company/who-we-are’ -> /pages/12

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ AliasRouter

Returns a new instance of AliasRouter.



10
11
12
# File 'lib/integral/middleware/alias_router.rb', line 10

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Handles rewriting the path to something the Rails router will understand



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/integral/middleware/alias_router.rb', line 15

def call(env)
  request = Rack::Request.new(env)

  # Return early if request is not a GET
  return @app.call(env) unless request.get?

  # Return early if request is within backend or Active Storage path directory
  backend_path = "/#{Integral.backend_namespace}/"
  return @app.call(env) if request.path_info.starts_with?(backend_path) || request.path_info.starts_with?(ActiveStorage.routes_prefix)

  # Rewrites path if the request linked to an Integral::Page or Integral::Category
  process_path(env, request)

  @app.call(env)
end