actionpack 4.2.10 → 7.2.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of actionpack might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +86 -600
- data/MIT-LICENSE +1 -1
- data/README.rdoc +13 -14
- data/lib/abstract_controller/asset_paths.rb +5 -1
- data/lib/abstract_controller/base.rb +166 -136
- data/lib/abstract_controller/caching/fragments.rb +149 -0
- data/lib/abstract_controller/caching.rb +68 -0
- data/lib/abstract_controller/callbacks.rb +126 -57
- data/lib/abstract_controller/collector.rb +13 -15
- data/lib/abstract_controller/deprecator.rb +9 -0
- data/lib/abstract_controller/error.rb +8 -0
- data/lib/abstract_controller/helpers.rb +181 -132
- data/lib/abstract_controller/logger.rb +5 -1
- data/lib/abstract_controller/railties/routes_helpers.rb +10 -3
- data/lib/abstract_controller/rendering.rb +56 -56
- data/lib/abstract_controller/translation.rb +29 -15
- data/lib/abstract_controller/url_for.rb +15 -11
- data/lib/abstract_controller.rb +21 -5
- data/lib/action_controller/api/api_rendering.rb +18 -0
- data/lib/action_controller/api.rb +154 -0
- data/lib/action_controller/base.rb +219 -155
- data/lib/action_controller/caching.rb +28 -68
- data/lib/action_controller/deprecator.rb +9 -0
- data/lib/action_controller/form_builder.rb +55 -0
- data/lib/action_controller/log_subscriber.rb +35 -22
- data/lib/action_controller/metal/allow_browser.rb +119 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +17 -0
- data/lib/action_controller/metal/conditional_get.rb +259 -122
- data/lib/action_controller/metal/content_security_policy.rb +86 -0
- data/lib/action_controller/metal/cookies.rb +9 -5
- data/lib/action_controller/metal/data_streaming.rb +87 -104
- 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 +35 -26
- data/lib/action_controller/metal/exceptions.rb +71 -24
- data/lib/action_controller/metal/flash.rb +26 -19
- data/lib/action_controller/metal/head.rb +45 -36
- data/lib/action_controller/metal/helpers.rb +80 -64
- data/lib/action_controller/metal/http_authentication.rb +297 -244
- data/lib/action_controller/metal/implicit_render.rb +57 -9
- data/lib/action_controller/metal/instrumentation.rb +76 -64
- data/lib/action_controller/metal/live.rb +238 -176
- data/lib/action_controller/metal/logging.rb +22 -0
- data/lib/action_controller/metal/mime_responds.rb +177 -166
- data/lib/action_controller/metal/parameter_encoding.rb +84 -0
- data/lib/action_controller/metal/params_wrapper.rb +145 -118
- 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 +203 -64
- data/lib/action_controller/metal/renderers.rb +108 -65
- data/lib/action_controller/metal/rendering.rb +216 -56
- data/lib/action_controller/metal/request_forgery_protection.rb +496 -163
- data/lib/action_controller/metal/rescue.rb +19 -21
- data/lib/action_controller/metal/streaming.rb +179 -138
- data/lib/action_controller/metal/strong_parameters.rb +1058 -382
- data/lib/action_controller/metal/testing.rb +11 -17
- data/lib/action_controller/metal/url_for.rb +37 -21
- data/lib/action_controller/metal.rb +236 -138
- data/lib/action_controller/railtie.rb +89 -11
- data/lib/action_controller/railties/helpers.rb +5 -1
- data/lib/action_controller/renderer.rb +161 -0
- data/lib/action_controller/template_assertions.rb +13 -0
- data/lib/action_controller/test_case.rb +425 -497
- data/lib/action_controller.rb +44 -22
- data/lib/action_dispatch/constants.rb +34 -0
- data/lib/action_dispatch/deprecator.rb +9 -0
- data/lib/action_dispatch/http/cache.rb +119 -63
- data/lib/action_dispatch/http/content_disposition.rb +47 -0
- data/lib/action_dispatch/http/content_security_policy.rb +364 -0
- data/lib/action_dispatch/http/filter_parameters.rb +36 -34
- data/lib/action_dispatch/http/filter_redirect.rb +24 -12
- data/lib/action_dispatch/http/headers.rb +66 -31
- data/lib/action_dispatch/http/mime_negotiation.rb +106 -75
- data/lib/action_dispatch/http/mime_type.rb +196 -136
- data/lib/action_dispatch/http/mime_types.rb +25 -7
- data/lib/action_dispatch/http/parameters.rb +97 -45
- data/lib/action_dispatch/http/permissions_policy.rb +187 -0
- data/lib/action_dispatch/http/rack_cache.rb +6 -0
- data/lib/action_dispatch/http/request.rb +299 -170
- data/lib/action_dispatch/http/response.rb +311 -160
- data/lib/action_dispatch/http/upload.rb +52 -23
- data/lib/action_dispatch/http/url.rb +201 -125
- data/lib/action_dispatch/journey/formatter.rb +110 -50
- data/lib/action_dispatch/journey/gtg/builder.rb +37 -50
- data/lib/action_dispatch/journey/gtg/simulator.rb +20 -17
- data/lib/action_dispatch/journey/gtg/transition_table.rb +96 -36
- data/lib/action_dispatch/journey/nfa/dot.rb +5 -14
- data/lib/action_dispatch/journey/nodes/node.rb +100 -20
- data/lib/action_dispatch/journey/parser.rb +19 -17
- data/lib/action_dispatch/journey/parser.y +4 -3
- data/lib/action_dispatch/journey/parser_extras.rb +14 -4
- data/lib/action_dispatch/journey/path/pattern.rb +79 -63
- data/lib/action_dispatch/journey/route.rb +108 -44
- data/lib/action_dispatch/journey/router/utils.rb +41 -29
- data/lib/action_dispatch/journey/router.rb +64 -57
- data/lib/action_dispatch/journey/routes.rb +23 -21
- data/lib/action_dispatch/journey/scanner.rb +28 -17
- data/lib/action_dispatch/journey/visitors.rb +100 -54
- data/lib/action_dispatch/journey/visualizer/fsm.js +49 -24
- data/lib/action_dispatch/journey/visualizer/index.html.erb +1 -1
- data/lib/action_dispatch/journey.rb +7 -5
- 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 +7 -6
- data/lib/action_dispatch/middleware/cookies.rb +471 -328
- data/lib/action_dispatch/middleware/debug_exceptions.rb +149 -66
- 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 +275 -73
- data/lib/action_dispatch/middleware/executor.rb +32 -0
- data/lib/action_dispatch/middleware/flash.rb +143 -101
- data/lib/action_dispatch/middleware/host_authorization.rb +171 -0
- data/lib/action_dispatch/middleware/public_exceptions.rb +36 -27
- data/lib/action_dispatch/middleware/reloader.rb +10 -92
- data/lib/action_dispatch/middleware/remote_ip.rb +133 -107
- data/lib/action_dispatch/middleware/request_id.rb +29 -15
- data/lib/action_dispatch/middleware/server_timing.rb +78 -0
- data/lib/action_dispatch/middleware/session/abstract_store.rb +49 -27
- data/lib/action_dispatch/middleware/session/cache_store.rb +33 -16
- data/lib/action_dispatch/middleware/session/cookie_store.rb +86 -80
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +15 -3
- data/lib/action_dispatch/middleware/show_exceptions.rb +66 -36
- data/lib/action_dispatch/middleware/ssl.rb +134 -36
- data/lib/action_dispatch/middleware/stack.rb +109 -44
- data/lib/action_dispatch/middleware/static.rb +159 -90
- 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 +7 -24
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +1 -1
- 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 +46 -36
- 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 +26 -7
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +3 -3
- 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 +139 -15
- 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 +6 -6
- data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +7 -7
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +9 -9
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +4 -4
- data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +1 -1
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +7 -4
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +125 -93
- data/lib/action_dispatch/railtie.rb +44 -16
- data/lib/action_dispatch/request/session.rb +159 -69
- data/lib/action_dispatch/request/utils.rb +97 -23
- data/lib/action_dispatch/routing/endpoint.rb +11 -2
- data/lib/action_dispatch/routing/inspector.rb +195 -106
- data/lib/action_dispatch/routing/mapper.rb +1338 -955
- data/lib/action_dispatch/routing/polymorphic_routes.rb +234 -201
- data/lib/action_dispatch/routing/redirection.rb +78 -51
- data/lib/action_dispatch/routing/route_set.rb +460 -374
- data/lib/action_dispatch/routing/routes_proxy.rb +36 -12
- data/lib/action_dispatch/routing/url_for.rb +172 -124
- data/lib/action_dispatch/routing.rb +159 -158
- data/lib/action_dispatch/system_test_case.rb +206 -0
- data/lib/action_dispatch/system_testing/browser.rb +84 -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 +71 -39
- data/lib/action_dispatch/testing/assertions/routing.rb +228 -103
- data/lib/action_dispatch/testing/assertions.rb +9 -6
- data/lib/action_dispatch/testing/integration.rb +486 -306
- 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 +35 -22
- data/lib/action_dispatch/testing/test_request.rb +29 -34
- data/lib/action_dispatch/testing/test_response.rb +48 -15
- data/lib/action_dispatch.rb +82 -40
- data/lib/action_pack/gem_version.rb +8 -4
- data/lib/action_pack/version.rb +6 -2
- data/lib/action_pack.rb +21 -18
- metadata +146 -56
- data/lib/action_controller/caching/fragments.rb +0 -103
- data/lib/action_controller/metal/force_ssl.rb +0 -97
- data/lib/action_controller/metal/hide_actions.rb +0 -40
- data/lib/action_controller/metal/rack_delegation.rb +0 -32
- data/lib/action_controller/middleware.rb +0 -39
- data/lib/action_controller/model_naming.rb +0 -12
- data/lib/action_dispatch/http/parameter_filter.rb +0 -72
- data/lib/action_dispatch/journey/backwards.rb +0 -5
- data/lib/action_dispatch/journey/nfa/builder.rb +0 -76
- data/lib/action_dispatch/journey/nfa/simulator.rb +0 -47
- data/lib/action_dispatch/journey/nfa/transition_table.rb +0 -163
- data/lib/action_dispatch/journey/router/strexp.rb +0 -27
- data/lib/action_dispatch/middleware/params_parser.rb +0 -60
- data/lib/action_dispatch/middleware/templates/rescues/_source.erb +0 -27
- data/lib/action_dispatch/testing/assertions/dom.rb +0 -3
- data/lib/action_dispatch/testing/assertions/selector.rb +0 -3
- data/lib/action_dispatch/testing/assertions/tag.rb +0 -3
@@ -1,197 +1,224 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :markup: markdown
|
4
|
+
|
5
|
+
require "action_view"
|
2
6
|
require "action_controller/log_subscriber"
|
3
7
|
require "action_controller/metal/params_wrapper"
|
4
8
|
|
5
9
|
module ActionController
|
6
|
-
# Action
|
7
|
-
# on request and then either it renders a template or redirects to another action. An action is defined as a public method
|
8
|
-
# on the controller, which will automatically be made accessible to the web-server through \Rails Routes.
|
10
|
+
# # Action Controller Base
|
9
11
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
+
# Action Controllers are the core of a web request in Rails. They are made up of
|
13
|
+
# one or more actions that are executed on request and then either it renders a
|
14
|
+
# template or redirects to another action. An action is defined as a public
|
15
|
+
# method on the controller, which will automatically be made accessible to the
|
16
|
+
# web-server through Rails Routes.
|
17
|
+
#
|
18
|
+
# By default, only the ApplicationController in a Rails application inherits
|
19
|
+
# from `ActionController::Base`. All other controllers inherit from
|
20
|
+
# ApplicationController. This gives you one class to configure things such as
|
12
21
|
# request forgery protection and filtering of sensitive request parameters.
|
13
22
|
#
|
14
23
|
# A sample controller could look like this:
|
15
24
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
25
|
+
# class PostsController < ApplicationController
|
26
|
+
# def index
|
27
|
+
# @posts = Post.all
|
28
|
+
# end
|
20
29
|
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
30
|
+
# def create
|
31
|
+
# @post = Post.create params[:post]
|
32
|
+
# redirect_to posts_path
|
33
|
+
# end
|
24
34
|
# end
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# Actions, by default, render a template in the <tt>app/views</tt> directory corresponding to the name of the controller and action
|
28
|
-
# after executing code in the action. For example, the +index+ action of the PostsController would render the
|
29
|
-
# template <tt>app/views/posts/index.html.erb</tt> by default after populating the <tt>@posts</tt> instance variable.
|
30
35
|
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
36
|
+
# Actions, by default, render a template in the `app/views` directory
|
37
|
+
# corresponding to the name of the controller and action after executing code in
|
38
|
+
# the action. For example, the `index` action of the PostsController would
|
39
|
+
# render the template `app/views/posts/index.html.erb` by default after
|
40
|
+
# populating the `@posts` instance variable.
|
34
41
|
#
|
35
|
-
#
|
36
|
-
#
|
42
|
+
# Unlike index, the create action will not render a template. After performing
|
43
|
+
# its main purpose (creating a new post), it initiates a redirect instead. This
|
44
|
+
# redirect works by returning an external `302 Moved` HTTP response that takes
|
45
|
+
# the user to the index action.
|
37
46
|
#
|
38
|
-
#
|
47
|
+
# These two methods represent the two basic action archetypes used in Action
|
48
|
+
# Controllers: Get-and-show and do-and-redirect. Most actions are variations on
|
49
|
+
# these themes.
|
39
50
|
#
|
40
|
-
#
|
41
|
-
# and action are called. The remaining request parameters, the session (if one is available), and the full request with
|
42
|
-
# all the HTTP headers are made available to the action through accessor methods. Then the action is performed.
|
51
|
+
# ## Requests
|
43
52
|
#
|
44
|
-
#
|
53
|
+
# For every request, the router determines the value of the `controller` and
|
54
|
+
# `action` keys. These determine which controller and action are called. The
|
55
|
+
# remaining request parameters, the session (if one is available), and the full
|
56
|
+
# request with all the HTTP headers are made available to the action through
|
57
|
+
# accessor methods. Then the action is performed.
|
45
58
|
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
# render plain: "This server hosted at #{location}"
|
49
|
-
# end
|
59
|
+
# The full request object is available via the request accessor and is primarily
|
60
|
+
# used to query for HTTP headers:
|
50
61
|
#
|
51
|
-
#
|
62
|
+
# def server_ip
|
63
|
+
# location = request.env["REMOTE_ADDR"]
|
64
|
+
# render plain: "This server hosted at #{location}"
|
65
|
+
# end
|
52
66
|
#
|
53
|
-
#
|
54
|
-
# which returns a hash. For example, an action that was performed through <tt>/posts?category=All&limit=5</tt> will include
|
55
|
-
# <tt>{ "category" => "All", "limit" => "5" }</tt> in params.
|
67
|
+
# ## Parameters
|
56
68
|
#
|
57
|
-
#
|
69
|
+
# All request parameters, whether they come from a query string in the URL or
|
70
|
+
# form data submitted through a POST request are available through the `params`
|
71
|
+
# method which returns a hash. For example, an action that was performed through
|
72
|
+
# `/posts?category=All&limit=5` will include `{ "category" => "All", "limit" =>
|
73
|
+
# "5" }` in `params`.
|
58
74
|
#
|
59
|
-
#
|
60
|
-
#
|
75
|
+
# It's also possible to construct multi-dimensional parameter hashes by
|
76
|
+
# specifying keys using brackets, such as:
|
61
77
|
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
# <tt>{ "post" => { "address" => { "street" => "hyacintvej" } } }</tt>. There's no limit to the depth of the nesting.
|
78
|
+
# <input type="text" name="post[name]" value="david">
|
79
|
+
# <input type="text" name="post[address]" value="hyacintvej">
|
65
80
|
#
|
66
|
-
#
|
81
|
+
# A request coming from a form holding these inputs will include `{ "post" => {
|
82
|
+
# "name" => "david", "address" => "hyacintvej" } }`. If the address input had
|
83
|
+
# been named `post[address][street]`, the `params` would have included `{ "post"
|
84
|
+
# => { "address" => { "street" => "hyacintvej" } } }`. There's no limit to the
|
85
|
+
# depth of the nesting.
|
67
86
|
#
|
68
|
-
# Sessions
|
69
|
-
# such as a Signup object constructed in a multi-paged process, or objects that don't change much and are needed all the time, such
|
70
|
-
# as a User object for a system that requires login. The session should not be used, however, as a cache for objects where it's likely
|
71
|
-
# they could be changed unknowingly. It's usually too much work to keep it all synchronized -- something databases already excel at.
|
87
|
+
# ## Sessions
|
72
88
|
#
|
73
|
-
#
|
89
|
+
# Sessions allow you to store objects in between requests. This is useful for
|
90
|
+
# objects that are not yet ready to be persisted, such as a Signup object
|
91
|
+
# constructed in a multi-paged process, or objects that don't change much and
|
92
|
+
# are needed all the time, such as a User object for a system that requires
|
93
|
+
# login. The session should not be used, however, as a cache for objects where
|
94
|
+
# it's likely they could be changed unknowingly. It's usually too much work to
|
95
|
+
# keep it all synchronized -- something databases already excel at.
|
74
96
|
#
|
75
|
-
#
|
97
|
+
# You can place objects in the session by using the `session` method, which
|
98
|
+
# accesses a hash:
|
76
99
|
#
|
77
|
-
#
|
100
|
+
# session[:person] = Person.authenticate(user_name, password)
|
78
101
|
#
|
79
|
-
#
|
102
|
+
# You can retrieve it again through the same hash:
|
80
103
|
#
|
81
|
-
#
|
104
|
+
# "Hello #{session[:person]}"
|
82
105
|
#
|
83
|
-
#
|
84
|
-
#
|
106
|
+
# For removing objects from the session, you can either assign a single key to
|
107
|
+
# `nil`:
|
85
108
|
#
|
86
|
-
#
|
109
|
+
# # removes :person from session
|
110
|
+
# session[:person] = nil
|
87
111
|
#
|
88
|
-
#
|
89
|
-
# This prevents the user from tampering with the session but also allows them to see its contents.
|
112
|
+
# or you can remove the entire session with `reset_session`.
|
90
113
|
#
|
91
|
-
#
|
114
|
+
# By default, sessions are stored in an encrypted browser cookie (see
|
115
|
+
# ActionDispatch::Session::CookieStore). Thus the user will not be able to read
|
116
|
+
# or edit the session data. However, the user can keep a copy of the cookie even
|
117
|
+
# after it has expired, so you should avoid storing sensitive information in
|
118
|
+
# cookie-based sessions.
|
92
119
|
#
|
93
|
-
#
|
120
|
+
# ## Responses
|
94
121
|
#
|
95
|
-
# Each action results in a response, which holds the headers and document to be
|
96
|
-
#
|
122
|
+
# Each action results in a response, which holds the headers and document to be
|
123
|
+
# sent to the user's browser. The actual response object is generated
|
124
|
+
# automatically through the use of renders and redirects and requires no user
|
125
|
+
# intervention.
|
97
126
|
#
|
98
|
-
#
|
127
|
+
# ## Renders
|
99
128
|
#
|
100
|
-
# Action Controller sends content to the user by using one of five rendering
|
101
|
-
#
|
102
|
-
#
|
129
|
+
# Action Controller sends content to the user by using one of five rendering
|
130
|
+
# methods. The most versatile and common is the rendering of a template.
|
131
|
+
# Included in the Action Pack is the Action View, which enables rendering of ERB
|
132
|
+
# templates. It's automatically configured. The controller passes objects to the
|
133
|
+
# view by assigning instance variables:
|
103
134
|
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
135
|
+
# def show
|
136
|
+
# @post = Post.find(params[:id])
|
137
|
+
# end
|
107
138
|
#
|
108
139
|
# Which are then automatically available to the view:
|
109
140
|
#
|
110
|
-
#
|
141
|
+
# Title: <%= @post.title %>
|
111
142
|
#
|
112
|
-
# You don't have to rely on the automated rendering. For example, actions that
|
113
|
-
# will use the manual
|
143
|
+
# You don't have to rely on the automated rendering. For example, actions that
|
144
|
+
# could result in the rendering of different templates will use the manual
|
145
|
+
# rendering methods:
|
114
146
|
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
147
|
+
# def search
|
148
|
+
# @results = Search.find(params[:query])
|
149
|
+
# case @results.count
|
150
|
+
# when 0 then render action: "no_results"
|
151
|
+
# when 1 then render action: "show"
|
152
|
+
# when 2..10 then render action: "show_many"
|
153
|
+
# end
|
121
154
|
# end
|
122
|
-
# end
|
123
155
|
#
|
124
156
|
# Read more about writing ERB and Builder templates in ActionView::Base.
|
125
157
|
#
|
126
|
-
#
|
158
|
+
# ## Redirects
|
127
159
|
#
|
128
|
-
# Redirects are used to move from one action to another. For example, after a
|
129
|
-
#
|
130
|
-
#
|
160
|
+
# Redirects are used to move from one action to another. For example, after a
|
161
|
+
# `create` action, which stores a blog entry to the database, we might like to
|
162
|
+
# show the user the new entry. Because we're following good DRY principles
|
163
|
+
# (Don't Repeat Yourself), we're going to reuse (and redirect to) a `show`
|
164
|
+
# action that we'll assume has already been created. The code might look like
|
165
|
+
# this:
|
131
166
|
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
167
|
+
# def create
|
168
|
+
# @entry = Entry.new(params[:entry])
|
169
|
+
# if @entry.save
|
170
|
+
# # The entry was saved correctly, redirect to show
|
171
|
+
# redirect_to action: 'show', id: @entry.id
|
172
|
+
# else
|
173
|
+
# # things didn't go so well, do something else
|
174
|
+
# end
|
139
175
|
# end
|
140
|
-
# end
|
141
176
|
#
|
142
|
-
# In this case, after saving our new entry to the database, the user is
|
143
|
-
#
|
144
|
-
#
|
177
|
+
# In this case, after saving our new entry to the database, the user is
|
178
|
+
# redirected to the `show` method, which is then executed. Note that this is an
|
179
|
+
# external HTTP-level redirection which will cause the browser to make a second
|
180
|
+
# request (a GET to the show action), and not some internal re-routing which
|
181
|
+
# calls both "create" and then "show" within one request.
|
145
182
|
#
|
146
|
-
# Learn more about
|
183
|
+
# Learn more about `redirect_to` and what options you have in
|
184
|
+
# ActionController::Redirecting.
|
147
185
|
#
|
148
|
-
#
|
186
|
+
# ## Calling multiple redirects or renders
|
149
187
|
#
|
150
|
-
# An action may
|
188
|
+
# An action may perform only a single render or a single redirect. Attempting to
|
189
|
+
# do either again will result in a DoubleRenderError:
|
151
190
|
#
|
152
|
-
#
|
153
|
-
#
|
154
|
-
#
|
155
|
-
#
|
191
|
+
# def do_something
|
192
|
+
# redirect_to action: "elsewhere"
|
193
|
+
# render action: "overthere" # raises DoubleRenderError
|
194
|
+
# end
|
156
195
|
#
|
157
|
-
# If you need to redirect on the condition of something, then be sure to add
|
196
|
+
# If you need to redirect on the condition of something, then be sure to add
|
197
|
+
# "return" to halt execution.
|
158
198
|
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
199
|
+
# def do_something
|
200
|
+
# if monkeys.nil?
|
201
|
+
# redirect_to(action: "elsewhere")
|
202
|
+
# return
|
203
|
+
# end
|
204
|
+
# render action: "overthere" # won't be called if monkeys is nil
|
205
|
+
# end
|
163
206
|
#
|
164
207
|
class Base < Metal
|
165
208
|
abstract!
|
166
209
|
|
167
|
-
# We document the request and response methods here because albeit they are
|
168
|
-
# implemented in ActionController::Metal, the type of the returned objects
|
169
|
-
# is unknown at that level.
|
170
|
-
|
171
|
-
##
|
172
|
-
# :method: request
|
173
|
-
#
|
174
|
-
# Returns an ActionDispatch::Request instance that represents the
|
175
|
-
# current request.
|
176
|
-
|
177
|
-
##
|
178
|
-
# :method: response
|
179
|
-
#
|
180
|
-
# Returns an ActionDispatch::Response that represents the current
|
181
|
-
# response.
|
182
|
-
|
183
210
|
# Shortcut helper that returns all the modules included in
|
184
211
|
# ActionController::Base except the ones passed as arguments:
|
185
212
|
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
213
|
+
# class MyBaseController < ActionController::Metal
|
214
|
+
# ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
|
215
|
+
# include left
|
216
|
+
# end
|
189
217
|
# end
|
190
|
-
# end
|
191
218
|
#
|
192
|
-
# This gives better control over what you want to exclude and makes it
|
193
|
-
#
|
194
|
-
#
|
219
|
+
# This gives better control over what you want to exclude and makes it easier to
|
220
|
+
# create a bare controller class, instead of listing the modules required
|
221
|
+
# manually.
|
195
222
|
def self.without_modules(*modules)
|
196
223
|
modules = modules.map do |m|
|
197
224
|
m.is_a?(Symbol) ? ActionController.const_get(m) : m
|
@@ -204,9 +231,7 @@ module ActionController
|
|
204
231
|
AbstractController::Rendering,
|
205
232
|
AbstractController::Translation,
|
206
233
|
AbstractController::AssetPaths,
|
207
|
-
|
208
234
|
Helpers,
|
209
|
-
HideActions,
|
210
235
|
UrlFor,
|
211
236
|
Redirecting,
|
212
237
|
ActionView::Layouts,
|
@@ -214,55 +239,94 @@ module ActionController
|
|
214
239
|
Renderers::All,
|
215
240
|
ConditionalGet,
|
216
241
|
EtagWithTemplateDigest,
|
217
|
-
|
242
|
+
EtagWithFlash,
|
218
243
|
Caching,
|
219
244
|
MimeResponds,
|
220
245
|
ImplicitRender,
|
221
246
|
StrongParameters,
|
222
|
-
|
247
|
+
ParameterEncoding,
|
223
248
|
Cookies,
|
224
249
|
Flash,
|
250
|
+
FormBuilder,
|
225
251
|
RequestForgeryProtection,
|
226
|
-
|
252
|
+
ContentSecurityPolicy,
|
253
|
+
PermissionsPolicy,
|
254
|
+
RateLimiting,
|
255
|
+
AllowBrowser,
|
227
256
|
Streaming,
|
228
257
|
DataStreaming,
|
229
258
|
HttpAuthentication::Basic::ControllerMethods,
|
230
259
|
HttpAuthentication::Digest::ControllerMethods,
|
231
260
|
HttpAuthentication::Token::ControllerMethods,
|
232
|
-
|
233
|
-
|
234
|
-
# also include them at the bottom.
|
261
|
+
DefaultHeaders,
|
262
|
+
Logging,
|
235
263
|
AbstractController::Callbacks,
|
236
|
-
|
237
|
-
# Append rescue at the bottom to wrap as much as possible.
|
238
264
|
Rescue,
|
239
|
-
|
240
|
-
# Add instrumentations hooks at the bottom, to ensure they instrument
|
241
|
-
# all the methods properly.
|
242
265
|
Instrumentation,
|
243
|
-
|
244
|
-
# Params wrapper should come before instrumentation so they are
|
245
|
-
# properly showed in logs
|
246
266
|
ParamsWrapper
|
247
267
|
]
|
248
268
|
|
249
|
-
|
250
|
-
|
251
|
-
|
269
|
+
# Note: Documenting these severely degrates the performance of rdoc
|
270
|
+
# :stopdoc:
|
271
|
+
include AbstractController::Rendering
|
272
|
+
include AbstractController::Translation
|
273
|
+
include AbstractController::AssetPaths
|
274
|
+
include Helpers
|
275
|
+
include UrlFor
|
276
|
+
include Redirecting
|
277
|
+
include ActionView::Layouts
|
278
|
+
include Rendering
|
279
|
+
include Renderers::All
|
280
|
+
include ConditionalGet
|
281
|
+
include EtagWithTemplateDigest
|
282
|
+
include EtagWithFlash
|
283
|
+
include Caching
|
284
|
+
include MimeResponds
|
285
|
+
include ImplicitRender
|
286
|
+
include StrongParameters
|
287
|
+
include ParameterEncoding
|
288
|
+
include Cookies
|
289
|
+
include Flash
|
290
|
+
include FormBuilder
|
291
|
+
include RequestForgeryProtection
|
292
|
+
include ContentSecurityPolicy
|
293
|
+
include PermissionsPolicy
|
294
|
+
include RateLimiting
|
295
|
+
include AllowBrowser
|
296
|
+
include Streaming
|
297
|
+
include DataStreaming
|
298
|
+
include HttpAuthentication::Basic::ControllerMethods
|
299
|
+
include HttpAuthentication::Digest::ControllerMethods
|
300
|
+
include HttpAuthentication::Token::ControllerMethods
|
301
|
+
include DefaultHeaders
|
302
|
+
include Logging
|
303
|
+
# Before callbacks should also be executed as early as possible, so also include
|
304
|
+
# them at the bottom.
|
305
|
+
include AbstractController::Callbacks
|
306
|
+
# Append rescue at the bottom to wrap as much as possible.
|
307
|
+
include Rescue
|
308
|
+
# Add instrumentations hooks at the bottom, to ensure they instrument all the
|
309
|
+
# methods properly.
|
310
|
+
include Instrumentation
|
311
|
+
# Params wrapper should come before instrumentation so they are properly showed
|
312
|
+
# in logs
|
313
|
+
include ParamsWrapper
|
314
|
+
# :startdoc:
|
315
|
+
setup_renderer!
|
252
316
|
|
253
317
|
# Define some internal variables that should not be propagated to the view.
|
254
|
-
PROTECTED_IVARS = AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES +
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
PROTECTED_IVARS
|
260
|
-
end
|
318
|
+
PROTECTED_IVARS = AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + %i(
|
319
|
+
@_params @_response @_request @_config @_url_options @_action_has_layout @_view_context_class
|
320
|
+
@_view_renderer @_lookup_context @_routes @_view_runtime @_db_runtime @_helper_proxy
|
321
|
+
@_marked_for_same_origin_verification @_rendered_format
|
322
|
+
)
|
261
323
|
|
262
|
-
def
|
324
|
+
def _protected_ivars
|
263
325
|
PROTECTED_IVARS
|
264
326
|
end
|
327
|
+
private :_protected_ivars
|
265
328
|
|
329
|
+
ActiveSupport.run_load_hooks(:action_controller_base, self)
|
266
330
|
ActiveSupport.run_load_hooks(:action_controller, self)
|
267
331
|
end
|
268
332
|
end
|
@@ -1,89 +1,49 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# :markup: markdown
|
4
4
|
|
5
5
|
module ActionController
|
6
|
-
#
|
7
|
-
#
|
6
|
+
# # Action Controller Caching
|
7
|
+
#
|
8
|
+
# Caching is a cheap way of speeding up slow applications by keeping the result
|
9
|
+
# of calculations, renderings, and database calls around for subsequent
|
10
|
+
# requests.
|
8
11
|
#
|
9
12
|
# You can read more about each approach by clicking the modules below.
|
10
13
|
#
|
11
|
-
# Note: To turn off all caching, set
|
12
|
-
#
|
14
|
+
# Note: To turn off all caching provided by Action Controller, set
|
15
|
+
# config.action_controller.perform_caching = false
|
13
16
|
#
|
14
|
-
#
|
17
|
+
# ## Caching stores
|
15
18
|
#
|
16
|
-
# All the caching stores from ActiveSupport::Cache are available to be used as
|
17
|
-
# for Action Controller caching.
|
19
|
+
# All the caching stores from ActiveSupport::Cache are available to be used as
|
20
|
+
# backends for Action Controller caching.
|
18
21
|
#
|
19
22
|
# Configuration examples (FileStore is the default):
|
20
23
|
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
24
|
+
# config.action_controller.cache_store = :memory_store
|
25
|
+
# config.action_controller.cache_store = :file_store, '/path/to/cache/directory'
|
26
|
+
# config.action_controller.cache_store = :mem_cache_store, 'localhost'
|
27
|
+
# config.action_controller.cache_store = :mem_cache_store, Memcached::Rails.new('localhost:11211')
|
28
|
+
# config.action_controller.cache_store = MyOwnStore.new('parameter')
|
26
29
|
module Caching
|
27
30
|
extend ActiveSupport::Concern
|
28
|
-
extend ActiveSupport::Autoload
|
29
|
-
|
30
|
-
eager_autoload do
|
31
|
-
autoload :Fragments
|
32
|
-
end
|
33
|
-
|
34
|
-
module ConfigMethods
|
35
|
-
def cache_store
|
36
|
-
config.cache_store
|
37
|
-
end
|
38
|
-
|
39
|
-
def cache_store=(store)
|
40
|
-
config.cache_store = ActiveSupport::Cache.lookup_store(store)
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
def cache_configured?
|
45
|
-
perform_caching && cache_store
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
include RackDelegation
|
50
|
-
include AbstractController::Callbacks
|
51
|
-
|
52
|
-
include ConfigMethods
|
53
|
-
include Fragments
|
54
31
|
|
55
32
|
included do
|
56
|
-
|
57
|
-
|
58
|
-
config_accessor :default_static_extension
|
59
|
-
self.default_static_extension ||= '.html'
|
60
|
-
|
61
|
-
config_accessor :perform_caching
|
62
|
-
self.perform_caching = true if perform_caching.nil?
|
63
|
-
|
64
|
-
class_attribute :_view_cache_dependencies
|
65
|
-
self._view_cache_dependencies = []
|
66
|
-
helper_method :view_cache_dependencies if respond_to?(:helper_method)
|
33
|
+
include AbstractController::Caching
|
67
34
|
end
|
68
35
|
|
69
|
-
|
70
|
-
def
|
71
|
-
|
36
|
+
private
|
37
|
+
def instrument_payload(key)
|
38
|
+
{
|
39
|
+
controller: controller_name,
|
40
|
+
action: action_name,
|
41
|
+
key: key
|
42
|
+
}
|
72
43
|
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def view_cache_dependencies
|
76
|
-
self.class._view_cache_dependencies.map { |dep| instance_exec(&dep) }.compact
|
77
|
-
end
|
78
44
|
|
79
|
-
|
80
|
-
|
81
|
-
def cache(key, options = {}, &block)
|
82
|
-
if cache_configured?
|
83
|
-
cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
|
84
|
-
else
|
85
|
-
yield
|
86
|
-
end
|
45
|
+
def instrument_name
|
46
|
+
"action_controller"
|
87
47
|
end
|
88
48
|
end
|
89
49
|
end
|