Class: ActionDispatch::Routing::RouteSet::Generator
Relationships & Source Files | |
Inherits: | Object |
Defined in: | actionpack/lib/action_dispatch/routing/route_set.rb |
Class Method Summary
Instance Attribute Summary
- #different_controller? ⇒ Boolean readonly
- #named_route readonly
- #options readonly
- #recall readonly
- #set readonly
- #named_route_exists? ⇒ Boolean readonly private
Instance Method Summary
- #controller
- #current_controller
-
#generate
Generates a path from routes, returns a RouteWithParams or MissingRoute.
-
#normalize_controller!
Remove leading slashes from controllers.
-
#normalize_controller_action_id!
This pulls
:controller
,:action
, and:id
out of the recall. - #normalize_options!
- #use_recall_for(key)
-
#use_relative_controller!
if the current controller is “foo/bar/baz” and controller: “baz/bat” is specified, the controller becomes “foo/baz/bat”.
- #segment_keys private
Constructor Details
.new(named_route, options, recall, set) ⇒ Generator
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 717
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
#different_controller? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 803
def different_controller? return false unless current_controller controller.to_param != current_controller.to_param end
#named_route (readonly)
[ GitHub ]
#named_route_exists? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 809
def named_route_exists? named_route && set.named_routes[named_route] end
#options (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 715
attr_reader :, :recall, :set, :named_route
#recall (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 715
attr_reader :, :recall, :set, :named_route
#set (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 715
attr_reader :, :recall, :set, :named_route
Instance Method Details
#controller
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 729
def controller @options[:controller] end
#current_controller
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 733
def current_controller @recall[:controller] end
#generate
Generates a path from routes, returns a RouteWithParams or MissingRoute. MissingRoute will raise ::ActionController::UrlGenerationError
.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 799
def generate @set.formatter.generate(named_route, , recall) end
#normalize_controller!
Remove leading slashes from controllers
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 787
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!
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.
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 769
def normalize_controller_action_id! use_recall_for(:controller) || return use_recall_for(:action) || return use_recall_for(:id) end
#normalize_options!
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 745
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
#segment_keys (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 813
def segment_keys set.named_routes[named_route].segment_keys end
#use_recall_for(key)
[ GitHub ]# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 737
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!
if the current controller is “foo/bar/baz” and controller: “baz/bat” is specified, the controller becomes “foo/baz/bat”
# File 'actionpack/lib/action_dispatch/routing/route_set.rb', line 777
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