Class: ActionDispatch::Routing::RouteSet

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

Overview

:nodoc:

Defined Under Namespace

Modules: MountedHelpers Classes: Dispatcher, Generator, NamedRouteCollection

Constant Summary collapse

PARAMETERS_KEY =
'action_dispatch.request.path_parameters'
RESERVED_OPTIONS =
[:host, :protocol, :port, :subdomain, :domain, :tld_length,
:trailing_slash, :anchor, :params, :only_path, :script_name]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_class = ActionDispatch::Request) ⇒ RouteSet

Returns a new instance of RouteSet.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 216

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.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)

  @append = []
  @prepend = []
  @disable_clear_and_finalize = false
  clear!
end

Instance Attribute Details

#default_scopeObject

Returns the value of attribute default_scope



208
209
210
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 208

def default_scope
  @default_scope
end

#default_url_optionsObject

Returns the value of attribute default_url_options



210
211
212
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 210

def default_url_options
  @default_url_options
end

#disable_clear_and_finalizeObject

Returns the value of attribute disable_clear_and_finalize



209
210
211
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 209

def disable_clear_and_finalize
  @disable_clear_and_finalize
end

#named_routesObject

Returns the value of attribute named_routes



208
209
210
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 208

def named_routes
  @named_routes
end

#request_classObject

Returns the value of attribute request_class



210
211
212
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 210

def request_class
  @request_class
end

#resources_path_namesObject

Returns the value of attribute resources_path_names



209
210
211
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 209

def resources_path_names
  @resources_path_names
end

#routesObject

Returns the value of attribute routes



208
209
210
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 208

def routes
  @routes
end

#setObject

Returns the value of attribute set



208
209
210
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 208

def set
  @set
end

#valid_conditionsObject

Returns the value of attribute valid_conditions



210
211
212
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 210

def valid_conditions
  @valid_conditions
end

Class Method Details

.default_resources_path_namesObject



212
213
214
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 212

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

Instance Method Details

#_generate_prefix(options = {}) ⇒ Object



500
501
502
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 500

def _generate_prefix(options = {})
  nil
end

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

Raises:

  • (ArgumentError)


342
343
344
345
346
347
348
349
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 342

def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
  raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i)
  route = Route.new(self, app, conditions, requirements, defaults, name, anchor)
  @set.add_route(route.app, route.conditions, route.defaults, route.name)
  named_routes[name] = route if name
  routes << route
  route
end

#append(&block) ⇒ Object



240
241
242
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 240

def append(&block)
  @append << block
end

#call(env) ⇒ Object



530
531
532
533
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 530

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

#clear!Object



269
270
271
272
273
274
275
276
277
278
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 269

def clear!
  @finalized = false
  routes.clear
  named_routes.clear
  @set = ::Rack::Mount::RouteSet.new(
    :parameters_key => PARAMETERS_KEY,
    :request_class  => request_class
  )
  @prepend.each { |blk| eval_block(blk) }
end

#define_mounted_helper(name) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 292

def define_mounted_helper(name)
  return if MountedHelpers.method_defined?(name)

  routes = self
  MountedHelpers.class_eval do
    define_method "_#{name}" do
      RoutesProxy.new(routes, self._routes_context)
    end
  end

  MountedHelpers.class_eval <<-RUBY
    def #{name}
      @#{name} ||= _#{name}
    end
  RUBY
end

#draw(&block) ⇒ Object



233
234
235
236
237
238
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 233

def draw(&block)
  clear! unless @disable_clear_and_finalize
  eval_block(block)
  finalize! unless @disable_clear_and_finalize
  nil
end

#empty?Boolean

Returns:

  • (Boolean)


338
339
340
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 338

def empty?
  routes.empty?
end

#eval_block(block) ⇒ Object



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

def eval_block(block)
  if block.arity == 1
    raise "You are using the old router DSL which has been removed in Rails 3.1. " <<
      "Please check how to update your routes file at: http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ " <<
      "or add the rails_legacy_mapper gem to your Gemfile"
  end
  mapper = Mapper.new(self)
  if default_scope
    mapper.with_default_scope(default_scope, &block)
  else
    mapper.instance_exec(&block)
  end
end

#extra_keys(options, recall = {}) ⇒ Object

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



485
486
487
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 485

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

#finalize!Object



262
263
264
265
266
267
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 262

def finalize!
  return if @finalized
  @append.each { |blk| eval_block(blk) }
  @finalized = true
  @set.freeze
end

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



493
494
495
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 493

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

#generate_extras(options, recall = {}) ⇒ Object



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

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

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



280
281
282
283
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 280

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

#mounted_helpersObject



288
289
290
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 288

def mounted_helpers
  MountedHelpers
end

#prepend(&block) ⇒ Object



244
245
246
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 244

def prepend(&block)
  @prepend << block
end

#recognize_path(path, environment = {}) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 535

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.parser.unescape(value)
      end
    end

    dispatcher = route.app
    while dispatcher.is_a?(Mapper::Constraints) && dispatcher.matches?(env) do
      dispatcher = dispatcher.app
    end

    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

#url_for(options) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 504

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

  handle_positional_args(options)

  user, password = extract_authentication(options)
  path_segments  = options.delete(:_path_segments)
  script_name    = options.delete(:script_name)

  path = (script_name.blank? ? _generate_prefix(options) : script_name.chomp('/')).to_s

  path_options = options.except(*RESERVED_OPTIONS)
  path_options = yield(path_options) if block_given?

  path_addition, params = generate(path_options, path_segments || {})
  path << path_addition

  ActionDispatch::Http::URL.url_for(options.merge({
    :path => path,
    :params => params,
    :user => user,
    :password => password
  }))
end

#url_helpersObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 309

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(:redefine_method, :_routes) { routes }
      end

      define_method(:_routes) { @_routes || routes }
    end

    helpers
  end
end