Class: ActionDispatch::Routing::RouteSet
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::RouteSet
show all
- Defined in:
- lib/action_dispatch/routing/route_set.rb
Overview
The RouteSet contains a collection of Route instances, representing the routes typically defined in ‘config/routes.rb`.
Defined Under Namespace
Modules: MountedHelpers
Classes: Config, CustomUrlHelper, Dispatcher, Generator, NamedRouteCollection, StaticDispatcher
Constant Summary
collapse
- PATH =
strategy for building URLs to send to the client
->(options) { ActionDispatch::Http::URL.path_for(options) }
- UNKNOWN =
->(options) { ActionDispatch::Http::URL.url_for(options) }
- DEFAULT_CONFIG =
Config.new(nil, false)
- RESERVED_OPTIONS =
[:host, :protocol, :port, :subdomain, :domain, :tld_length,
:trailing_slash, :anchor, :params, :only_path, :script_name,
:original_script_name]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_polymorphic_mapping(klass, options, &block) ⇒ Object
-
#add_route(mapping, name) ⇒ Object
-
#add_url_helper(name, options, &block) ⇒ Object
-
#api_only? ⇒ Boolean
-
#append(&block) ⇒ Object
-
#call(env) ⇒ Object
-
#clear! ⇒ Object
-
#default_env ⇒ Object
-
#define_mounted_helper(name, script_namer = nil) ⇒ Object
-
#draw(&block) ⇒ Object
-
#eager_load! ⇒ Object
-
#empty? ⇒ Boolean
-
#extra_keys(options, recall = {}) ⇒ Object
Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.
-
#finalize! ⇒ Object
-
#find_script_name(options) ⇒ Object
-
#from_requirements(requirements) ⇒ Object
Returns a Route matching the given requirements, or ‘nil` if none are found.
-
#generate_extras(options, recall = {}) ⇒ Object
-
#generate_url_helpers(supports_path) ⇒ Object
-
#initialize(config = DEFAULT_CONFIG) ⇒ RouteSet
constructor
A new instance of RouteSet.
-
#mounted_helpers ⇒ Object
Contains all the mounted helpers across different engines and the ‘main_app` helper for the application.
-
#optimize_routes_generation? ⇒ Boolean
-
#path_for(options, route_name = nil, reserved = RESERVED_OPTIONS) ⇒ Object
-
#prepend(&block) ⇒ Object
-
#recognize_path(path, environment = {}) ⇒ Object
-
#recognize_path_with_request(req, path, extras, raise_on_missing: true) ⇒ Object
-
#relative_url_root ⇒ Object
-
#request_class ⇒ Object
-
#url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil, reserved = RESERVED_OPTIONS) ⇒ Object
The ‘options` argument must be a hash whose keys are symbols.
-
#url_helpers(supports_path = true) ⇒ Object
Constructor Details
#initialize(config = DEFAULT_CONFIG) ⇒ RouteSet
Returns a new instance of RouteSet.
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/action_dispatch/routing/route_set.rb', line 384
def initialize(config = DEFAULT_CONFIG)
self.named_routes = NamedRouteCollection.new
self.resources_path_names = self.class.default_resources_path_names
self.default_url_options = {}
self.draw_paths = []
@config = config
@append = []
@prepend = []
@disable_clear_and_finalize = false
@finalized = false
@env_key = "ROUTES_#{object_id}_SCRIPT_NAME"
@default_env = nil
@set = Journey::Routes.new
@router = Journey::Router.new @set
@formatter = Journey::Formatter.new self
@polymorphic_mappings = {}
end
|
Instance Attribute Details
#default_scope ⇒ Object
Returns the value of attribute default_scope.
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def default_scope
@default_scope
end
|
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
356
357
358
|
# File 'lib/action_dispatch/routing/route_set.rb', line 356
def default_url_options
@default_url_options
end
|
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
355
356
357
|
# File 'lib/action_dispatch/routing/route_set.rb', line 355
def disable_clear_and_finalize
@disable_clear_and_finalize
end
|
#draw_paths ⇒ Object
Returns the value of attribute draw_paths.
356
357
358
|
# File 'lib/action_dispatch/routing/route_set.rb', line 356
def draw_paths
@draw_paths
end
|
#env_key ⇒ Object
Returns the value of attribute env_key.
357
358
359
|
# File 'lib/action_dispatch/routing/route_set.rb', line 357
def env_key
@env_key
end
|
Returns the value of attribute formatter.
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def formatter
@formatter
end
|
#named_routes ⇒ Object
Returns the value of attribute named_routes.
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def named_routes
@named_routes
end
|
#polymorphic_mappings ⇒ Object
Returns the value of attribute polymorphic_mappings.
357
358
359
|
# File 'lib/action_dispatch/routing/route_set.rb', line 357
def polymorphic_mappings
@polymorphic_mappings
end
|
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
355
356
357
|
# File 'lib/action_dispatch/routing/route_set.rb', line 355
def resources_path_names
@resources_path_names
end
|
#router ⇒ Object
Returns the value of attribute router.
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def router
@router
end
|
#set ⇒ Object
Also known as:
routes
Returns the value of attribute set.
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def set
@set
end
|
Class Method Details
.default_resources_path_names ⇒ Object
361
362
363
|
# File 'lib/action_dispatch/routing/route_set.rb', line 361
def self.default_resources_path_names
{ new: "new", edit: "edit" }
end
|
.new_with_config(config) ⇒ Object
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/action_dispatch/routing/route_set.rb', line 365
def self.new_with_config(config)
route_set_config = DEFAULT_CONFIG
if config.respond_to? :relative_url_root
route_set_config.relative_url_root = config.relative_url_root
end
if config.respond_to? :api_only
route_set_config.api_only = config.api_only
end
new route_set_config
end
|
Instance Method Details
#add_polymorphic_mapping(klass, options, &block) ⇒ Object
664
665
666
|
# File 'lib/action_dispatch/routing/route_set.rb', line 664
def add_polymorphic_mapping(klass, options, &block)
@polymorphic_mappings[klass] = CustomUrlHelper.new(klass, options, &block)
end
|
#add_route(mapping, name) ⇒ Object
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
# File 'lib/action_dispatch/routing/route_set.rb', line 633
def add_route(mapping, name)
raise ArgumentError, "Invalid route name: '#{name}'" unless name.blank? || name.to_s.match(/^[_a-z]\w*$/i)
if name && named_routes[name]
raise ArgumentError, "Invalid route name, already in use: '#{name}' \n" \
"You may have defined two routes with the same name using the `:as` option, or " \
"you may be overriding a route already defined by a resource with the same naming. " \
"For the latter, you can restrict the routes created with `resources` as explained here: \n" \
"https://guides.rubyonrails.org/routing.html#restricting-the-routes-created"
end
route = @set.add_route(name, mapping)
named_routes[name] = route if name
if route.segment_keys.include?(:controller)
ActionDispatch.deprecator.warn(<<-MSG.squish)
Using a dynamic :controller segment in a route is deprecated and
will be removed in Rails 7.2.
MSG
end
if route.segment_keys.include?(:action)
ActionDispatch.deprecator.warn(<<-MSG.squish)
Using a dynamic :action segment in a route is deprecated and
will be removed in Rails 7.2.
MSG
end
route
end
|
#add_url_helper(name, options, &block) ⇒ Object
668
669
670
|
# File 'lib/action_dispatch/routing/route_set.rb', line 668
def add_url_helper(name, options, &block)
named_routes.add_url_helper(name, options, &block)
end
|
#api_only? ⇒ Boolean
415
416
417
|
# File 'lib/action_dispatch/routing/route_set.rb', line 415
def api_only?
@config.api_only
end
|
#append(&block) ⇒ Object
454
455
456
|
# File 'lib/action_dispatch/routing/route_set.rb', line 454
def append(&block)
@append << block
end
|
#call(env) ⇒ Object
893
894
895
896
897
|
# File 'lib/action_dispatch/routing/route_set.rb', line 893
def call(env)
req = make_request(env)
req.path_info = Journey::Router::Utils.normalize_path(req.path_info)
@router.serve(req)
end
|
#clear! ⇒ Object
478
479
480
481
482
483
484
485
|
# File 'lib/action_dispatch/routing/route_set.rb', line 478
def clear!
@finalized = false
named_routes.clear
set.clear
formatter.clear
@polymorphic_mappings.clear
@prepend.each { |blk| eval_block(blk) }
end
|
#default_env ⇒ Object
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
# File 'lib/action_dispatch/routing/route_set.rb', line 428
def default_env
if default_url_options != @default_env&.[]("action_dispatch.routes.default_url_options")
url_options = default_url_options.dup.freeze
uri = URI(ActionDispatch::Http::URL.full_url_for(host: "example.org", **url_options))
@default_env = {
"action_dispatch.routes" => self,
"action_dispatch.routes.default_url_options" => url_options,
"HTTPS" => uri.scheme == "https" ? "on" : "off",
"rack.url_scheme" => uri.scheme,
"HTTP_HOST" => uri.port == uri.default_port ? uri.host : "#{uri.host}:#{uri.port}",
"SCRIPT_NAME" => uri.path.chomp("/"),
"rack.input" => "",
}.freeze
end
@default_env
end
|
#define_mounted_helper(name, script_namer = nil) ⇒ Object
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
# File 'lib/action_dispatch/routing/route_set.rb', line 499
def define_mounted_helper(name, script_namer = nil)
return if MountedHelpers.method_defined?(name)
routes = self
helpers = routes.url_helpers
MountedHelpers.class_eval do
define_method "_#{name}" do
RoutesProxy.new(routes, _routes_context, helpers, script_namer)
end
end
MountedHelpers.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{name}
@_#{name} ||= _#{name}
end
RUBY
end
|
#draw(&block) ⇒ Object
447
448
449
450
451
452
|
# File 'lib/action_dispatch/routing/route_set.rb', line 447
def draw(&block)
clear! unless @disable_clear_and_finalize
eval_block(block)
finalize! unless @disable_clear_and_finalize
nil
end
|
#eager_load! ⇒ Object
404
405
406
407
408
409
|
# File 'lib/action_dispatch/routing/route_set.rb', line 404
def eager_load!
router.eager_load!
routes.each(&:eager_load!)
formatter.eager_load!
nil
end
|
#empty? ⇒ Boolean
629
630
631
|
# File 'lib/action_dispatch/routing/route_set.rb', line 629
def empty?
routes.empty?
end
|
Generate the path indicated by the arguments, and return an array of the keys that were not used to generate it.
808
809
810
|
# File 'lib/action_dispatch/routing/route_set.rb', line 808
def (options, recall = {})
(options, recall).last
end
|
#finalize! ⇒ Object
472
473
474
475
476
|
# File 'lib/action_dispatch/routing/route_set.rb', line 472
def finalize!
return if @finalized
@append.each { |blk| eval_block(blk) }
@finalized = true
end
|
#find_script_name(options) ⇒ Object
836
837
838
|
# File 'lib/action_dispatch/routing/route_set.rb', line 836
def find_script_name(options)
options.delete(:script_name) || relative_url_root || ""
end
|
#from_requirements(requirements) ⇒ Object
Returns a Route matching the given requirements, or ‘nil` if none are found.
This is intended for use by tools such as Language Servers.
Given the routes are defined as:
resources :posts
Then the following will return the Route for the ‘show` action:
Rails.application.routes.from_requirements(controller: "posts", action: "show")
29
30
31
|
# File 'lib/action_dispatch/routing/route_set.rb', line 29
def from_requirements(requirements)
routes.find { |route| route.requirements == requirements }
end
|
812
813
814
815
816
817
818
819
820
821
|
# File 'lib/action_dispatch/routing/route_set.rb', line 812
def (options, recall = {})
if recall
options = options.merge(_recall: recall)
end
route_name = options.delete :use_route
generator = generate(route_name, options, recall)
path_info = path_for(options, route_name, [])
[URI(path_info).path, generator.params.except(:_recall).keys]
end
|
#generate_url_helpers(supports_path) ⇒ Object
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
# File 'lib/action_dispatch/routing/route_set.rb', line 526
def generate_url_helpers(supports_path)
routes = self
Module.new do
extend ActiveSupport::Concern
include UrlFor
proxy_class = Class.new do
include UrlFor
include routes.named_routes.path_helpers_module
include routes.named_routes.url_helpers_module
attr_reader :_routes
def initialize(routes)
@_routes = routes
end
def optimize_routes_generation?
@_routes.optimize_routes_generation?
end
end
@_proxy = proxy_class.new(routes)
class << self
def url_for(options)
@_proxy.url_for(options)
end
def full_url_for(options)
@_proxy.full_url_for(options)
end
def route_for(name, *args)
@_proxy.route_for(name, *args)
end
def optimize_routes_generation?
@_proxy.optimize_routes_generation?
end
def polymorphic_url(record_or_hash_or_array, options = {})
@_proxy.polymorphic_url(record_or_hash_or_array, options)
end
def polymorphic_path(record_or_hash_or_array, options = {})
@_proxy.polymorphic_path(record_or_hash_or_array, options)
end
def _routes; @_proxy._routes; end
def url_options; {}; end
end
url_helpers = routes.named_routes.url_helpers_module
extend url_helpers
include url_helpers
if supports_path
path_helpers = routes.named_routes.path_helpers_module
include path_helpers
extend path_helpers
end
included do
redefine_singleton_method(:_routes) { routes }
end
define_method(:_routes) { @_routes || routes }
define_method(:_generate_paths_by_default) do
supports_path
end
private :_generate_paths_by_default
def self.included(base)
super
if base.respond_to?(:_routes) && !base._routes.equal?(@_proxy._routes)
@dup_for_reinclude ||= self.dup
base.include @dup_for_reinclude
end
end
end
end
|
#mounted_helpers ⇒ Object
Contains all the mounted helpers across different engines and the ‘main_app` helper for the application. You can include this in your classes if you want to access routes for other engines.
495
496
497
|
# File 'lib/action_dispatch/routing/route_set.rb', line 495
def mounted_helpers
MountedHelpers
end
|
#optimize_routes_generation? ⇒ Boolean
832
833
834
|
# File 'lib/action_dispatch/routing/route_set.rb', line 832
def optimize_routes_generation?
default_url_options.empty?
end
|
#path_for(options, route_name = nil, reserved = RESERVED_OPTIONS) ⇒ Object
840
841
842
|
# File 'lib/action_dispatch/routing/route_set.rb', line 840
def path_for(options, route_name = nil, reserved = RESERVED_OPTIONS)
url_for(options, route_name, PATH, nil, reserved)
end
|
#prepend(&block) ⇒ Object
458
459
460
|
# File 'lib/action_dispatch/routing/route_set.rb', line 458
def prepend(&block)
@prepend << block
end
|
#recognize_path(path, environment = {}) ⇒ Object
899
900
901
902
903
904
905
906
907
908
909
910
911
912
|
# File 'lib/action_dispatch/routing/route_set.rb', line 899
def recognize_path(path, environment = {})
method = (environment[:method] || "GET").to_s.upcase
path = Journey::Router::Utils.normalize_path(path) unless path&.include?("://")
= environment[:extras] || {}
begin
env = Rack::MockRequest.env_for(path, method: method)
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end
req = make_request(env)
recognize_path_with_request(req, path, )
end
|
#recognize_path_with_request(req, path, extras, raise_on_missing: true) ⇒ Object
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
|
# File 'lib/action_dispatch/routing/route_set.rb', line 914
def recognize_path_with_request(req, path, , raise_on_missing: true)
@router.recognize(req) do |route, params|
params.merge!()
params.each do |key, value|
if value.is_a?(String)
value = value.dup.force_encoding(Encoding::BINARY)
params[key] = URI::DEFAULT_PARSER.unescape(value)
end
end
req.path_parameters = params
app = route.app
if app.matches?(req) && app.dispatcher?
begin
req.controller_class
rescue NameError
raise ActionController::RoutingError, "A route matches #{path.inspect}, but references missing controller: #{params[:controller].camelize}Controller"
end
return req.path_parameters
elsif app.matches?(req) && app.engine?
path_parameters = app.rack_app.routes.recognize_path_with_request(req, path, , raise_on_missing: false)
return path_parameters if path_parameters
end
end
if raise_on_missing
raise ActionController::RoutingError, "No route matches #{path.inspect}"
end
end
|
#relative_url_root ⇒ Object
411
412
413
|
# File 'lib/action_dispatch/routing/route_set.rb', line 411
def relative_url_root
@config.relative_url_root
end
|
#request_class ⇒ Object
419
420
421
|
# File 'lib/action_dispatch/routing/route_set.rb', line 419
def request_class
ActionDispatch::Request
end
|
#url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil, reserved = RESERVED_OPTIONS) ⇒ Object
The ‘options` argument must be a hash whose keys are symbols.
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
|
# File 'lib/action_dispatch/routing/route_set.rb', line 845
def url_for(options, route_name = nil, url_strategy = UNKNOWN, method_name = nil, reserved = RESERVED_OPTIONS)
options = default_url_options.merge options
user = password = nil
if options[:user] && options[:password]
user = options.delete :user
password = options.delete :password
end
recall = options.delete(:_recall) { {} }
original_script_name = options.delete(:original_script_name)
script_name = find_script_name options
if original_script_name
script_name = original_script_name + script_name
end
path_options = options.dup
reserved.each { |ro| path_options.delete ro }
route_with_params = generate(route_name, path_options, recall)
path = route_with_params.path(method_name)
if options[:trailing_slash] && !options[:format] && !path.end_with?("/")
path += "/"
end
params = route_with_params.params
if options.key? :params
if options[:params].respond_to?(:to_hash)
params.merge! options[:params]
else
params[:params] = options[:params]
end
end
options[:path] = path
options[:script_name] = script_name
options[:params] = params
options[:user] = user
options[:password] = password
url_strategy.call options
end
|
#url_helpers(supports_path = true) ⇒ Object
518
519
520
521
522
523
524
|
# File 'lib/action_dispatch/routing/route_set.rb', line 518
def url_helpers(supports_path = true)
if supports_path
@url_helpers_with_paths ||= generate_url_helpers(true)
else
@url_helpers_without_paths ||= generate_url_helpers(false)
end
end
|