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
-
#handle_nil_action! ⇒ Object
This handles the case of :action => nil being explicitly passed.
-
#initialize(options, recall, set, extras = false) ⇒ Generator
constructor
A new instance of Generator.
-
#normalize_controller_action_id! ⇒ Object
This pulls :controller, :action, and :id out of the recall.
- #normalize_options! ⇒ Object
- #raise_routing_error ⇒ Object
- #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, extras = false) ⇒ Generator
Returns a new instance of Generator.
434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 434 def initialize(, recall, set, extras = false) @named_route = .delete(:use_route) @options = .dup @recall = recall.dup @set = set @extras = extras normalize_controller_action_id! use_relative_controller! controller.sub!(%r{^/}, '') if controller handle_nil_action! end |
Instance Attribute Details
#named_route ⇒ Object (readonly)
Returns the value of attribute named_route
432 433 434 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 432 def named_route @named_route end |
#options ⇒ Object (readonly)
Returns the value of attribute options
432 433 434 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 432 def @options end |
#recall ⇒ Object (readonly)
Returns the value of attribute recall
432 433 434 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 432 def recall @recall end |
#set ⇒ Object (readonly)
Returns the value of attribute set
432 433 434 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 432 def set @set end |
Instance Method Details
#controller ⇒ Object
448 449 450 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 448 def controller @controller ||= @options[:controller] end |
#current_controller ⇒ Object
452 453 454 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 452 def current_controller @recall[:controller] end |
#different_controller? ⇒ Boolean
535 536 537 538 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 535 def different_controller? return false unless current_controller controller.to_param != current_controller.to_param end |
#generate ⇒ Object
519 520 521 522 523 524 525 526 527 528 529 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 519 def generate path, params = @set.formatter.generate(:path_info, named_route, , recall, PARAMETERIZE) raise_routing_error unless path return [path, params.keys] if @extras [path, params] rescue Journey::Router::RoutingError raise_routing_error end |
#handle_nil_action! ⇒ Object
This handles the case of :action => nil being explicitly passed. It is identical to :action => “index”
512 513 514 515 516 517 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 512 def handle_nil_action! if .has_key?(:action) && [:action].nil? [:action] = 'index' end recall[:action] = .delete(:action) if [:action] == 'index' 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.
491 492 493 494 495 496 497 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 491 def normalize_controller_action_id! @recall[:action] ||= 'index' if current_controller use_recall_for(:controller) or return use_recall_for(:action) or return use_recall_for(:id) end |
#normalize_options! ⇒ Object
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 466 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 [:action] [:action] = [:action].to_s end end |
#raise_routing_error ⇒ Object
531 532 533 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 531 def raise_routing_error raise ActionController::RoutingError, "No route matches #{.inspect}" end |
#use_recall_for(key) ⇒ Object
456 457 458 459 460 461 462 463 464 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 456 def use_recall_for(key) if @recall[key] && (!@options.key?(key) || @options[key] == @recall[key]) if named_route_exists? @options[key] = @recall.delete(key) if segment_keys.include?(key) else @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”
501 502 503 504 505 506 507 508 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 501 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 @controller = @options[:controller] = parts.join("/") end end |