omg-actionpack 8.0.0.alpha1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +129 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +57 -0
- data/lib/abstract_controller/asset_paths.rb +14 -0
- data/lib/abstract_controller/base.rb +299 -0
- data/lib/abstract_controller/caching/fragments.rb +149 -0
- data/lib/abstract_controller/caching.rb +68 -0
- data/lib/abstract_controller/callbacks.rb +265 -0
- data/lib/abstract_controller/collector.rb +44 -0
- data/lib/abstract_controller/deprecator.rb +9 -0
- data/lib/abstract_controller/error.rb +8 -0
- data/lib/abstract_controller/helpers.rb +243 -0
- data/lib/abstract_controller/logger.rb +16 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +25 -0
- data/lib/abstract_controller/rendering.rb +126 -0
- data/lib/abstract_controller/translation.rb +42 -0
- data/lib/abstract_controller/url_for.rb +37 -0
- data/lib/abstract_controller.rb +36 -0
- data/lib/action_controller/api/api_rendering.rb +18 -0
- data/lib/action_controller/api.rb +155 -0
- data/lib/action_controller/base.rb +332 -0
- data/lib/action_controller/caching.rb +49 -0
- data/lib/action_controller/deprecator.rb +9 -0
- data/lib/action_controller/form_builder.rb +55 -0
- data/lib/action_controller/log_subscriber.rb +96 -0
- data/lib/action_controller/metal/allow_browser.rb +123 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +17 -0
- data/lib/action_controller/metal/conditional_get.rb +341 -0
- data/lib/action_controller/metal/content_security_policy.rb +86 -0
- data/lib/action_controller/metal/cookies.rb +20 -0
- data/lib/action_controller/metal/data_streaming.rb +154 -0
- data/lib/action_controller/metal/default_headers.rb +21 -0
- data/lib/action_controller/metal/etag_with_flash.rb +22 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +59 -0
- data/lib/action_controller/metal/exceptions.rb +106 -0
- data/lib/action_controller/metal/flash.rb +67 -0
- data/lib/action_controller/metal/head.rb +67 -0
- data/lib/action_controller/metal/helpers.rb +129 -0
- data/lib/action_controller/metal/http_authentication.rb +565 -0
- data/lib/action_controller/metal/implicit_render.rb +67 -0
- data/lib/action_controller/metal/instrumentation.rb +120 -0
- data/lib/action_controller/metal/live.rb +398 -0
- data/lib/action_controller/metal/logging.rb +22 -0
- data/lib/action_controller/metal/mime_responds.rb +337 -0
- data/lib/action_controller/metal/parameter_encoding.rb +84 -0
- data/lib/action_controller/metal/params_wrapper.rb +312 -0
- data/lib/action_controller/metal/permissions_policy.rb +38 -0
- data/lib/action_controller/metal/rate_limiting.rb +62 -0
- data/lib/action_controller/metal/redirecting.rb +251 -0
- data/lib/action_controller/metal/renderers.rb +181 -0
- data/lib/action_controller/metal/rendering.rb +260 -0
- data/lib/action_controller/metal/request_forgery_protection.rb +667 -0
- data/lib/action_controller/metal/rescue.rb +33 -0
- data/lib/action_controller/metal/streaming.rb +183 -0
- data/lib/action_controller/metal/strong_parameters.rb +1546 -0
- data/lib/action_controller/metal/testing.rb +25 -0
- data/lib/action_controller/metal/url_for.rb +65 -0
- data/lib/action_controller/metal.rb +339 -0
- data/lib/action_controller/railtie.rb +149 -0
- data/lib/action_controller/railties/helpers.rb +26 -0
- data/lib/action_controller/renderer.rb +161 -0
- data/lib/action_controller/template_assertions.rb +13 -0
- data/lib/action_controller/test_case.rb +691 -0
- data/lib/action_controller.rb +80 -0
- data/lib/action_dispatch/constants.rb +34 -0
- data/lib/action_dispatch/deprecator.rb +9 -0
- data/lib/action_dispatch/http/cache.rb +249 -0
- data/lib/action_dispatch/http/content_disposition.rb +47 -0
- data/lib/action_dispatch/http/content_security_policy.rb +365 -0
- data/lib/action_dispatch/http/filter_parameters.rb +80 -0
- data/lib/action_dispatch/http/filter_redirect.rb +50 -0
- data/lib/action_dispatch/http/headers.rb +134 -0
- data/lib/action_dispatch/http/mime_negotiation.rb +187 -0
- data/lib/action_dispatch/http/mime_type.rb +389 -0
- data/lib/action_dispatch/http/mime_types.rb +54 -0
- data/lib/action_dispatch/http/parameters.rb +119 -0
- data/lib/action_dispatch/http/permissions_policy.rb +189 -0
- data/lib/action_dispatch/http/rack_cache.rb +67 -0
- data/lib/action_dispatch/http/request.rb +498 -0
- data/lib/action_dispatch/http/response.rb +556 -0
- data/lib/action_dispatch/http/upload.rb +107 -0
- data/lib/action_dispatch/http/url.rb +344 -0
- data/lib/action_dispatch/journey/formatter.rb +226 -0
- data/lib/action_dispatch/journey/gtg/builder.rb +149 -0
- data/lib/action_dispatch/journey/gtg/simulator.rb +50 -0
- data/lib/action_dispatch/journey/gtg/transition_table.rb +217 -0
- data/lib/action_dispatch/journey/nfa/dot.rb +27 -0
- data/lib/action_dispatch/journey/nodes/node.rb +208 -0
- data/lib/action_dispatch/journey/parser.rb +103 -0
- data/lib/action_dispatch/journey/path/pattern.rb +209 -0
- data/lib/action_dispatch/journey/route.rb +189 -0
- data/lib/action_dispatch/journey/router/utils.rb +105 -0
- data/lib/action_dispatch/journey/router.rb +151 -0
- data/lib/action_dispatch/journey/routes.rb +82 -0
- data/lib/action_dispatch/journey/scanner.rb +70 -0
- data/lib/action_dispatch/journey/visitors.rb +267 -0
- data/lib/action_dispatch/journey/visualizer/fsm.css +30 -0
- data/lib/action_dispatch/journey/visualizer/fsm.js +159 -0
- data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
- data/lib/action_dispatch/journey.rb +7 -0
- data/lib/action_dispatch/log_subscriber.rb +25 -0
- data/lib/action_dispatch/middleware/actionable_exceptions.rb +46 -0
- data/lib/action_dispatch/middleware/assume_ssl.rb +27 -0
- data/lib/action_dispatch/middleware/callbacks.rb +38 -0
- data/lib/action_dispatch/middleware/cookies.rb +719 -0
- data/lib/action_dispatch/middleware/debug_exceptions.rb +206 -0
- data/lib/action_dispatch/middleware/debug_locks.rb +129 -0
- data/lib/action_dispatch/middleware/debug_view.rb +73 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +350 -0
- data/lib/action_dispatch/middleware/executor.rb +32 -0
- data/lib/action_dispatch/middleware/flash.rb +318 -0
- data/lib/action_dispatch/middleware/host_authorization.rb +171 -0
- data/lib/action_dispatch/middleware/public_exceptions.rb +64 -0
- data/lib/action_dispatch/middleware/reloader.rb +16 -0
- data/lib/action_dispatch/middleware/remote_ip.rb +199 -0
- data/lib/action_dispatch/middleware/request_id.rb +50 -0
- data/lib/action_dispatch/middleware/server_timing.rb +78 -0
- data/lib/action_dispatch/middleware/session/abstract_store.rb +112 -0
- data/lib/action_dispatch/middleware/session/cache_store.rb +66 -0
- data/lib/action_dispatch/middleware/session/cookie_store.rb +129 -0
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +34 -0
- data/lib/action_dispatch/middleware/show_exceptions.rb +88 -0
- data/lib/action_dispatch/middleware/ssl.rb +180 -0
- data/lib/action_dispatch/middleware/stack.rb +194 -0
- data/lib/action_dispatch/middleware/static.rb +192 -0
- data/lib/action_dispatch/middleware/templates/rescues/_actions.html.erb +13 -0
- data/lib/action_dispatch/middleware/templates/rescues/_actions.text.erb +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_message_and_suggestions.html.erb +22 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +17 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +36 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +62 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +12 -0
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +35 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +24 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +16 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +284 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +23 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +32 -0
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +6 -0
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +19 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +232 -0
- data/lib/action_dispatch/railtie.rb +77 -0
- data/lib/action_dispatch/request/session.rb +283 -0
- data/lib/action_dispatch/request/utils.rb +109 -0
- data/lib/action_dispatch/routing/endpoint.rb +19 -0
- data/lib/action_dispatch/routing/inspector.rb +323 -0
- data/lib/action_dispatch/routing/mapper.rb +2372 -0
- data/lib/action_dispatch/routing/polymorphic_routes.rb +363 -0
- data/lib/action_dispatch/routing/redirection.rb +218 -0
- data/lib/action_dispatch/routing/route_set.rb +958 -0
- data/lib/action_dispatch/routing/routes_proxy.rb +66 -0
- data/lib/action_dispatch/routing/url_for.rb +244 -0
- data/lib/action_dispatch/routing.rb +262 -0
- data/lib/action_dispatch/system_test_case.rb +206 -0
- data/lib/action_dispatch/system_testing/browser.rb +75 -0
- data/lib/action_dispatch/system_testing/driver.rb +85 -0
- data/lib/action_dispatch/system_testing/server.rb +33 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +164 -0
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +23 -0
- data/lib/action_dispatch/testing/assertion_response.rb +48 -0
- data/lib/action_dispatch/testing/assertions/response.rb +114 -0
- data/lib/action_dispatch/testing/assertions/routing.rb +343 -0
- data/lib/action_dispatch/testing/assertions.rb +25 -0
- data/lib/action_dispatch/testing/integration.rb +694 -0
- data/lib/action_dispatch/testing/request_encoder.rb +60 -0
- data/lib/action_dispatch/testing/test_helpers/page_dump_helper.rb +35 -0
- data/lib/action_dispatch/testing/test_process.rb +57 -0
- data/lib/action_dispatch/testing/test_request.rb +73 -0
- data/lib/action_dispatch/testing/test_response.rb +58 -0
- data/lib/action_dispatch.rb +147 -0
- data/lib/action_pack/gem_version.rb +19 -0
- data/lib/action_pack/version.rb +12 -0
- data/lib/action_pack.rb +27 -0
- metadata +375 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
5
|
+
require "active_support/core_ext/array/extract_options"
|
|
6
|
+
|
|
7
|
+
module ActionDispatch
|
|
8
|
+
module Routing
|
|
9
|
+
class RoutesProxy # :nodoc:
|
|
10
|
+
include ActionDispatch::Routing::UrlFor
|
|
11
|
+
|
|
12
|
+
attr_accessor :scope, :routes
|
|
13
|
+
alias :_routes :routes
|
|
14
|
+
|
|
15
|
+
def initialize(routes, scope, helpers, script_namer = nil)
|
|
16
|
+
@routes, @scope = routes, scope
|
|
17
|
+
@helpers = helpers
|
|
18
|
+
@script_namer = script_namer
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def url_options
|
|
22
|
+
scope.send(:_with_routes, routes) do
|
|
23
|
+
scope.url_options
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
def respond_to_missing?(method, _)
|
|
29
|
+
super || @helpers.respond_to?(method)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def method_missing(method, *args)
|
|
33
|
+
if @helpers.respond_to?(method)
|
|
34
|
+
options = args.extract_options!
|
|
35
|
+
options = url_options.merge((options || {}).symbolize_keys)
|
|
36
|
+
|
|
37
|
+
if @script_namer
|
|
38
|
+
options[:script_name] = merge_script_names(
|
|
39
|
+
options[:script_name],
|
|
40
|
+
@script_namer.call(options)
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
args << options
|
|
45
|
+
@helpers.public_send(method, *args)
|
|
46
|
+
else
|
|
47
|
+
super
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Keeps the part of the script name provided by the global context via
|
|
52
|
+
# [ENV]("SCRIPT_NAME"), which `mount` doesn't know about since it depends on the
|
|
53
|
+
# specific request, but use our script name resolver for the mount point
|
|
54
|
+
# dependent part.
|
|
55
|
+
def merge_script_names(previous_script_name, new_script_name)
|
|
56
|
+
return new_script_name unless previous_script_name
|
|
57
|
+
|
|
58
|
+
resolved_parts = new_script_name.count("/")
|
|
59
|
+
previous_parts = previous_script_name.count("/")
|
|
60
|
+
context_parts = previous_parts - resolved_parts + 1
|
|
61
|
+
|
|
62
|
+
(previous_script_name.split("/").slice(0, context_parts).join("/")) + new_script_name
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
5
|
+
module ActionDispatch
|
|
6
|
+
module Routing
|
|
7
|
+
# # Action Dispatch Routing UrlFor
|
|
8
|
+
#
|
|
9
|
+
# In `config/routes.rb` you define URL-to-controller mappings, but the reverse
|
|
10
|
+
# is also possible: a URL can be generated from one of your routing definitions.
|
|
11
|
+
# URL generation functionality is centralized in this module.
|
|
12
|
+
#
|
|
13
|
+
# See ActionDispatch::Routing for general information about routing and
|
|
14
|
+
# `config/routes.rb`.
|
|
15
|
+
#
|
|
16
|
+
# **Tip:** If you need to generate URLs from your models or some other place,
|
|
17
|
+
# then ActionDispatch::Routing::UrlFor is what you're looking for. Read on for
|
|
18
|
+
# an introduction. In general, this module should not be included on its own, as
|
|
19
|
+
# it is usually included by `url_helpers` (as in
|
|
20
|
+
# `Rails.application.routes.url_helpers`).
|
|
21
|
+
#
|
|
22
|
+
# ## URL generation from parameters
|
|
23
|
+
#
|
|
24
|
+
# As you may know, some functions, such as `ActionController::Base#url_for` and
|
|
25
|
+
# ActionView::Helpers::UrlHelper#link_to, can generate URLs given a set of
|
|
26
|
+
# parameters. For example, you've probably had the chance to write code like
|
|
27
|
+
# this in one of your views:
|
|
28
|
+
#
|
|
29
|
+
# <%= link_to('Click here', controller: 'users',
|
|
30
|
+
# action: 'new', message: 'Welcome!') %>
|
|
31
|
+
# # => <a href="/users/new?message=Welcome%21">Click here</a>
|
|
32
|
+
#
|
|
33
|
+
# `link_to`, and all other functions that require URL generation functionality,
|
|
34
|
+
# actually use ActionDispatch::Routing::UrlFor under the hood. And in
|
|
35
|
+
# particular, they use the ActionDispatch::Routing::UrlFor#url_for method. One
|
|
36
|
+
# can generate the same path as the above example by using the following code:
|
|
37
|
+
#
|
|
38
|
+
# include ActionDispatch::Routing::UrlFor
|
|
39
|
+
# url_for(controller: 'users',
|
|
40
|
+
# action: 'new',
|
|
41
|
+
# message: 'Welcome!',
|
|
42
|
+
# only_path: true)
|
|
43
|
+
# # => "/users/new?message=Welcome%21"
|
|
44
|
+
#
|
|
45
|
+
# Notice the `only_path: true` part. This is because UrlFor has no information
|
|
46
|
+
# about the website hostname that your Rails app is serving. So if you want to
|
|
47
|
+
# include the hostname as well, then you must also pass the `:host` argument:
|
|
48
|
+
#
|
|
49
|
+
# include UrlFor
|
|
50
|
+
# url_for(controller: 'users',
|
|
51
|
+
# action: 'new',
|
|
52
|
+
# message: 'Welcome!',
|
|
53
|
+
# host: 'www.example.com')
|
|
54
|
+
# # => "http://www.example.com/users/new?message=Welcome%21"
|
|
55
|
+
#
|
|
56
|
+
# By default, all controllers and views have access to a special version of
|
|
57
|
+
# `url_for`, that already knows what the current hostname is. So if you use
|
|
58
|
+
# `url_for` in your controllers or your views, then you don't need to explicitly
|
|
59
|
+
# pass the `:host` argument.
|
|
60
|
+
#
|
|
61
|
+
# For convenience, mailers also include ActionDispatch::Routing::UrlFor. So
|
|
62
|
+
# within mailers, you can use url_for. However, mailers cannot access incoming
|
|
63
|
+
# web requests in order to derive hostname information, so you have to provide
|
|
64
|
+
# the `:host` option or set the default host using `default_url_options`. For
|
|
65
|
+
# more information on url_for in mailers see the ActionMailer::Base
|
|
66
|
+
# documentation.
|
|
67
|
+
#
|
|
68
|
+
# ## URL generation for named routes
|
|
69
|
+
#
|
|
70
|
+
# UrlFor also allows one to access methods that have been auto-generated from
|
|
71
|
+
# named routes. For example, suppose that you have a 'users' resource in your
|
|
72
|
+
# `config/routes.rb`:
|
|
73
|
+
#
|
|
74
|
+
# resources :users
|
|
75
|
+
#
|
|
76
|
+
# This generates, among other things, the method `users_path`. By default, this
|
|
77
|
+
# method is accessible from your controllers, views, and mailers. If you need to
|
|
78
|
+
# access this auto-generated method from other places (such as a model), then
|
|
79
|
+
# you can do that by including `Rails.application.routes.url_helpers` in your
|
|
80
|
+
# class:
|
|
81
|
+
#
|
|
82
|
+
# class User < ActiveRecord::Base
|
|
83
|
+
# include Rails.application.routes.url_helpers
|
|
84
|
+
#
|
|
85
|
+
# def base_uri
|
|
86
|
+
# user_path(self)
|
|
87
|
+
# end
|
|
88
|
+
# end
|
|
89
|
+
#
|
|
90
|
+
# User.find(1).base_uri # => "/users/1"
|
|
91
|
+
#
|
|
92
|
+
module UrlFor
|
|
93
|
+
extend ActiveSupport::Concern
|
|
94
|
+
include PolymorphicRoutes
|
|
95
|
+
|
|
96
|
+
included do
|
|
97
|
+
unless method_defined?(:default_url_options)
|
|
98
|
+
# Including in a class uses an inheritable hash. Modules get a plain hash.
|
|
99
|
+
if respond_to?(:class_attribute)
|
|
100
|
+
class_attribute :default_url_options
|
|
101
|
+
else
|
|
102
|
+
mattr_writer :default_url_options
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
self.default_url_options = {}
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
include(*_url_for_modules) if respond_to?(:_url_for_modules)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def initialize(...)
|
|
112
|
+
@_routes = nil
|
|
113
|
+
super
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Hook overridden in controller to add request information with
|
|
117
|
+
# `default_url_options`. Application logic should not go into url_options.
|
|
118
|
+
def url_options
|
|
119
|
+
default_url_options
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Generate a URL based on the options provided, `default_url_options`, and the
|
|
123
|
+
# routes defined in `config/routes.rb`. The following options are supported:
|
|
124
|
+
#
|
|
125
|
+
# * `:only_path` - If true, the relative URL is returned. Defaults to `false`.
|
|
126
|
+
# * `:protocol` - The protocol to connect to. Defaults to `"http"`.
|
|
127
|
+
# * `:host` - Specifies the host the link should be targeted at. If
|
|
128
|
+
# `:only_path` is false, this option must be provided either explicitly, or
|
|
129
|
+
# via `default_url_options`.
|
|
130
|
+
# * `:subdomain` - Specifies the subdomain of the link, using the `tld_length`
|
|
131
|
+
# to split the subdomain from the host. If false, removes all subdomains
|
|
132
|
+
# from the host part of the link.
|
|
133
|
+
# * `:domain` - Specifies the domain of the link, using the `tld_length` to
|
|
134
|
+
# split the domain from the host.
|
|
135
|
+
# * `:tld_length` - Number of labels the TLD id composed of, only used if
|
|
136
|
+
# `:subdomain` or `:domain` are supplied. Defaults to
|
|
137
|
+
# `ActionDispatch::Http::URL.tld_length`, which in turn defaults to 1.
|
|
138
|
+
# * `:port` - Optionally specify the port to connect to.
|
|
139
|
+
# * `:anchor` - An anchor name to be appended to the path.
|
|
140
|
+
# * `:params` - The query parameters to be appended to the path.
|
|
141
|
+
# * `:path_params` - The query parameters that will only be used for the named
|
|
142
|
+
# dynamic segments of path. If unused, they will be discarded.
|
|
143
|
+
# * `:trailing_slash` - If true, adds a trailing slash, as in
|
|
144
|
+
# `"/archive/2009/"`.
|
|
145
|
+
# * `:script_name` - Specifies application path relative to domain root. If
|
|
146
|
+
# provided, prepends application path.
|
|
147
|
+
#
|
|
148
|
+
#
|
|
149
|
+
# Any other key (`:controller`, `:action`, etc.) given to `url_for` is forwarded
|
|
150
|
+
# to the Routes module.
|
|
151
|
+
#
|
|
152
|
+
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', port: '8080'
|
|
153
|
+
# # => 'http://somehost.org:8080/tasks/testing'
|
|
154
|
+
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', anchor: 'ok', only_path: true
|
|
155
|
+
# # => '/tasks/testing#ok'
|
|
156
|
+
# url_for controller: 'tasks', action: 'testing', trailing_slash: true
|
|
157
|
+
# # => 'http://somehost.org/tasks/testing/'
|
|
158
|
+
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', number: '33'
|
|
159
|
+
# # => 'http://somehost.org/tasks/testing?number=33'
|
|
160
|
+
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp"
|
|
161
|
+
# # => 'http://somehost.org/myapp/tasks/testing'
|
|
162
|
+
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true
|
|
163
|
+
# # => '/myapp/tasks/testing'
|
|
164
|
+
#
|
|
165
|
+
# Missing routes keys may be filled in from the current request's parameters
|
|
166
|
+
# (e.g. `:controller`, `:action`, `:id`, and any other parameters that are
|
|
167
|
+
# placed in the path). Given that the current action has been reached through
|
|
168
|
+
# `GET /users/1`:
|
|
169
|
+
#
|
|
170
|
+
# url_for(only_path: true) # => '/users/1'
|
|
171
|
+
# url_for(only_path: true, action: 'edit') # => '/users/1/edit'
|
|
172
|
+
# url_for(only_path: true, action: 'edit', id: 2) # => '/users/2/edit'
|
|
173
|
+
#
|
|
174
|
+
# Notice that no `:id` parameter was provided to the first `url_for` call and
|
|
175
|
+
# the helper used the one from the route's path. Any path parameter implicitly
|
|
176
|
+
# used by `url_for` can always be overwritten like shown on the last `url_for`
|
|
177
|
+
# calls.
|
|
178
|
+
def url_for(options = nil)
|
|
179
|
+
full_url_for(options)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def full_url_for(options = nil) # :nodoc:
|
|
183
|
+
case options
|
|
184
|
+
when nil
|
|
185
|
+
_routes.url_for(url_options.symbolize_keys)
|
|
186
|
+
when Hash, ActionController::Parameters
|
|
187
|
+
route_name = options.delete :use_route
|
|
188
|
+
merged_url_options = options.to_h.symbolize_keys.reverse_merge!(url_options)
|
|
189
|
+
_routes.url_for(merged_url_options, route_name)
|
|
190
|
+
when String
|
|
191
|
+
options
|
|
192
|
+
when Symbol
|
|
193
|
+
HelperMethodBuilder.url.handle_string_call self, options
|
|
194
|
+
when Array
|
|
195
|
+
components = options.dup
|
|
196
|
+
polymorphic_url(components, components.extract_options!)
|
|
197
|
+
when Class
|
|
198
|
+
HelperMethodBuilder.url.handle_class_call self, options
|
|
199
|
+
else
|
|
200
|
+
HelperMethodBuilder.url.handle_model_call self, options
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Allows calling direct or regular named route.
|
|
205
|
+
#
|
|
206
|
+
# resources :buckets
|
|
207
|
+
#
|
|
208
|
+
# direct :recordable do |recording|
|
|
209
|
+
# route_for(:bucket, recording.bucket)
|
|
210
|
+
# end
|
|
211
|
+
#
|
|
212
|
+
# direct :threadable do |threadable|
|
|
213
|
+
# route_for(:recordable, threadable.parent)
|
|
214
|
+
# end
|
|
215
|
+
#
|
|
216
|
+
# This maintains the context of the original caller on whether to return a path
|
|
217
|
+
# or full URL, e.g:
|
|
218
|
+
#
|
|
219
|
+
# threadable_path(threadable) # => "/buckets/1"
|
|
220
|
+
# threadable_url(threadable) # => "http://example.com/buckets/1"
|
|
221
|
+
#
|
|
222
|
+
def route_for(name, *args)
|
|
223
|
+
public_send(:"#{name}_url", *args)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
protected
|
|
227
|
+
def optimize_routes_generation?
|
|
228
|
+
_routes.optimize_routes_generation? && default_url_options.empty?
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
private
|
|
232
|
+
def _with_routes(routes) # :doc:
|
|
233
|
+
old_routes, @_routes = @_routes, routes
|
|
234
|
+
yield
|
|
235
|
+
ensure
|
|
236
|
+
@_routes = old_routes
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def _routes_context # :doc:
|
|
240
|
+
self
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# :markup: markdown
|
|
4
|
+
|
|
5
|
+
module ActionDispatch
|
|
6
|
+
# The routing module provides URL rewriting in native Ruby. It's a way to
|
|
7
|
+
# redirect incoming requests to controllers and actions. This replaces
|
|
8
|
+
# mod_rewrite rules. Best of all, Rails' Routing works with any web server.
|
|
9
|
+
# Routes are defined in `config/routes.rb`.
|
|
10
|
+
#
|
|
11
|
+
# Think of creating routes as drawing a map for your requests. The map tells
|
|
12
|
+
# them where to go based on some predefined pattern:
|
|
13
|
+
#
|
|
14
|
+
# Rails.application.routes.draw do
|
|
15
|
+
# Pattern 1 tells some request to go to one place
|
|
16
|
+
# Pattern 2 tell them to go to another
|
|
17
|
+
# ...
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# The following symbols are special:
|
|
21
|
+
#
|
|
22
|
+
# :controller maps to your controller name
|
|
23
|
+
# :action maps to an action with your controllers
|
|
24
|
+
#
|
|
25
|
+
# Other names simply map to a parameter as in the case of `:id`.
|
|
26
|
+
#
|
|
27
|
+
# ## Resources
|
|
28
|
+
#
|
|
29
|
+
# Resource routing allows you to quickly declare all of the common routes for a
|
|
30
|
+
# given resourceful controller. Instead of declaring separate routes for your
|
|
31
|
+
# `index`, `show`, `new`, `edit`, `create`, `update`, and `destroy` actions, a
|
|
32
|
+
# resourceful route declares them in a single line of code:
|
|
33
|
+
#
|
|
34
|
+
# resources :photos
|
|
35
|
+
#
|
|
36
|
+
# Sometimes, you have a resource that clients always look up without referencing
|
|
37
|
+
# an ID. A common example, /profile always shows the profile of the currently
|
|
38
|
+
# logged in user. In this case, you can use a singular resource to map /profile
|
|
39
|
+
# (rather than /profile/:id) to the show action.
|
|
40
|
+
#
|
|
41
|
+
# resource :profile
|
|
42
|
+
#
|
|
43
|
+
# It's common to have resources that are logically children of other resources:
|
|
44
|
+
#
|
|
45
|
+
# resources :magazines do
|
|
46
|
+
# resources :ads
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
# You may wish to organize groups of controllers under a namespace. Most
|
|
50
|
+
# commonly, you might group a number of administrative controllers under an
|
|
51
|
+
# `admin` namespace. You would place these controllers under the
|
|
52
|
+
# `app/controllers/admin` directory, and you can group them together in your
|
|
53
|
+
# router:
|
|
54
|
+
#
|
|
55
|
+
# namespace "admin" do
|
|
56
|
+
# resources :posts, :comments
|
|
57
|
+
# end
|
|
58
|
+
#
|
|
59
|
+
# Alternatively, you can add prefixes to your path without using a separate
|
|
60
|
+
# directory by using `scope`. `scope` takes additional options which apply to
|
|
61
|
+
# all enclosed routes.
|
|
62
|
+
#
|
|
63
|
+
# scope path: "/cpanel", as: 'admin' do
|
|
64
|
+
# resources :posts, :comments
|
|
65
|
+
# end
|
|
66
|
+
#
|
|
67
|
+
# For more, see Routing::Mapper::Resources#resources,
|
|
68
|
+
# Routing::Mapper::Scoping#namespace, and Routing::Mapper::Scoping#scope.
|
|
69
|
+
#
|
|
70
|
+
# ## Non-resourceful routes
|
|
71
|
+
#
|
|
72
|
+
# For routes that don't fit the `resources` mold, you can use the HTTP helper
|
|
73
|
+
# methods `get`, `post`, `patch`, `put` and `delete`.
|
|
74
|
+
#
|
|
75
|
+
# get 'post/:id', to: 'posts#show'
|
|
76
|
+
# post 'post/:id', to: 'posts#create_comment'
|
|
77
|
+
#
|
|
78
|
+
# Now, if you POST to `/posts/:id`, it will route to the `create_comment`
|
|
79
|
+
# action. A GET on the same URL will route to the `show` action.
|
|
80
|
+
#
|
|
81
|
+
# If your route needs to respond to more than one HTTP method (or all methods)
|
|
82
|
+
# then using the `:via` option on `match` is preferable.
|
|
83
|
+
#
|
|
84
|
+
# match 'post/:id', to: 'posts#show', via: [:get, :post]
|
|
85
|
+
#
|
|
86
|
+
# ## Named routes
|
|
87
|
+
#
|
|
88
|
+
# Routes can be named by passing an `:as` option, allowing for easy reference
|
|
89
|
+
# within your source as `name_of_route_url` for the full URL and
|
|
90
|
+
# `name_of_route_path` for the URI path.
|
|
91
|
+
#
|
|
92
|
+
# Example:
|
|
93
|
+
#
|
|
94
|
+
# # In config/routes.rb
|
|
95
|
+
# get '/login', to: 'accounts#login', as: 'login'
|
|
96
|
+
#
|
|
97
|
+
# # With render, redirect_to, tests, etc.
|
|
98
|
+
# redirect_to login_url
|
|
99
|
+
#
|
|
100
|
+
# Arguments can be passed as well.
|
|
101
|
+
#
|
|
102
|
+
# redirect_to show_item_path(id: 25)
|
|
103
|
+
#
|
|
104
|
+
# Use `root` as a shorthand to name a route for the root path "/".
|
|
105
|
+
#
|
|
106
|
+
# # In config/routes.rb
|
|
107
|
+
# root to: 'blogs#index'
|
|
108
|
+
#
|
|
109
|
+
# # would recognize http://www.example.com/ as
|
|
110
|
+
# params = { controller: 'blogs', action: 'index' }
|
|
111
|
+
#
|
|
112
|
+
# # and provide these named routes
|
|
113
|
+
# root_url # => 'http://www.example.com/'
|
|
114
|
+
# root_path # => '/'
|
|
115
|
+
#
|
|
116
|
+
# Note: when using `controller`, the route is simply named after the method you
|
|
117
|
+
# call on the block parameter rather than map.
|
|
118
|
+
#
|
|
119
|
+
# # In config/routes.rb
|
|
120
|
+
# controller :blog do
|
|
121
|
+
# get 'blog/show' => :list
|
|
122
|
+
# get 'blog/delete' => :delete
|
|
123
|
+
# get 'blog/edit' => :edit
|
|
124
|
+
# end
|
|
125
|
+
#
|
|
126
|
+
# # provides named routes for show, delete, and edit
|
|
127
|
+
# link_to @article.title, blog_show_path(id: @article.id)
|
|
128
|
+
#
|
|
129
|
+
# ## Pretty URLs
|
|
130
|
+
#
|
|
131
|
+
# Routes can generate pretty URLs. For example:
|
|
132
|
+
#
|
|
133
|
+
# get '/articles/:year/:month/:day', to: 'articles#find_by_id', constraints: {
|
|
134
|
+
# year: /\d{4}/,
|
|
135
|
+
# month: /\d{1,2}/,
|
|
136
|
+
# day: /\d{1,2}/
|
|
137
|
+
# }
|
|
138
|
+
#
|
|
139
|
+
# Using the route above, the URL "http://localhost:3000/articles/2005/11/06"
|
|
140
|
+
# maps to
|
|
141
|
+
#
|
|
142
|
+
# params = {year: '2005', month: '11', day: '06'}
|
|
143
|
+
#
|
|
144
|
+
# ## Regular Expressions and parameters
|
|
145
|
+
# You can specify a regular expression to define a format for a parameter.
|
|
146
|
+
#
|
|
147
|
+
# controller 'geocode' do
|
|
148
|
+
# get 'geocode/:postalcode', to: :show, constraints: {
|
|
149
|
+
# postalcode: /\d{5}(-\d{4})?/
|
|
150
|
+
# }
|
|
151
|
+
# end
|
|
152
|
+
#
|
|
153
|
+
# Constraints can include the 'ignorecase' and 'extended syntax' regular
|
|
154
|
+
# expression modifiers:
|
|
155
|
+
#
|
|
156
|
+
# controller 'geocode' do
|
|
157
|
+
# get 'geocode/:postalcode', to: :show, constraints: {
|
|
158
|
+
# postalcode: /hx\d\d\s\d[a-z]{2}/i
|
|
159
|
+
# }
|
|
160
|
+
# end
|
|
161
|
+
#
|
|
162
|
+
# controller 'geocode' do
|
|
163
|
+
# get 'geocode/:postalcode', to: :show, constraints: {
|
|
164
|
+
# postalcode: /# Postalcode format
|
|
165
|
+
# \d{5} #Prefix
|
|
166
|
+
# (-\d{4})? #Suffix
|
|
167
|
+
# /x
|
|
168
|
+
# }
|
|
169
|
+
# end
|
|
170
|
+
#
|
|
171
|
+
# Using the multiline modifier will raise an `ArgumentError`. Encoding regular
|
|
172
|
+
# expression modifiers are silently ignored. The match will always use the
|
|
173
|
+
# default encoding or ASCII.
|
|
174
|
+
#
|
|
175
|
+
# ## External redirects
|
|
176
|
+
#
|
|
177
|
+
# You can redirect any path to another path using the redirect helper in your
|
|
178
|
+
# router:
|
|
179
|
+
#
|
|
180
|
+
# get "/stories", to: redirect("/posts")
|
|
181
|
+
#
|
|
182
|
+
# ## Unicode character routes
|
|
183
|
+
#
|
|
184
|
+
# You can specify unicode character routes in your router:
|
|
185
|
+
#
|
|
186
|
+
# get "こんにちは", to: "welcome#index"
|
|
187
|
+
#
|
|
188
|
+
# ## Routing to Rack Applications
|
|
189
|
+
#
|
|
190
|
+
# Instead of a String, like `posts#index`, which corresponds to the index action
|
|
191
|
+
# in the PostsController, you can specify any Rack application as the endpoint
|
|
192
|
+
# for a matcher:
|
|
193
|
+
#
|
|
194
|
+
# get "/application.js", to: Sprockets
|
|
195
|
+
#
|
|
196
|
+
# ## Reloading routes
|
|
197
|
+
#
|
|
198
|
+
# You can reload routes if you feel you must:
|
|
199
|
+
#
|
|
200
|
+
# Rails.application.reload_routes!
|
|
201
|
+
#
|
|
202
|
+
# This will clear all named routes and reload config/routes.rb if the file has
|
|
203
|
+
# been modified from last load. To absolutely force reloading, use `reload!`.
|
|
204
|
+
#
|
|
205
|
+
# ## Testing Routes
|
|
206
|
+
#
|
|
207
|
+
# The two main methods for testing your routes:
|
|
208
|
+
#
|
|
209
|
+
# ### `assert_routing`
|
|
210
|
+
#
|
|
211
|
+
# def test_movie_route_properly_splits
|
|
212
|
+
# opts = {controller: "plugin", action: "checkout", id: "2"}
|
|
213
|
+
# assert_routing "plugin/checkout/2", opts
|
|
214
|
+
# end
|
|
215
|
+
#
|
|
216
|
+
# `assert_routing` lets you test whether or not the route properly resolves into
|
|
217
|
+
# options.
|
|
218
|
+
#
|
|
219
|
+
# ### `assert_recognizes`
|
|
220
|
+
#
|
|
221
|
+
# def test_route_has_options
|
|
222
|
+
# opts = {controller: "plugin", action: "show", id: "12"}
|
|
223
|
+
# assert_recognizes opts, "/plugins/show/12"
|
|
224
|
+
# end
|
|
225
|
+
#
|
|
226
|
+
# Note the subtle difference between the two: `assert_routing` tests that a URL
|
|
227
|
+
# fits options while `assert_recognizes` tests that a URL breaks into parameters
|
|
228
|
+
# properly.
|
|
229
|
+
#
|
|
230
|
+
# In tests you can simply pass the URL or named route to `get` or `post`.
|
|
231
|
+
#
|
|
232
|
+
# def send_to_jail
|
|
233
|
+
# get '/jail'
|
|
234
|
+
# assert_response :success
|
|
235
|
+
# end
|
|
236
|
+
#
|
|
237
|
+
# def goes_to_login
|
|
238
|
+
# get login_url
|
|
239
|
+
# #...
|
|
240
|
+
# end
|
|
241
|
+
#
|
|
242
|
+
# ## View a list of all your routes
|
|
243
|
+
#
|
|
244
|
+
# $ bin/rails routes
|
|
245
|
+
#
|
|
246
|
+
# Target a specific controller with `-c`, or grep routes using `-g`. Useful in
|
|
247
|
+
# conjunction with `--expanded` which displays routes vertically.
|
|
248
|
+
module Routing
|
|
249
|
+
extend ActiveSupport::Autoload
|
|
250
|
+
|
|
251
|
+
autoload :Mapper
|
|
252
|
+
autoload :RouteSet
|
|
253
|
+
eager_autoload do
|
|
254
|
+
autoload :RoutesProxy
|
|
255
|
+
end
|
|
256
|
+
autoload :UrlFor
|
|
257
|
+
autoload :PolymorphicRoutes
|
|
258
|
+
|
|
259
|
+
SEPARATORS = %w( / . ? ) # :nodoc:
|
|
260
|
+
HTTP_METHODS = [:get, :head, :post, :patch, :put, :delete, :options] # :nodoc:
|
|
261
|
+
end
|
|
262
|
+
end
|