Class: ActionDispatch::Routing::RouteSet::Generator

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

Overview

:nodoc:

Constant Summary collapse

PARAMETERIZE =
lambda do |name, value|
  if name == :controller
    value
  elsif value.is_a?(Array)
    value.map { |v| v.to_param }.join('/')
  elsif param = value.to_param
    param
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, recall, set, extras = false) ⇒ Generator

Returns a new instance of Generator.



437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/action_dispatch/routing/route_set.rb', line 437

def initialize(options, recall, set, extras = false)
  @named_route = options.delete(:use_route)
  @options     = options.dup
  @recall      = recall.dup
  @set         = set
  @extras      = extras

  normalize_options!
  normalize_controller_action_id!
  use_relative_controller!
  normalize_controller!
  handle_nil_action!
end

Instance Attribute Details

#named_routeObject (readonly)

Returns the value of attribute named_route.



435
436
437
# File 'lib/action_dispatch/routing/route_set.rb', line 435

def named_route
  @named_route
end

#optionsObject (readonly)

Returns the value of attribute options.



435
436
437
# File 'lib/action_dispatch/routing/route_set.rb', line 435

def options
  @options
end

#recallObject (readonly)

Returns the value of attribute recall.



435
436
437
# File 'lib/action_dispatch/routing/route_set.rb', line 435

def recall
  @recall
end

#setObject (readonly)

Returns the value of attribute set.



435
436
437
# File 'lib/action_dispatch/routing/route_set.rb', line 435

def set
  @set
end

Instance Method Details

#controllerObject



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

def controller
  @options[:controller]
end

#current_controllerObject



455
456
457
# File 'lib/action_dispatch/routing/route_set.rb', line 455

def current_controller
  @recall[:controller]
end

#different_controller?Boolean

Returns:

  • (Boolean)


543
544
545
546
# File 'lib/action_dispatch/routing/route_set.rb', line 543

def different_controller?
  return false unless current_controller
  controller.to_param != current_controller.to_param
end

#generateObject



527
528
529
530
531
532
533
534
535
536
537
# File 'lib/action_dispatch/routing/route_set.rb', line 527

def generate
  path, params = @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)

  raise_routing_error unless path

  return [path, params.keys] if @extras

  [path, params]
rescue Journey::Router::RoutingError
  raise_routing_error
end

#handle_nil_action!Object

This handles the case of :action => nil being explicitly passed. It is identical to :action => “index”



520
521
522
523
524
525
# File 'lib/action_dispatch/routing/route_set.rb', line 520

def handle_nil_action!
  if options.has_key?(:action) && options[:action].nil?
    options[:action] = 'index'
  end
  recall[:action] = options.delete(:action) if options[:action] == 'index'
end

#normalize_controller!Object

Remove leading slashes from controllers



514
515
516
# File 'lib/action_dispatch/routing/route_set.rb', line 514

def normalize_controller!
  @options[:controller] = controller.sub(%r{^/}, '') if controller
end

#normalize_controller_action_id!Object

This pulls :controller, :action, and :id out of the recall. The recall key is only used if there is no key in the options or if the key in the options is identical. If any of :controller, :action or :id is not found, don’t pull any more keys from the recall.



494
495
496
497
498
499
500
# File 'lib/action_dispatch/routing/route_set.rb', line 494

def normalize_controller_action_id!
  @recall[:action] ||= 'index' if current_controller

  use_recall_for(:controller) or return
  use_recall_for(:action) or return
  use_recall_for(:id)
end

#normalize_options!Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/action_dispatch/routing/route_set.rb', line 469

def normalize_options!
  # If an explicit :controller was given, always make :action explicit
  # too, so that action expiry works as expected for things like
  #
  #   generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
  #
  # (the above is from the unit tests). In the above case, because the
  # controller was explicitly given, but no action, the action is implied to
  # be "index", not the recalled action of "show".

  if options[:controller]
    options[:action]     ||= 'index'
    options[:controller]   = options[:controller].to_s
  end

  if options[:action]
    options[:action] = options[:action].to_s
  end
end

#raise_routing_errorObject



539
540
541
# File 'lib/action_dispatch/routing/route_set.rb', line 539

def raise_routing_error
  raise ActionController::RoutingError, "No route matches #{options.inspect}"
end

#use_recall_for(key) ⇒ Object



459
460
461
462
463
464
465
466
467
# File 'lib/action_dispatch/routing/route_set.rb', line 459

def use_recall_for(key)
  if @recall[key] && (!@options.key?(key) || @options[key] == @recall[key])
    if named_route_exists?
      @options[key] = @recall.delete(key) if segment_keys.include?(key)
    else
      @options[key] = @recall.delete(key)
    end
  end
end

#use_relative_controller!Object

if the current controller is “foo/bar/baz” and :controller => “baz/bat” is specified, the controller becomes “foo/baz/bat”



504
505
506
507
508
509
510
511
# File 'lib/action_dispatch/routing/route_set.rb', line 504

def use_relative_controller!
  if !named_route && different_controller? && !controller.start_with?("/")
    old_parts = current_controller.split('/')
    size = controller.count("/") + 1
    parts = old_parts[0...-size] << controller
    @options[:controller] = parts.join("/")
  end
end