Module: ActionDispatchJourneyRouterWithFiltering

Defined in:
lib/routing_filter/adapters/routers/journey.rb

Overview

Prepend the method lookup to intercept find_routes in rails.

This enables us to intercept the incoming route paths before they are recognized by the rails router and transformed to a route set and dispatched to a controller.

Instance Method Summary collapse

Instance Method Details

#find_routes(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/routing_filter/adapters/routers/journey.rb', line 9

def find_routes(env)
  path = if env.is_a?(Hash)
           env['PATH_INFO']
         else
           env.path_info
         end

  filter_parameters = {}
  original_path = path.dup

  # Apply the custom user around_recognize filter callbacks
  @routes.filters.run(:around_recognize, path, env) do
    # Yield the filter paramters for adjustment by the user
    filter_parameters
  end

  # Recognize the routes
  super(env) do |match, parameters, route|
    # Merge in custom parameters that will be visible to the controller
    params = (parameters || {}).merge(filter_parameters)

    # Reset the path before yielding to the controller (prevents breakages in CSRF validation)
    if env.is_a?(Hash)
      env['PATH_INFO'] = original_path
    else
      env.path_info = original_path
    end

    # Yield results are dispatched to the controller
    yield [match, params, route]
  end
end