Class: ActionDispatch::Routing::RouteSet::Generator
- Defined in:
- actionpack/lib/action_dispatch/routing/route_set.rb
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 a RouteWithParams or MissingRoute.
-
#initialize(named_route, options, recall, set) ⇒ Generator
constructor
A new instance of Generator.
-
#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
- #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(named_route, options, recall, set) ⇒ Generator
Returns a new instance of Generator.
690 691 692 693 694 695 696 697 698 699 700 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 690 def initialize(named_route, , recall, set) @named_route = named_route @options = @recall = recall @set = set normalize_controller_action_id! use_relative_controller! normalize_controller! end |
Instance Attribute Details
#named_route ⇒ Object (readonly)
Returns the value of attribute named_route
688 689 690 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 688 def named_route @named_route end |
#options ⇒ Object (readonly)
Returns the value of attribute options
688 689 690 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 688 def @options end |
#recall ⇒ Object (readonly)
Returns the value of attribute recall
688 689 690 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 688 def recall @recall end |
#set ⇒ Object (readonly)
Returns the value of attribute set
688 689 690 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 688 def set @set end |
Instance Method Details
#controller ⇒ Object
702 703 704 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 702 def controller @options[:controller] end |
#current_controller ⇒ Object
706 707 708 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 706 def current_controller @recall[:controller] end |
#different_controller? ⇒ Boolean
777 778 779 780 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 777 def different_controller? return false unless current_controller controller.to_param != current_controller.to_param end |
#generate ⇒ Object
Generates a path from routes, returns a RouteWithParams or MissingRoute. MissingRoute will raise ActionController::UrlGenerationError.
773 774 775 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 773 def generate @set.formatter.generate(named_route, , recall) end |
#normalize_controller! ⇒ Object
Remove leading slashes from controllers
761 762 763 764 765 766 767 768 769 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 761 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.
743 744 745 746 747 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 743 def normalize_controller_action_id! use_recall_for(:controller) || return use_recall_for(:action) || return use_recall_for(:id) end |
#normalize_options! ⇒ Object
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 718 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 |
#use_recall_for(key) ⇒ Object
710 711 712 713 714 715 716 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 710 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”
751 752 753 754 755 756 757 758 |
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 751 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 |