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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Generator.



656
657
658
659
660
661
662
663
664
665
666
# File 'lib/action_dispatch/routing/route_set.rb', line 656

def initialize(named_route, options, recall, set)
  @named_route = named_route
  @options     = options
  @recall      = recall
  @set         = set

  normalize_options!
  normalize_controller_action_id!
  use_relative_controller!
  normalize_controller!
end

Instance Attribute Details

#named_routeObject (readonly)

Returns the value of attribute named_route.



654
655
656
# File 'lib/action_dispatch/routing/route_set.rb', line 654

def named_route
  @named_route
end

#optionsObject (readonly)

Returns the value of attribute options.



654
655
656
# File 'lib/action_dispatch/routing/route_set.rb', line 654

def options
  @options
end

#recallObject (readonly)

Returns the value of attribute recall.



654
655
656
# File 'lib/action_dispatch/routing/route_set.rb', line 654

def recall
  @recall
end

#setObject (readonly)

Returns the value of attribute set.



654
655
656
# File 'lib/action_dispatch/routing/route_set.rb', line 654

def set
  @set
end

Instance Method Details

#controllerObject



668
669
670
# File 'lib/action_dispatch/routing/route_set.rb', line 668

def controller
  @options[:controller]
end

#current_controllerObject



672
673
674
# File 'lib/action_dispatch/routing/route_set.rb', line 672

def current_controller
  @recall[:controller]
end

#different_controller?Boolean

Returns:

  • (Boolean)


743
744
745
746
# File 'lib/action_dispatch/routing/route_set.rb', line 743

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

#generateObject

Generates a path from routes, returns a RouteWithParams or MissingRoute. MissingRoute will raise ActionController::UrlGenerationError.



739
740
741
# File 'lib/action_dispatch/routing/route_set.rb', line 739

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

#normalize_controller!Object

Remove leading slashes from controllers



727
728
729
730
731
732
733
734
735
# File 'lib/action_dispatch/routing/route_set.rb', line 727

def normalize_controller!
  if controller
    if controller.start_with?("/")
      @options[:controller] = controller[1..-1]
    else
      @options[:controller] = controller
    end
  end
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.



709
710
711
712
713
# File 'lib/action_dispatch/routing/route_set.rb', line 709

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

#normalize_options!Object



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/action_dispatch/routing/route_set.rb', line 684

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

#use_recall_for(key) ⇒ Object



676
677
678
679
680
681
682
# File 'lib/action_dispatch/routing/route_set.rb', line 676

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[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”



717
718
719
720
721
722
723
724
# File 'lib/action_dispatch/routing/route_set.rb', line 717

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