Class: ActionDispatch::Routing::RouteSet

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

Overview

:nodoc:

Defined Under Namespace

Classes: Dispatcher, Generator, NamedRouteCollection

Constant Summary

PARAMETERS_KEY =
'action_dispatch.request.path_parameters'
RESERVED_OPTIONS =
[:anchor, :params, :only_path, :host, :protocol, :port, :trailing_slash]
CONTROLLER_REGEXP =
/[_a-zA-Z0-9]+/

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RouteSet) initialize(request_class = ActionDispatch::Request)

A new instance of RouteSet



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 210

def initialize(request_class = ActionDispatch::Request)
  self.routes = []
  self.named_routes = NamedRouteCollection.new
  self.resources_path_names = self.class.default_resources_path_names.dup
  self.controller_namespaces = Set.new
  self.default_url_options = {}

  self.request_class = request_class
  self.valid_conditions = request_class.public_instance_methods.map { |m| m.to_sym }
  self.valid_conditions.delete(:id)
  self.valid_conditions.push(:controller, :action)

  @disable_clear_and_finalize = false
  clear!
end

Instance Attribute Details

- (Object) controller_namespaces

Returns the value of attribute controller_namespaces



8
9
10
# File 'actionpack/lib/action_dispatch/routing/deprecated_mapper.rb', line 8

def controller_namespaces
  @controller_namespaces
end

- (Object) default_url_options

Returns the value of attribute default_url_options



204
205
206
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 204

def default_url_options
  @default_url_options
end

- (Object) disable_clear_and_finalize

Returns the value of attribute disable_clear_and_finalize



203
204
205
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 203

def disable_clear_and_finalize
  @disable_clear_and_finalize
end

- (Object) named_routes

Returns the value of attribute named_routes



202
203
204
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 202

def named_routes
  @named_routes
end

- (Object) request_class

Returns the value of attribute request_class



204
205
206
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 204

def request_class
  @request_class
end

- (Object) resources_path_names

Returns the value of attribute resources_path_names



203
204
205
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 203

def resources_path_names
  @resources_path_names
end

- (Object) routes

Returns the value of attribute routes



202
203
204
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 202

def routes
  @routes
end

- (Object) set

Returns the value of attribute set



202
203
204
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 202

def set
  @set
end

- (Object) valid_conditions

Returns the value of attribute valid_conditions



204
205
206
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 204

def valid_conditions
  @valid_conditions
end

Class Method Details

+ (Object) default_resources_path_names



206
207
208
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 206

def self.default_resources_path_names
  { :new => 'new', :edit => 'edit' }
end

Instance Method Details

- (Object) add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)



297
298
299
300
301
302
303
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 297

def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
  route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
  @set.add_route(*route)
  named_routes[name] = route if name
  routes << route
  route
end

- (Object) call(env)



490
491
492
493
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 490

def call(env)
  finalize!
  @set.call(env)
end

- (Object) clear!



247
248
249
250
251
252
253
254
255
256
257
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 247

def clear!
  # Clear the controller cache so we may discover new ones
  @controller_constraints = nil
  @finalized = false
  routes.clear
  named_routes.clear
  @set = ::Rack::Mount::RouteSet.new(
    :parameters_key => PARAMETERS_KEY,
    :request_class  => request_class
  )
end

- (Object) controller_constraints



12
13
14
15
16
17
18
19
# File 'actionpack/lib/action_dispatch/routing/deprecated_mapper.rb', line 12

def controller_constraints
  @controller_constraints ||= begin
    namespaces = controller_namespaces + in_memory_controller_namespaces
    source = namespaces.map { |ns| "#{Regexp.escape(ns)}/#{CONTROLLER_REGEXP.source}" }
    source << CONTROLLER_REGEXP.source
    Regexp.compile(source.sort.reverse.join('|'))
  end
end

- (Object) draw(&block)



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 226

def draw(&block)
  clear! unless @disable_clear_and_finalize

  mapper = Mapper.new(self)
  if block.arity == 1
    mapper.instance_exec(DeprecatedMapper.new(self), &block)
  else
    mapper.instance_exec(&block)
  end

  finalize! unless @disable_clear_and_finalize

  nil
