Class: ActionDispatch::Routing::RouteSet
- Inherits:
-
Object
- Object
- ActionDispatch::Routing::RouteSet
- Defined in:
- lib/action_dispatch/routing/route_set.rb
Overview
:stopdoc:
Defined Under Namespace
Modules: MountedHelpers Classes: Dispatcher, Generator, NamedRouteCollection
Constant Summary collapse
- PATH =
strategy for building urls to send to the client
->() { ActionDispatch::Http::URL.path_for() }
- FULL =
->() { ActionDispatch::Http::URL.full_url_for() }
- UNKNOWN =
->() { ActionDispatch::Http::URL.url_for() }
- LEGACY =
->() { if .key?(:only_path) if [:only_path] ActiveSupport::Deprecation.warn(<<-MSG.squish) You are calling a `*_path` helper with the `only_path` option explicitly set to `true`. This option will stop working on path helpers in Rails 5. Simply remove the `only_path: true` argument from your call as it is redundant when applied to a path helper. MSG PATH.call() else ActiveSupport::Deprecation.warn(<<-MSG.squish) You are calling a `*_path` helper with the `only_path` option explicitly set to `false`. This option will stop working on path helpers in Rails 5. Use the corresponding `*_url` helper instead. MSG FULL.call() end else PATH.call() end }
- 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
-
#default_scope ⇒ Object
Returns the value of attribute default_scope.
-
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
-
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#named_routes ⇒ Object
Returns the value of attribute named_routes.
-
#request_class ⇒ Object
Returns the value of attribute request_class.
-
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
-
#router ⇒ Object
Returns the value of attribute router.
-
#set ⇒ Object
(also: #routes)
Returns the value of attribute set.
Class Method Summary collapse
Instance Method Summary collapse
- #add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) ⇒ Object
- #append(&block) ⇒ Object
- #call(env) ⇒ Object
- #clear! ⇒ Object
- #define_mounted_helper(name) ⇒ Object
- #dispatcher(defaults) ⇒ 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(request_class = ActionDispatch::Request) ⇒ 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
-
#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(request_class = ActionDispatch::Request) ⇒ RouteSet
Returns a new instance of RouteSet.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/action_dispatch/routing/route_set.rb', line 392 def initialize(request_class = ActionDispatch::Request) self.named_routes = NamedRouteCollection.new self.resources_path_names = self.class.default_resources_path_names self. = {} self.request_class = request_class @append = [] @prepend = [] @disable_clear_and_finalize = false @finalized = false @set = Journey::Routes.new @router = Journey::Router.new @set @formatter = Journey::Formatter.new @set end |
Instance Attribute Details
#default_scope ⇒ Object
Returns the value of attribute default_scope.
382 383 384 |
# File 'lib/action_dispatch/routing/route_set.rb', line 382 def default_scope @default_scope end |
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
384 385 386 |
# File 'lib/action_dispatch/routing/route_set.rb', line 384 def @default_url_options end |
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
383 384 385 |
# File 'lib/action_dispatch/routing/route_set.rb', line 383 def disable_clear_and_finalize @disable_clear_and_finalize end |
#formatter ⇒ Object
Returns the value of attribute formatter.
382 383 384 |
# File 'lib/action_dispatch/routing/route_set.rb', line 382 def formatter @formatter end |
#named_routes ⇒ Object
Returns the value of attribute named_routes.
382 383 384 |
# File 'lib/action_dispatch/routing/route_set.rb', line 382 def named_routes @named_routes end |
#request_class ⇒ Object
Returns the value of attribute request_class.
384 385 386 |
# File 'lib/action_dispatch/routing/route_set.rb', line 384 def request_class @request_class end |
#resources_path_names ⇒ Object
Returns the value of attribute resources_path_names.
383 384 385 |
# File 'lib/action_dispatch/routing/route_set.rb', line 383 def resources_path_names @resources_path_names end |
#router ⇒ Object
Returns the value of attribute router.
382 383 384 |
# File 'lib/action_dispatch/routing/route_set.rb', line 382 def router @router end |
#set ⇒ Object Also known as: routes
Returns the value of attribute set.
382 383 384 |
# File 'lib/action_dispatch/routing/route_set.rb', line 382 def set @set end |
Class Method Details
.default_resources_path_names ⇒ Object
388 389 390 |
# File 'lib/action_dispatch/routing/route_set.rb', line 388 def self.default_resources_path_names { :new => 'new', :edit => 'edit' } end |
Instance Method Details
#add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/action_dispatch/routing/route_set.rb', line 543 def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) 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 path = conditions.delete :path_info ast = conditions.delete :parsed_path_info path = build_path(path, ast, requirements, anchor) conditions = build_conditions(conditions, path.names.map { |x| x.to_sym }) route = @set.add_route(app, path, conditions, defaults, name) named_routes[name] = route if name route end |
#append(&block) ⇒ Object
415 416 417 |
# File 'lib/action_dispatch/routing/route_set.rb', line 415 def append(&block) @append << block end |
#call(env) ⇒ Object
812 813 814 815 816 |
# File 'lib/action_dispatch/routing/route_set.rb', line 812 def call(env) req = request_class.new(env) req.path_info = Journey::Router::Utils.normalize_path(req.path_info) @router.serve(req) end |
#clear! ⇒ Object
443 444 445 446 447 448 449 |
# File 'lib/action_dispatch/routing/route_set.rb', line 443 def clear! @finalized = false named_routes.clear set.clear formatter.clear @prepend.each { |blk| eval_block(blk) } end |
#define_mounted_helper(name) ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/action_dispatch/routing/route_set.rb', line 468 def define_mounted_helper(name) return if MountedHelpers.method_defined?(name) routes = self MountedHelpers.class_eval do define_method "_#{name}" do RoutesProxy.new(routes, _routes_context) end end MountedHelpers.class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def #{name} @_#{name} ||= _#{name} end RUBY end |
#dispatcher(defaults) ⇒ Object
451 452 453 |
# File 'lib/action_dispatch/routing/route_set.rb', line 451 def dispatcher(defaults) Routing::RouteSet::Dispatcher.new(defaults) end |
#draw(&block) ⇒ Object
408 409 410 411 412 413 |
# File 'lib/action_dispatch/routing/route_set.rb', line 408 def draw(&block) clear! unless @disable_clear_and_finalize eval_block(block) finalize! unless @disable_clear_and_finalize nil end |
#empty? ⇒ Boolean
539 540 541 |
# File 'lib/action_dispatch/routing/route_set.rb', line 539 def empty? routes.empty? end |
#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.
739 740 741 |
# File 'lib/action_dispatch/routing/route_set.rb', line 739 def extra_keys(, recall={}) generate_extras(, recall).last end |
#finalize! ⇒ Object
437 438 439 440 441 |
# File 'lib/action_dispatch/routing/route_set.rb', line 437 def finalize! return if @finalized @append.each { |blk| eval_block(blk) } @finalized = true end |
#find_relative_url_root(options) ⇒ Object
766 767 768 |
# File 'lib/action_dispatch/routing/route_set.rb', line 766 def find_relative_url_root() .delete(:relative_url_root) || relative_url_root end |
#find_script_name(options) ⇒ Object
762 763 764 |
# File 'lib/action_dispatch/routing/route_set.rb', line 762 def find_script_name() .delete(:script_name) || find_relative_url_root() || '' end |
#generate_extras(options, recall = {}) ⇒ Object
743 744 745 746 747 |
# File 'lib/action_dispatch/routing/route_set.rb', line 743 def generate_extras(, recall={}) route_key = .delete :use_route path, params = generate(route_key, , 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.
464 465 466 |
# File 'lib/action_dispatch/routing/route_set.rb', line 464 def mounted_helpers MountedHelpers end |
#optimize_routes_generation? ⇒ Boolean
758 759 760 |
# File 'lib/action_dispatch/routing/route_set.rb', line 758 def optimize_routes_generation? .empty? end |
#path_for(options, route_name = nil) ⇒ Object
770 771 772 |
# File 'lib/action_dispatch/routing/route_set.rb', line 770 def path_for(, route_name = nil) url_for(, route_name, PATH) end |
#prepend(&block) ⇒ Object
419 420 421 |
# File 'lib/action_dispatch/routing/route_set.rb', line 419 def prepend(&block) @prepend << block end |
#recognize_path(path, environment = {}) ⇒ Object
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/action_dispatch/routing/route_set.rb', line 818 def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://} extras = environment[:extras] || {} begin env = Rack::MockRequest.env_for(path, {:method => method}) rescue URI::InvalidURIError => e raise ActionController::RoutingError, e. end req = request_class.new(env) @router.recognize(req) do |route, params| params.merge!(extras) 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 old_params = req.path_parameters req.path_parameters = old_params.merge params app = route.app if app.matches?(req) && app.dispatcher? dispatcher = app.app if dispatcher.controller(params, false) dispatcher.prepare_params!(params) return params else raise ActionController::RoutingError, "A route matches #{path.inspect}, but references missing controller: #{params[:controller].camelize}Controller" end end end raise ActionController::RoutingError, "No route matches #{path.inspect}" end |
#url_for(options, route_name = nil, url_strategy = UNKNOWN) ⇒ Object
The options
argument must be a hash whose keys are symbols.
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/action_dispatch/routing/route_set.rb', line 775 def url_for(, route_name = nil, url_strategy = UNKNOWN) = .merge user = password = nil if [:user] && [:password] user = .delete :user password = .delete :password end recall = .delete(:_recall) { {} } original_script_name = .delete(:original_script_name) script_name = find_script_name if original_script_name script_name = original_script_name + script_name end = .dup RESERVED_OPTIONS.each { |ro| .delete ro } path, params = generate(route_name, , recall) if .key? :params params.merge! [:params] end [:path] = path [:script_name] = script_name [:params] = params [:user] = user [:password] = password url_strategy.call end |
#url_helpers(supports_path = true) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 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 535 536 537 |
# File 'lib/action_dispatch/routing/route_set.rb', line 485 def url_helpers(supports_path = true) routes = self Module.new do extend ActiveSupport::Concern include UrlFor # Define url_for in the singleton level so one can do: # Rails.application.routes.url_helpers.url_for(args) @_routes = routes class << self delegate :url_for, :optimize_routes_generation?, to: '@_routes' attr_reader :_routes def ; {}; end end url_helpers = routes.named_routes.url_helpers_module # Make named_routes available in the module singleton # as well, so one can do: # Rails.application.routes.url_helpers.posts_path extend url_helpers # Any class that includes this module will get all # named routes... include url_helpers if supports_path path_helpers = routes.named_routes.path_helpers_module else path_helpers = routes.named_routes.path_helpers_module(true) end include path_helpers extend path_helpers # plus a singleton class method called _routes ... included do singleton_class.send(:redefine_method, :_routes) { routes } end # And an instance method _routes. Note that # UrlFor (included in this module) add extra # conveniences for working with @_routes. define_method(:_routes) { @_routes || routes } define_method(:_generate_paths_by_default) do supports_path end private :_generate_paths_by_default end end |