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

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

Constant Summary

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(named_route, options, recall, set) ⇒ Generator



566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 566

def initialize(named_route, options, recall, set)
  @named_route = named_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

#named_routeObject (readonly)

Returns the value of attribute named_route



564
565
566
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 564

def named_route
  @named_route
end

#optionsObject (readonly)

Returns the value of attribute options



564
565
566
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 564

def options
  @options
end

#recallObject (readonly)

Returns the value of attribute recall



564
565
566
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 564

def recall
  @recall
end

#setObject (readonly)

Returns the value of attribute set



564
565
566
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 564

def set
  @set
end

Instance Method Details

#controllerObject



580
581
582
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 580

def controller
  @options[:controller]
end

#current_controllerObject



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

def current_controller
  @recall[:controller]
end

#different_controller?Boolean



661
662
663
664
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 661

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

#generateObject

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



657
658
659
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 657

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

#normalize_action!Object

Move 'index' action from options to recall



649
650
651
652
653
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 649

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

#normalize_controller!Object

Remove leading slashes from controllers



644
645
646
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 644

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.



626
627
628
629
630
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 626

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

#normalize_options!Object



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 601

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

#normalize_recall!Object

Set 'index' as default action for recall



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

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

#use_recall_for(key) ⇒ Object



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

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

#use_relative_controller!Object

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



634
635
636
637
638
639
640
641
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 634

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