end

- (Boolean) empty?

Returns:

  • (Boolean)


293
294
295
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 293

def empty?
  routes.empty?
end

- (Object) extra_keys(options, recall = {})

Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.



444
445
446
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 444

def extra_keys(options, recall={})
  generate_extras(options, recall).last
end

- (Object) finalize!



241
242
243
244
245
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 241

def finalize!
  return if @finalized
  @finalized = true
  @set.freeze
end

- (Object) generate(options, recall = {}, extras = false)



452
453
454
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 452

def generate(options, recall = {}, extras = false)
  Generator.new(options, recall, self, extras).generate
end

- (Object) generate_extras(options, recall = {})



448
449
450
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 448

def generate_extras(options, recall={})
  generate(options, recall, true)
end

- (Object) in_memory_controller_namespaces



21
22
23
24
25
26
27
28
29
# File 'actionpack/lib/action_dispatch/routing/deprecated_mapper.rb', line 21

def in_memory_controller_namespaces
  namespaces = Set.new
  ActionController::Base.descendants.each do |klass|
    next if klass.anonymous?
    namespaces << klass.name.underscore.split('/')[0...-1].join('/')
  end
  namespaces.delete('')
  namespaces
end

- (Object) install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)



259
260
261
262
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 259

def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
  Array(destinations).each { |d| d.module_eval { include Helpers } }
  named_routes.install(destinations, regenerate_code)
end

- (Object) recognize_path(path, environment = {})



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 495

def recognize_path(path, environment = {})
  method = (environment[:method] || "GET").to_s.upcase
  path = Rack::Mount::Utils.normalize_path(path) unless path =~ %r{://}

  begin
    env = Rack::MockRequest.env_for(path, {:method => method})
  rescue URI::InvalidURIError => e
    raise ActionController::RoutingError, e.message
  end

  req = @request_class.new(env)
  @set.recognize(req) do |route, matches, params|
    params.each do |key, value|
      if value.is_a?(String)
        value = value.dup.force_encoding(Encoding::BINARY) if value.encoding_aware?
        params[key] = URI.unescape(value)
      end
    end

    dispatcher = route.app
    dispatcher = dispatcher.app while dispatcher.is_a?(Mapper::Constraints)

    if dispatcher.is_a?(Dispatcher) && dispatcher.controller(params, false)
      dispatcher.prepare_params!(params)
      return params
    end
  end

  raise ActionController::RoutingError, "No route matches #{path.inspect}"
end

- (Object) url_for(options)



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 458

def url_for(options)
  finalize!
  options = (options || {}).reverse_merge!(default_url_options)

  handle_positional_args(options)

  rewritten_url = ""

  path_segments = options.delete(:_path_segments)

  unless options[:only_path]
    rewritten_url << (options[:protocol] || "http")
    rewritten_url << "://" unless rewritten_url.match("://")
    rewritten_url << rewrite_authentication(options)

    raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host]

    rewritten_url << options[:host]
    rewritten_url << ":#{options.delete(:port)}" if options.key?(:port)
  end

  path_options = options.except(*RESERVED_OPTIONS)
  path_options = yield(path_options) if block_given?
  path = generate(path_options, path_segments || {})

  # ROUTES TODO: This can be called directly, so script_name should probably be set in the routes
  rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
  rewritten_url << "##{Rack::Mount::Utils.escape_uri(options[:anchor].to_param.to_s)}" if options[:anchor]

  rewritten_url
end

- (Object) url_helpers



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 264

def url_helpers
  @url_helpers ||= begin
    routes = self

    helpers = Module.new do
      extend ActiveSupport::Concern
      include UrlFor

      @routes = routes
      class << self
        delegate :url_for, :to => '@routes'
      end
      extend routes.named_routes.module

      # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that
      # we can include?
      # Yes plz - JP
      included do
        routes.install_helpers(self)
        singleton_class.send(:define_method, :_routes) { routes }
      end

      define_method(:_routes) { routes }
    end

    helpers
  end
end