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

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

Overview

:nodoc:

Constant Summary

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

- (Generator) initialize(options, recall, set)



505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 505

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

  normalize_recall!
  normalize_options!
  normalize_controller_action_id!
  use_relative_controller!
  normalize_controller!
  normalize_action!
end

Instance Attribute Details

- (Object) named_route (readonly)

Returns the value of attribute named_route



503
504
505
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 503

def named_route
  @named_route
end

- (Object) options (readonly)

Returns the value of attribute options



503
504
505
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 503

def options
  @options
end

- (Object) recall (readonly)

Returns the value of attribute recall



503
504
505
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 503

def recall
  @recall
end

- (Object) set (readonly)

Returns the value of attribute set



503
504
505
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 503

def set
  @set
end

Instance Method Details

- (Object) controller



519
520
521
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 519

def controller
  @options[:controller]
end

- (Object) current_controller



523
524
525
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 523

def current_controller
  @recall[:controller]
end

- (Boolean) different_controller?



600
601
602
603
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 600

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

- (Object) generate

Generates a path from routes, returns [path, params]. If no route is generated the formatter will raise ActionController::UrlGenerationError



596
597
598
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 596

def generate
  @set.formatter.generate(:path_info, named_route, options, recall, PARAMETERIZE)
end

- (Object) normalize_action!

Move 'index' action from options to recall



588
589
590
591
592
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 588

def normalize_action!
  if @options[:action] == 'index'
    @recall[:action] = @options.delete(:action)
  end
end

- (Object) normalize_controller!

Remove leading slashes from controllers



583
584
585
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 583

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

- (Object) normalize_controller_action_id!

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.



565
566
567
568
569
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 565

def normalize_controller_action_id!
  use_recall_for(:controller) or return
  use_recall_for(:action) or return
  use_recall_for(:id)
end

- (Object) normalize_options!



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 540

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.key?(:action)
    options[:action] = (options[:action] || 'index').to_s
  end
end

- (Object) normalize_recall!

Set 'index' as default action for recall



536
537
538
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 536

def normalize_recall!
  @recall[:action] ||= 'index'
end

- (Object) use_recall_for(key)



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

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

- (Object) use_relative_controller!

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



573
574
575
576
577
578
579
580
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 573

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