Class: ActionDispatch::Routing::RouteSet::Generator
- Defined in:
- actionpack/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
-
#named_route ⇒ Object
readonly
Returns the value of attribute named_route.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#recall ⇒ Object
readonly
Returns the value of attribute recall.
-
#set ⇒ Object
readonly
Returns the value of attribute set.
Instance Method Summary collapse
- #controller ⇒ Object
- #current_controller ⇒ Object
- #different_controller? ⇒ Boolean
-
#generate ⇒ Object
Generates a path from routes, returns [path, params].
-
#initialize(options, recall, set) ⇒ Generator
constructor
A new instance of Generator.
-
#normalize_action! ⇒ Object
Move ‘index’ action from options to recall.
-
#normalize_controller! ⇒ Object
Remove leading slashes from controllers.
-
#normalize_controller_action_id! ⇒ Object
This pulls :controller, :action, and :id out of the recall.
- #normalize_options! ⇒ Object
-
#normalize_recall! ⇒ Object
Set ‘index’ as default action for recall.
- #use_recall_for(key) ⇒ Object
-
#use_relative_controller! ⇒ Object
if the current controller is “foo/bar/baz” and controller: “baz/bat” is specified, the controller becomes “foo/baz/bat”.
Constructor Details
#initialize(options, recall, set) ⇒ Generator
Returns a new instance of Generator.
511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 511 def initialize(, recall, set) @named_route = .delete(:use_route) @options = .dup @recall = recall.dup @set = set normalize_recall! normalize_controller_action_id! use_relative_controller! normalize_controller! normalize_action! end |
Instance Attribute Details
#named_route ⇒ Object (readonly)
Returns the value of attribute named_route
509 510 511 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 509 def named_route @named_route end |
#options ⇒ Object (readonly)
Returns the value of attribute options
509 510 511 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 509 def @options end |
#recall ⇒ Object (readonly)
Returns the value of attribute recall
509 510 511 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 509 def recall @recall end |
#set ⇒ Object (readonly)
Returns the value of attribute set
509 510 511 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 509 def set @set end |
Instance Method Details
#controller ⇒ Object
525 526 527 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 525 def controller @options[:controller] end |
#current_controller ⇒ Object
529 530 531 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 529 def current_controller @recall[:controller] end |
#different_controller? ⇒ Boolean
606 607 608 609 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 606 def different_controller? return false unless current_controller controller.to_param != current_controller.to_param end |
#generate ⇒ Object
Generates a path from routes, returns [path, params]. If no route is generated the formatter will raise ActionController::UrlGenerationError
602 603 604 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 602 def generate @set.formatter.generate(:path_info, named_route, , recall, PARAMETERIZE) end |
#normalize_action! ⇒ Object
Move ‘index’ action from options to recall
594 595 596 597 598 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 594 def normalize_action! if @options[:action] == 'index' @recall[:action] = @options.delete(:action) end end |
#normalize_controller! ⇒ Object
Remove leading slashes from controllers
589 590 591 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 589 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.
571 572 573 574 575 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 571 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
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 546 def # 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 [:controller] [:action] ||= 'index' [:controller] = [:controller].to_s end if .key?(:action) [:action] = ([:action] || 'index').to_s end end |
#normalize_recall! ⇒ Object
Set ‘index’ as default action for recall
542 543 544 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 542 def normalize_recall! @recall[:action] ||= 'index' end |
#use_recall_for(key) ⇒ Object
533 534 535 536 537 538 539 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 533 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”
579 580 581 582 583 584 585 586 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 579 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 |