Class: ActionDispatch::Routing::RouteSet
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::RouteSet
show all
- Defined in:
- lib/action_dispatch/routing/route_set.rb
Overview
Defined Under Namespace
Modules: MountedHelpers
Classes: Config, 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, :relative_url_root]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_route(mapping, path_ast, name, anchor) ⇒ Object
-
#api_only? ⇒ Boolean
-
#append(&block) ⇒ Object
-
#call(env) ⇒ Object
-
#clear! ⇒ Object
-
#define_mounted_helper(name) ⇒ Object
-
#draw(&block) ⇒ 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_relative_url_root(options) ⇒ Object
-
#find_script_name(options) ⇒ Object
-
#generate_extras(options, recall = {}) ⇒ 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) ⇒ Object
-
#prepend(&block) ⇒ Object
-
#recognize_path(path, environment = {}) ⇒ Object
-
#relative_url_root ⇒ Object
-
#request_class ⇒ Object
-
#url_for(options, route_name = nil, url_strategy = UNKNOWN) ⇒ 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.
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/action_dispatch/routing/route_set.rb', line 337
def initialize(config = DEFAULT_CONFIG)
self.named_routes = NamedRouteCollection.new
self.resources_path_names = self.class.default_resources_path_names
self.default_url_options = {}
@config = config
@append = []
@prepend = []
@disable_clear_and_finalize = false
@finalized = false
@env_key = "ROUTES_#{object_id}_SCRIPT_NAME".freeze
@set = Journey::Routes.new
@router = Journey::Router.new @set
@formatter = Journey::Formatter.new self
end
|
Instance Attribute Details
#default_scope ⇒ Object
Returns the value of attribute default_scope.
307
308
309
|
# File 'lib/action_dispatch/routing/route_set.rb', line 307
def default_scope
@default_scope
end
|
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
309
310
311
|
# File 'lib/action_dispatch/routing/route_set.rb', line 309
def default_url_options
@default_url_options
end
|
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
308
309
310
|
# File 'lib/action_dispatch/routing/route_set.rb', line 308
def disable_clear_and_finalize
@disable_clear_and_finalize
end
|
#env_key ⇒ Object
Returns the value of attribute env_key.
310
311
312
|
# File 'lib/action_dispatch/routing/route_set.rb', line 310
def env_key
@env_key
end
|
Returns the value of attribute formatter.
307
308
309
|
# File 'lib/action_dispatch/routing/route_set.rb', line 307
def formatter
@formatter
end
|
#named_routes ⇒ Object
Returns the value of attribute named_routes.
307
308
309
|
# File 'lib/action_dispatch/routing/route_set.rb', line 307
def named_routes
@named_routes
end
|
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
308
309
310
|
# File 'lib/action_dispatch/routing/route_set.rb', line 308
def resources_path_names
@resources_path_names
end
|
#router ⇒ Object
Returns the value of attribute router.
307
308
309
|
# File 'lib/action_dispatch/routing/route_set.rb', line 307
def router
@router
end
|
#set ⇒ Object
Also known as:
routes
Returns the value of attribute set.
307
308
309
|
# File 'lib/action_dispatch/routing/route_set.rb', line 307
def set
@set
end
|
Class Method Details
.default_resources_path_names ⇒ Object
314
315
316
|
# File 'lib/action_dispatch/routing/route_set.rb', line 314
def self.default_resources_path_names
{ :new => 'new', :edit => 'edit' }
end
|
.new_with_config(config) ⇒ Object
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/action_dispatch/routing/route_set.rb', line 318
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_route(mapping, path_ast, name, anchor) ⇒ Object
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
# File 'lib/action_dispatch/routing/route_set.rb', line 505
def add_route(mapping, path_ast, name, anchor)
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" \
"http://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)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a dynamic :controller segment in a route is deprecated and
will be removed in Rails 5.2.
MSG
end
if route.segment_keys.include?(:action)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using a dynamic :action segment in a route is deprecated and
will be removed in Rails 5.2.
MSG
end
route
end
|
#api_only? ⇒ Boolean
358
359
360
|
# File 'lib/action_dispatch/routing/route_set.rb', line 358
def api_only?
@config.api_only
end
|
#append(&block) ⇒ Object
378
379
380
|
# File 'lib/action_dispatch/routing/route_set.rb', line 378
def append(&block)
@append << block
end
|
#call(env) ⇒ Object
724
725
726
727
728
|
# File 'lib/action_dispatch/routing/route_set.rb', line 724
def call(env)
req = make_request(env)
req.path_info = Journey::Router::Utils.normalize_path(req.path_info)
@router.serve(req)
end
|
#clear! ⇒ Object
402
403
404
405
406
407
408
|
# File 'lib/action_dispatch/routing/route_set.rb', line 402
def clear!
@finalized = false
named_routes.clear
set.clear
formatter.clear
@prepend.each { |blk| eval_block(blk) }
end
|
#define_mounted_helper(name) ⇒ Object
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# File 'lib/action_dispatch/routing/route_set.rb', line 423
def define_mounted_helper(name)
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)
end
end
MountedHelpers.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{name}
@_#{name} ||= _#{name}
end
RUBY
end
|
#draw(&block) ⇒ Object
371
372
373
374
375
376
|
# File 'lib/action_dispatch/routing/route_set.rb', line 371
def draw(&block)
clear! unless @disable_clear_and_finalize
eval_block(block)
finalize! unless @disable_clear_and_finalize
nil
end
|
#empty? ⇒ Boolean
501
502
503
|
# File 'lib/action_dispatch/routing/route_set.rb', line 501
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.
651
652
653
|
# File 'lib/action_dispatch/routing/route_set.rb', line 651
def (options, recall={})
(options, recall).last
end
|
#finalize! ⇒ Object
396
397
398
399
400
|
# File 'lib/action_dispatch/routing/route_set.rb', line 396
def finalize!
return if @finalized
@append.each { |blk| eval_block(blk) }
@finalized = true
end
|
#find_relative_url_root(options) ⇒ Object
678
679
680
|
# File 'lib/action_dispatch/routing/route_set.rb', line 678
def find_relative_url_root(options)
options.delete(:relative_url_root) || relative_url_root
end
|
#find_script_name(options) ⇒ Object
674
675
676
|
# File 'lib/action_dispatch/routing/route_set.rb', line 674
def find_script_name(options)
options.delete(:script_name) || find_relative_url_root(options) || ''
end
|
655
656
657
658
659
|
# File 'lib/action_dispatch/routing/route_set.rb', line 655
def (options, recall={})
route_key = options.delete :use_route
path, params = generate(route_key, options, recall)
return path, params.keys
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.
419
420
421
|
# File 'lib/action_dispatch/routing/route_set.rb', line 419
def mounted_helpers
MountedHelpers
end
|
#optimize_routes_generation? ⇒ Boolean
670
671
672
|
# File 'lib/action_dispatch/routing/route_set.rb', line 670
def optimize_routes_generation?
default_url_options.empty?
end
|
#path_for(options, route_name = nil) ⇒ Object
682
683
684
|
# File 'lib/action_dispatch/routing/route_set.rb', line 682
def path_for(options, route_name = nil)
url_for(options, route_name, PATH)
end
|
#prepend(&block) ⇒ Object
382
383
384
|
# File 'lib/action_dispatch/routing/route_set.rb', line 382
def prepend(&block)
@prepend << block
end
|
#recognize_path(path, environment = {}) ⇒ Object
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
# File 'lib/action_dispatch/routing/route_set.rb', line 730
def recognize_path(path, environment = {})
method = (environment[:method] || "GET").to_s.upcase
path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://}
= 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)
@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.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
end
end
raise ActionController::RoutingError, "No route matches #{path.inspect}"
end
|
#relative_url_root ⇒ Object
354
355
356
|
# File 'lib/action_dispatch/routing/route_set.rb', line 354
def relative_url_root
@config.relative_url_root
end
|
#request_class ⇒ Object
362
363
364
|
# File 'lib/action_dispatch/routing/route_set.rb', line 362
def request_class
ActionDispatch::Request
end
|
#url_for(options, route_name = nil, url_strategy = UNKNOWN) ⇒ Object
The options
argument must be a hash whose keys are symbols.
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
# File 'lib/action_dispatch/routing/route_set.rb', line 687
def url_for(options, route_name = nil, url_strategy = UNKNOWN)
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_OPTIONS.each { |ro| path_options.delete ro }
path, params = generate(route_name, path_options, recall)
if options.key? :params
params.merge! options[:params]
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'lib/action_dispatch/routing/route_set.rb', line 442
def url_helpers(supports_path = true)
routes = self
Module.new do
extend ActiveSupport::Concern
include UrlFor
@_routes = routes
class << self
def url_for(options)
@_routes.url_for(options)
end
def optimize_routes_generation?
@_routes.optimize_routes_generation?
end
attr_reader :_routes
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
singleton_class.send(:redefine_method, :_routes) { routes }
end
define_method(:_routes) { @_routes || routes }
define_method(:_generate_paths_by_default) do
supports_path
end
private :_generate_paths_by_default
end
end
|