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

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

Constant Summary collapse

PARAMETERIZE =
lambda do |name, value|
  if name == :controller
    value
  else
    value.to_param
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Generator.



547
548
549
550
551
552
553
554
555
556
557
# File 'lib/action_dispatch/routing/route_set.rb', line 547

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.



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

def named_route
  @named_route
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#recallObject (readonly)

Returns the value of attribute recall.



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

def recall
  @recall
end

#setObject (readonly)

Returns the value of attribute set.



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

def set
  @set
end

Instance Method Details

#controllerObject



559
560
561
# File 'lib/action_dispatch/routing/route_set.rb', line 559

def controller
  @options[:controller]
end

#current_controllerObject



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

def current_controller
  @recall[:controller]
end

#different_controller?Boolean

Returns:

  • (Boolean)


634
635
636
637
# File 'lib/action_dispatch/routing/route_set.rb', line 634

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



630
631
632
# File 'lib/action_dispatch/routing/route_set.rb', line 630

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

#normalize_controller!Object

Remove leading slashes from controllers



618
619
620
621
622
623
624
625
626
# File 'lib/action_dispatch/routing/route_set.rb', line 618

def normalize_controller!
  if controller
    if controller.start_with?("/".freeze)
      @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.



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

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



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/action_dispatch/routing/route_set.rb', line 575

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



567
568
569
570
571
572
573
# File 'lib/action_dispatch/routing/route_set.rb', line 567

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”



608
609
610
611
612
613
614
615
# File 'lib/action_dispatch/routing/route_set.rb', line 608

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