Class: ActionDispatch::Routing::RouteSet::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
actionpack/lib/action_dispatch/routing/route_set.rb

Overview

:nodoc:

Instance Method Summary (collapse)

Constructor Details

- (Dispatcher) initialize(options = {})



22
23
24
25
26
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 22

def initialize(options={})
  @defaults = options[:defaults]
  @glob_param = options.delete(:glob)
  @controller_class_names = ThreadSafe::Cache.new
end

Instance Method Details

- (Object) call(env)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 28

def call(env)
  params = env[PARAMETERS_KEY]

  # If any of the path parameters has an invalid encoding then
  # raise since it's likely to trigger errors further on.
  params.each do |key, value|
    next unless value.respond_to?(:valid_encoding?)

    unless value.valid_encoding?
      raise ActionController::BadRequest, "Invalid parameter: #{key} => #{value}"
    end
  end

  prepare_params!(params)

  # Just raise undefined constant errors if a controller was specified as default.
  unless controller = controller(params, @defaults.key?(:controller))
    return [404, {'X-Cascade' => 'pass'}, []]
  end

  dispatch(controller, params[:action], env)
end

- (Object) controller(params, default_controller = true)

If this is a default_controller (i.e. a controller specified by the user) we should raise an error in case it's not found, because it usually means a user error. However, if the controller was retrieved through a dynamic segment, as in :controller(/:action), we should simply return nil and delegate the control back to Rack cascade. Besides, if this is not a default controller, it means we should respect the @scope parameter.



63
64
65
66
67
68
69
70
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 63

def controller(params, default_controller=true)
  if params && params.key?(:controller)
    controller_param = params[:controller]
    controller_reference(controller_param)
  end
rescue NameError => e
  raise ActionController::RoutingError, e.message, e.backtrace if default_controller
end

- (Object) prepare_params!(params)



51
52
53
54
55
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 51

def prepare_params!(params)
  normalize_controller!(params)
  merge_default_action!(params)
  split_glob_param!(params) if @glob_param
end