actionpack 6.0.0
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 +7 -0
- data/CHANGELOG.md +311 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +58 -0
- data/lib/abstract_controller.rb +27 -0
- data/lib/abstract_controller/asset_paths.rb +12 -0
- data/lib/abstract_controller/base.rb +267 -0
- data/lib/abstract_controller/caching.rb +66 -0
- data/lib/abstract_controller/caching/fragments.rb +150 -0
- data/lib/abstract_controller/callbacks.rb +224 -0
- data/lib/abstract_controller/collector.rb +43 -0
- data/lib/abstract_controller/error.rb +6 -0
- data/lib/abstract_controller/helpers.rb +194 -0
- data/lib/abstract_controller/logger.rb +14 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +20 -0
- data/lib/abstract_controller/rendering.rb +127 -0
- data/lib/abstract_controller/translation.rb +32 -0
- data/lib/abstract_controller/url_for.rb +35 -0
- data/lib/action_controller.rb +67 -0
- data/lib/action_controller/api.rb +150 -0
- data/lib/action_controller/api/api_rendering.rb +16 -0
- data/lib/action_controller/base.rb +271 -0
- data/lib/action_controller/caching.rb +46 -0
- data/lib/action_controller/form_builder.rb +50 -0
- data/lib/action_controller/log_subscriber.rb +81 -0
- data/lib/action_controller/metal.rb +256 -0
- data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
- data/lib/action_controller/metal/conditional_get.rb +280 -0
- data/lib/action_controller/metal/content_security_policy.rb +52 -0
- data/lib/action_controller/metal/cookies.rb +16 -0
- data/lib/action_controller/metal/data_streaming.rb +151 -0
- data/lib/action_controller/metal/default_headers.rb +17 -0
- data/lib/action_controller/metal/etag_with_flash.rb +18 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +57 -0
- data/lib/action_controller/metal/exceptions.rb +74 -0
- data/lib/action_controller/metal/flash.rb +61 -0
- data/lib/action_controller/metal/force_ssl.rb +58 -0
- data/lib/action_controller/metal/head.rb +60 -0
- data/lib/action_controller/metal/helpers.rb +122 -0
- data/lib/action_controller/metal/http_authentication.rb +518 -0
- data/lib/action_controller/metal/implicit_render.rb +63 -0
- data/lib/action_controller/metal/instrumentation.rb +105 -0
- data/lib/action_controller/metal/live.rb +314 -0
- data/lib/action_controller/metal/mime_responds.rb +324 -0
- data/lib/action_controller/metal/parameter_encoding.rb +51 -0
- data/lib/action_controller/metal/params_wrapper.rb +297 -0
- data/lib/action_controller/metal/redirecting.rb +133 -0
- data/lib/action_controller/metal/renderers.rb +181 -0
- data/lib/action_controller/metal/rendering.rb +122 -0
- data/lib/action_controller/metal/request_forgery_protection.rb +456 -0
- data/lib/action_controller/metal/rescue.rb +28 -0
- data/lib/action_controller/metal/streaming.rb +223 -0
- data/lib/action_controller/metal/strong_parameters.rb +1105 -0
- data/lib/action_controller/metal/testing.rb +16 -0
- data/lib/action_controller/metal/url_for.rb +58 -0
- data/lib/action_controller/railtie.rb +89 -0
- data/lib/action_controller/railties/helpers.rb +24 -0
- data/lib/action_controller/renderer.rb +130 -0
- data/lib/action_controller/template_assertions.rb +11 -0
- data/lib/action_controller/test_case.rb +626 -0
- data/lib/action_dispatch.rb +114 -0
- data/lib/action_dispatch/http/cache.rb +226 -0
- data/lib/action_dispatch/http/content_disposition.rb +45 -0
- data/lib/action_dispatch/http/content_security_policy.rb +284 -0
- data/lib/action_dispatch/http/filter_parameters.rb +86 -0
- data/lib/action_dispatch/http/filter_redirect.rb +37 -0
- data/lib/action_dispatch/http/headers.rb +132 -0
- data/lib/action_dispatch/http/mime_negotiation.rb +177 -0
- data/lib/action_dispatch/http/mime_type.rb +350 -0
- data/lib/action_dispatch/http/mime_types.rb +50 -0
- data/lib/action_dispatch/http/parameter_filter.rb +12 -0
- data/lib/action_dispatch/http/parameters.rb +136 -0
- data/lib/action_dispatch/http/rack_cache.rb +63 -0
- data/lib/action_dispatch/http/request.rb +427 -0
- data/lib/action_dispatch/http/response.rb +534 -0
- data/lib/action_dispatch/http/upload.rb +92 -0
- data/lib/action_dispatch/http/url.rb +350 -0
- data/lib/action_dispatch/journey.rb +7 -0
- data/lib/action_dispatch/journey/formatter.rb +189 -0
- data/lib/action_dispatch/journey/gtg/builder.rb +164 -0
- data/lib/action_dispatch/journey/gtg/simulator.rb +41 -0
- data/lib/action_dispatch/journey/gtg/transition_table.rb +158 -0
- data/lib/action_dispatch/journey/nfa/builder.rb +78 -0
- data/lib/action_dispatch/journey/nfa/dot.rb +36 -0
- data/lib/action_dispatch/journey/nfa/simulator.rb +47 -0
- data/lib/action_dispatch/journey/nfa/transition_table.rb +120 -0
- data/lib/action_dispatch/journey/nodes/node.rb +141 -0
- data/lib/action_dispatch/journey/parser.rb +199 -0
- data/lib/action_dispatch/journey/parser.y +50 -0
- data/lib/action_dispatch/journey/parser_extras.rb +31 -0
- data/lib/action_dispatch/journey/path/pattern.rb +203 -0
- data/lib/action_dispatch/journey/route.rb +204 -0
- data/lib/action_dispatch/journey/router.rb +153 -0
- data/lib/action_dispatch/journey/router/utils.rb +102 -0
- data/lib/action_dispatch/journey/routes.rb +81 -0
- data/lib/action_dispatch/journey/scanner.rb +71 -0
- data/lib/action_dispatch/journey/visitors.rb +268 -0
- data/lib/action_dispatch/journey/visualizer/fsm.css +30 -0
- data/lib/action_dispatch/journey/visualizer/fsm.js +134 -0
- data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
- data/lib/action_dispatch/middleware/actionable_exceptions.rb +39 -0
- data/lib/action_dispatch/middleware/callbacks.rb +34 -0
- data/lib/action_dispatch/middleware/cookies.rb +663 -0
- data/lib/action_dispatch/middleware/debug_exceptions.rb +185 -0
- data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
- data/lib/action_dispatch/middleware/debug_view.rb +68 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +181 -0
- data/lib/action_dispatch/middleware/executor.rb +21 -0
- data/lib/action_dispatch/middleware/flash.rb +300 -0
- data/lib/action_dispatch/middleware/host_authorization.rb +103 -0
- data/lib/action_dispatch/middleware/public_exceptions.rb +61 -0
- data/lib/action_dispatch/middleware/reloader.rb +12 -0
- data/lib/action_dispatch/middleware/remote_ip.rb +181 -0
- data/lib/action_dispatch/middleware/request_id.rb +43 -0
- data/lib/action_dispatch/middleware/session/abstract_store.rb +92 -0
- data/lib/action_dispatch/middleware/session/cache_store.rb +54 -0
- data/lib/action_dispatch/middleware/session/cookie_store.rb +113 -0
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +28 -0
- data/lib/action_dispatch/middleware/show_exceptions.rb +62 -0
- data/lib/action_dispatch/middleware/ssl.rb +150 -0
- data/lib/action_dispatch/middleware/stack.rb +148 -0
- data/lib/action_dispatch/middleware/static.rb +129 -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/_request_and_response.html.erb +24 -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 +29 -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 +7 -0
- data/lib/action_dispatch/middleware/templates/rescues/blocked_host.text.erb +5 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +38 -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 +15 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +165 -0
- data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +19 -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 +16 -0
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +203 -0
- data/lib/action_dispatch/railtie.rb +58 -0
- data/lib/action_dispatch/request/session.rb +242 -0
- data/lib/action_dispatch/request/utils.rb +78 -0
- data/lib/action_dispatch/routing.rb +261 -0
- data/lib/action_dispatch/routing/endpoint.rb +17 -0
- data/lib/action_dispatch/routing/inspector.rb +274 -0
- data/lib/action_dispatch/routing/mapper.rb +2289 -0
- data/lib/action_dispatch/routing/polymorphic_routes.rb +351 -0
- data/lib/action_dispatch/routing/redirection.rb +201 -0
- data/lib/action_dispatch/routing/route_set.rb +887 -0
- data/lib/action_dispatch/routing/routes_proxy.rb +69 -0
- data/lib/action_dispatch/routing/url_for.rb +237 -0
- data/lib/action_dispatch/system_test_case.rb +168 -0
- data/lib/action_dispatch/system_testing/browser.rb +80 -0
- data/lib/action_dispatch/system_testing/driver.rb +68 -0
- data/lib/action_dispatch/system_testing/server.rb +31 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +97 -0
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +33 -0
- data/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb +26 -0
- data/lib/action_dispatch/testing/assertion_response.rb +47 -0
- data/lib/action_dispatch/testing/assertions.rb +24 -0
- data/lib/action_dispatch/testing/assertions/response.rb +106 -0
- data/lib/action_dispatch/testing/assertions/routing.rb +234 -0
- data/lib/action_dispatch/testing/integration.rb +659 -0
- data/lib/action_dispatch/testing/request_encoder.rb +55 -0
- data/lib/action_dispatch/testing/test_process.rb +50 -0
- data/lib/action_dispatch/testing/test_request.rb +71 -0
- data/lib/action_dispatch/testing/test_response.rb +25 -0
- data/lib/action_pack.rb +26 -0
- data/lib/action_pack/gem_version.rb +17 -0
- data/lib/action_pack/version.rb +10 -0
- metadata +329 -0
@@ -0,0 +1,150 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_view"
|
4
|
+
require "action_controller"
|
5
|
+
require "action_controller/log_subscriber"
|
6
|
+
|
7
|
+
module ActionController
|
8
|
+
# API Controller is a lightweight version of <tt>ActionController::Base</tt>,
|
9
|
+
# created for applications that don't require all functionalities that a complete
|
10
|
+
# \Rails controller provides, allowing you to create controllers with just the
|
11
|
+
# features that you need for API only applications.
|
12
|
+
#
|
13
|
+
# An API Controller is different from a normal controller in the sense that
|
14
|
+
# by default it doesn't include a number of features that are usually required
|
15
|
+
# by browser access only: layouts and templates rendering,
|
16
|
+
# flash, assets, and so on. This makes the entire controller stack thinner,
|
17
|
+
# suitable for API applications. It doesn't mean you won't have such
|
18
|
+
# features if you need them: they're all available for you to include in
|
19
|
+
# your application, they're just not part of the default API controller stack.
|
20
|
+
#
|
21
|
+
# Normally, +ApplicationController+ is the only controller that inherits from
|
22
|
+
# <tt>ActionController::API</tt>. All other controllers in turn inherit from
|
23
|
+
# +ApplicationController+.
|
24
|
+
#
|
25
|
+
# A sample controller could look like this:
|
26
|
+
#
|
27
|
+
# class PostsController < ApplicationController
|
28
|
+
# def index
|
29
|
+
# posts = Post.all
|
30
|
+
# render json: posts
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# Request, response, and parameters objects all work the exact same way as
|
35
|
+
# <tt>ActionController::Base</tt>.
|
36
|
+
#
|
37
|
+
# == Renders
|
38
|
+
#
|
39
|
+
# The default API Controller stack includes all renderers, which means you
|
40
|
+
# can use <tt>render :json</tt> and brothers freely in your controllers. Keep
|
41
|
+
# in mind that templates are not going to be rendered, so you need to ensure
|
42
|
+
# your controller is calling either <tt>render</tt> or <tt>redirect_to</tt> in
|
43
|
+
# all actions, otherwise it will return 204 No Content.
|
44
|
+
#
|
45
|
+
# def show
|
46
|
+
# post = Post.find(params[:id])
|
47
|
+
# render json: post
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# == Redirects
|
51
|
+
#
|
52
|
+
# Redirects are used to move from one action to another. You can use the
|
53
|
+
# <tt>redirect_to</tt> method in your controllers in the same way as in
|
54
|
+
# <tt>ActionController::Base</tt>. For example:
|
55
|
+
#
|
56
|
+
# def create
|
57
|
+
# redirect_to root_url and return if not_authorized?
|
58
|
+
# # do stuff here
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# == Adding New Behavior
|
62
|
+
#
|
63
|
+
# In some scenarios you may want to add back some functionality provided by
|
64
|
+
# <tt>ActionController::Base</tt> that is not present by default in
|
65
|
+
# <tt>ActionController::API</tt>, for instance <tt>MimeResponds</tt>. This
|
66
|
+
# module gives you the <tt>respond_to</tt> method. Adding it is quite simple,
|
67
|
+
# you just need to include the module in a specific controller or in
|
68
|
+
# +ApplicationController+ in case you want it available in your entire
|
69
|
+
# application:
|
70
|
+
#
|
71
|
+
# class ApplicationController < ActionController::API
|
72
|
+
# include ActionController::MimeResponds
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# class PostsController < ApplicationController
|
76
|
+
# def index
|
77
|
+
# posts = Post.all
|
78
|
+
#
|
79
|
+
# respond_to do |format|
|
80
|
+
# format.json { render json: posts }
|
81
|
+
# format.xml { render xml: posts }
|
82
|
+
# end
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# Make sure to check the modules included in <tt>ActionController::Base</tt>
|
87
|
+
# if you want to use any other functionality that is not provided
|
88
|
+
# by <tt>ActionController::API</tt> out of the box.
|
89
|
+
class API < Metal
|
90
|
+
abstract!
|
91
|
+
|
92
|
+
# Shortcut helper that returns all the ActionController::API modules except
|
93
|
+
# the ones passed as arguments:
|
94
|
+
#
|
95
|
+
# class MyAPIBaseController < ActionController::Metal
|
96
|
+
# ActionController::API.without_modules(:ForceSSL, :UrlFor).each do |left|
|
97
|
+
# include left
|
98
|
+
# end
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# This gives better control over what you want to exclude and makes it easier
|
102
|
+
# to create an API controller class, instead of listing the modules required
|
103
|
+
# manually.
|
104
|
+
def self.without_modules(*modules)
|
105
|
+
modules = modules.map do |m|
|
106
|
+
m.is_a?(Symbol) ? ActionController.const_get(m) : m
|
107
|
+
end
|
108
|
+
|
109
|
+
MODULES - modules
|
110
|
+
end
|
111
|
+
|
112
|
+
MODULES = [
|
113
|
+
AbstractController::Rendering,
|
114
|
+
|
115
|
+
UrlFor,
|
116
|
+
Redirecting,
|
117
|
+
ApiRendering,
|
118
|
+
Renderers::All,
|
119
|
+
ConditionalGet,
|
120
|
+
BasicImplicitRender,
|
121
|
+
StrongParameters,
|
122
|
+
|
123
|
+
ForceSSL,
|
124
|
+
DataStreaming,
|
125
|
+
DefaultHeaders,
|
126
|
+
|
127
|
+
# Before callbacks should also be executed as early as possible, so
|
128
|
+
# also include them at the bottom.
|
129
|
+
AbstractController::Callbacks,
|
130
|
+
|
131
|
+
# Append rescue at the bottom to wrap as much as possible.
|
132
|
+
Rescue,
|
133
|
+
|
134
|
+
# Add instrumentations hooks at the bottom, to ensure they instrument
|
135
|
+
# all the methods properly.
|
136
|
+
Instrumentation,
|
137
|
+
|
138
|
+
# Params wrapper should come before instrumentation so they are
|
139
|
+
# properly showed in logs
|
140
|
+
ParamsWrapper
|
141
|
+
]
|
142
|
+
|
143
|
+
MODULES.each do |mod|
|
144
|
+
include mod
|
145
|
+
end
|
146
|
+
|
147
|
+
ActiveSupport.run_load_hooks(:action_controller_api, self)
|
148
|
+
ActiveSupport.run_load_hooks(:action_controller, self)
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module ApiRendering
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Rendering
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_to_body(options = {})
|
12
|
+
_process_options(options)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,271 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "action_view"
|
4
|
+
require "action_controller/log_subscriber"
|
5
|
+
require "action_controller/metal/params_wrapper"
|
6
|
+
|
7
|
+
module ActionController
|
8
|
+
# Action Controllers are the core of a web request in \Rails. They are made up of one or more actions that are executed
|
9
|
+
# on request and then either it renders a template or redirects to another action. An action is defined as a public method
|
10
|
+
# on the controller, which will automatically be made accessible to the web-server through \Rails Routes.
|
11
|
+
#
|
12
|
+
# By default, only the ApplicationController in a \Rails application inherits from <tt>ActionController::Base</tt>. All other
|
13
|
+
# controllers inherit from ApplicationController. This gives you one class to configure things such as
|
14
|
+
# request forgery protection and filtering of sensitive request parameters.
|
15
|
+
#
|
16
|
+
# A sample controller could look like this:
|
17
|
+
#
|
18
|
+
# class PostsController < ApplicationController
|
19
|
+
# def index
|
20
|
+
# @posts = Post.all
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# def create
|
24
|
+
# @post = Post.create params[:post]
|
25
|
+
# redirect_to posts_path
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Actions, by default, render a template in the <tt>app/views</tt> directory corresponding to the name of the controller and action
|
30
|
+
# after executing code in the action. For example, the +index+ action of the PostsController would render the
|
31
|
+
# template <tt>app/views/posts/index.html.erb</tt> by default after populating the <tt>@posts</tt> instance variable.
|
32
|
+
#
|
33
|
+
# Unlike index, the create action will not render a template. After performing its main purpose (creating a
|
34
|
+
# new post), it initiates a redirect instead. This redirect works by returning an external
|
35
|
+
# <tt>302 Moved</tt> HTTP response that takes the user to the index action.
|
36
|
+
#
|
37
|
+
# These two methods represent the two basic action archetypes used in Action Controllers: Get-and-show and do-and-redirect.
|
38
|
+
# Most actions are variations on these themes.
|
39
|
+
#
|
40
|
+
# == Requests
|
41
|
+
#
|
42
|
+
# For every request, the router determines the value of the +controller+ and +action+ keys. These determine which controller
|
43
|
+
# and action are called. The remaining request parameters, the session (if one is available), and the full request with
|
44
|
+
# all the HTTP headers are made available to the action through accessor methods. Then the action is performed.
|
45
|
+
#
|
46
|
+
# The full request object is available via the request accessor and is primarily used to query for HTTP headers:
|
47
|
+
#
|
48
|
+
# def server_ip
|
49
|
+
# location = request.env["REMOTE_ADDR"]
|
50
|
+
# render plain: "This server hosted at #{location}"
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# == Parameters
|
54
|
+
#
|
55
|
+
# All request parameters, whether they come from a query string in the URL or form data submitted through a POST request are
|
56
|
+
# available through the <tt>params</tt> method which returns a hash. For example, an action that was performed through
|
57
|
+
# <tt>/posts?category=All&limit=5</tt> will include <tt>{ "category" => "All", "limit" => "5" }</tt> in <tt>params</tt>.
|
58
|
+
#
|
59
|
+
# It's also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as:
|
60
|
+
#
|
61
|
+
# <input type="text" name="post[name]" value="david">
|
62
|
+
# <input type="text" name="post[address]" value="hyacintvej">
|
63
|
+
#
|
64
|
+
# A request coming from a form holding these inputs will include <tt>{ "post" => { "name" => "david", "address" => "hyacintvej" } }</tt>.
|
65
|
+
# If the address input had been named <tt>post[address][street]</tt>, the <tt>params</tt> would have included
|
66
|
+
# <tt>{ "post" => { "address" => { "street" => "hyacintvej" } } }</tt>. There's no limit to the depth of the nesting.
|
67
|
+
#
|
68
|
+
# == Sessions
|
69
|
+
#
|
70
|
+
# Sessions allow you to store objects in between requests. This is useful for objects that are not yet ready to be persisted,
|
71
|
+
# 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
|
72
|
+
# 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
|
73
|
+
# they could be changed unknowingly. It's usually too much work to keep it all synchronized -- something databases already excel at.
|
74
|
+
#
|
75
|
+
# You can place objects in the session by using the <tt>session</tt> method, which accesses a hash:
|
76
|
+
#
|
77
|
+
# session[:person] = Person.authenticate(user_name, password)
|
78
|
+
#
|
79
|
+
# You can retrieve it again through the same hash:
|
80
|
+
#
|
81
|
+
# "Hello #{session[:person]}"
|
82
|
+
#
|
83
|
+
# For removing objects from the session, you can either assign a single key to +nil+:
|
84
|
+
#
|
85
|
+
# # removes :person from session
|
86
|
+
# session[:person] = nil
|
87
|
+
#
|
88
|
+
# or you can remove the entire session with +reset_session+.
|
89
|
+
#
|
90
|
+
# Sessions are stored by default in a browser cookie that's cryptographically signed, but unencrypted.
|
91
|
+
# This prevents the user from tampering with the session but also allows them to see its contents.
|
92
|
+
#
|
93
|
+
# Do not put secret information in cookie-based sessions!
|
94
|
+
#
|
95
|
+
# == Responses
|
96
|
+
#
|
97
|
+
# Each action results in a response, which holds the headers and document to be sent to the user's browser. The actual response
|
98
|
+
# object is generated automatically through the use of renders and redirects and requires no user intervention.
|
99
|
+
#
|
100
|
+
# == Renders
|
101
|
+
#
|
102
|
+
# Action Controller sends content to the user by using one of five rendering methods. The most versatile and common is the rendering
|
103
|
+
# of a template. Included in the Action Pack is the Action View, which enables rendering of ERB templates. It's automatically configured.
|
104
|
+
# The controller passes objects to the view by assigning instance variables:
|
105
|
+
#
|
106
|
+
# def show
|
107
|
+
# @post = Post.find(params[:id])
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
# Which are then automatically available to the view:
|
111
|
+
#
|
112
|
+
# Title: <%= @post.title %>
|
113
|
+
#
|
114
|
+
# You don't have to rely on the automated rendering. For example, actions that could result in the rendering of different templates
|
115
|
+
# will use the manual rendering methods:
|
116
|
+
#
|
117
|
+
# def search
|
118
|
+
# @results = Search.find(params[:query])
|
119
|
+
# case @results.count
|
120
|
+
# when 0 then render action: "no_results"
|
121
|
+
# when 1 then render action: "show"
|
122
|
+
# when 2..10 then render action: "show_many"
|
123
|
+
# end
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# Read more about writing ERB and Builder templates in ActionView::Base.
|
127
|
+
#
|
128
|
+
# == Redirects
|
129
|
+
#
|
130
|
+
# Redirects are used to move from one action to another. For example, after a <tt>create</tt> action, which stores a blog entry to the
|
131
|
+
# database, we might like to show the user the new entry. Because we're following good DRY principles (Don't Repeat Yourself), we're
|
132
|
+
# going to reuse (and redirect to) a <tt>show</tt> action that we'll assume has already been created. The code might look like this:
|
133
|
+
#
|
134
|
+
# def create
|
135
|
+
# @entry = Entry.new(params[:entry])
|
136
|
+
# if @entry.save
|
137
|
+
# # The entry was saved correctly, redirect to show
|
138
|
+
# redirect_to action: 'show', id: @entry.id
|
139
|
+
# else
|
140
|
+
# # things didn't go so well, do something else
|
141
|
+
# end
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
# In this case, after saving our new entry to the database, the user is redirected to the <tt>show</tt> method, which is then executed.
|
145
|
+
# Note that this is an external HTTP-level redirection which will cause the browser to make a second request (a GET to the show action),
|
146
|
+
# and not some internal re-routing which calls both "create" and then "show" within one request.
|
147
|
+
#
|
148
|
+
# Learn more about <tt>redirect_to</tt> and what options you have in ActionController::Redirecting.
|
149
|
+
#
|
150
|
+
# == Calling multiple redirects or renders
|
151
|
+
#
|
152
|
+
# An action may contain only a single render or a single redirect. Attempting to try to do either again will result in a DoubleRenderError:
|
153
|
+
#
|
154
|
+
# def do_something
|
155
|
+
# redirect_to action: "elsewhere"
|
156
|
+
# render action: "overthere" # raises DoubleRenderError
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# If you need to redirect on the condition of something, then be sure to add "and return" to halt execution.
|
160
|
+
#
|
161
|
+
# def do_something
|
162
|
+
# redirect_to(action: "elsewhere") and return if monkeys.nil?
|
163
|
+
# render action: "overthere" # won't be called if monkeys is nil
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
class Base < Metal
|
167
|
+
abstract!
|
168
|
+
|
169
|
+
# We document the request and response methods here because albeit they are
|
170
|
+
# implemented in ActionController::Metal, the type of the returned objects
|
171
|
+
# is unknown at that level.
|
172
|
+
|
173
|
+
##
|
174
|
+
# :method: request
|
175
|
+
#
|
176
|
+
# Returns an ActionDispatch::Request instance that represents the
|
177
|
+
# current request.
|
178
|
+
|
179
|
+
##
|
180
|
+
# :method: response
|
181
|
+
#
|
182
|
+
# Returns an ActionDispatch::Response that represents the current
|
183
|
+
# response.
|
184
|
+
|
185
|
+
# Shortcut helper that returns all the modules included in
|
186
|
+
# ActionController::Base except the ones passed as arguments:
|
187
|
+
#
|
188
|
+
# class MyBaseController < ActionController::Metal
|
189
|
+
# ActionController::Base.without_modules(:ParamsWrapper, :Streaming).each do |left|
|
190
|
+
# include left
|
191
|
+
# end
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# This gives better control over what you want to exclude and makes it
|
195
|
+
# easier to create a bare controller class, instead of listing the modules
|
196
|
+
# required manually.
|
197
|
+
def self.without_modules(*modules)
|
198
|
+
modules = modules.map do |m|
|
199
|
+
m.is_a?(Symbol) ? ActionController.const_get(m) : m
|
200
|
+
end
|
201
|
+
|
202
|
+
MODULES - modules
|
203
|
+
end
|
204
|
+
|
205
|
+
MODULES = [
|
206
|
+
AbstractController::Rendering,
|
207
|
+
AbstractController::Translation,
|
208
|
+
AbstractController::AssetPaths,
|
209
|
+
|
210
|
+
Helpers,
|
211
|
+
UrlFor,
|
212
|
+
Redirecting,
|
213
|
+
ActionView::Layouts,
|
214
|
+
Rendering,
|
215
|
+
Renderers::All,
|
216
|
+
ConditionalGet,
|
217
|
+
EtagWithTemplateDigest,
|
218
|
+
EtagWithFlash,
|
219
|
+
Caching,
|
220
|
+
MimeResponds,
|
221
|
+
ImplicitRender,
|
222
|
+
StrongParameters,
|
223
|
+
ParameterEncoding,
|
224
|
+
Cookies,
|
225
|
+
Flash,
|
226
|
+
FormBuilder,
|
227
|
+
RequestForgeryProtection,
|
228
|
+
ContentSecurityPolicy,
|
229
|
+
ForceSSL,
|
230
|
+
Streaming,
|
231
|
+
DataStreaming,
|
232
|
+
HttpAuthentication::Basic::ControllerMethods,
|
233
|
+
HttpAuthentication::Digest::ControllerMethods,
|
234
|
+
HttpAuthentication::Token::ControllerMethods,
|
235
|
+
DefaultHeaders,
|
236
|
+
|
237
|
+
# Before callbacks should also be executed as early as possible, so
|
238
|
+
# also include them at the bottom.
|
239
|
+
AbstractController::Callbacks,
|
240
|
+
|
241
|
+
# Append rescue at the bottom to wrap as much as possible.
|
242
|
+
Rescue,
|
243
|
+
|
244
|
+
# Add instrumentations hooks at the bottom, to ensure they instrument
|
245
|
+
# all the methods properly.
|
246
|
+
Instrumentation,
|
247
|
+
|
248
|
+
# Params wrapper should come before instrumentation so they are
|
249
|
+
# properly showed in logs
|
250
|
+
ParamsWrapper
|
251
|
+
]
|
252
|
+
|
253
|
+
MODULES.each do |mod|
|
254
|
+
include mod
|
255
|
+
end
|
256
|
+
setup_renderer!
|
257
|
+
|
258
|
+
# Define some internal variables that should not be propagated to the view.
|
259
|
+
PROTECTED_IVARS = AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + %i(
|
260
|
+
@_params @_response @_request @_config @_url_options @_action_has_layout @_view_context_class
|
261
|
+
@_view_renderer @_lookup_context @_routes @_view_runtime @_db_runtime @_helper_proxy
|
262
|
+
)
|
263
|
+
|
264
|
+
def _protected_ivars # :nodoc:
|
265
|
+
PROTECTED_IVARS
|
266
|
+
end
|
267
|
+
|
268
|
+
ActiveSupport.run_load_hooks(:action_controller_base, self)
|
269
|
+
ActiveSupport.run_load_hooks(:action_controller, self)
|
270
|
+
end
|
271
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
# \Caching is a cheap way of speeding up slow applications by keeping the result of
|
5
|
+
# calculations, renderings, and database calls around for subsequent requests.
|
6
|
+
#
|
7
|
+
# You can read more about each approach by clicking the modules below.
|
8
|
+
#
|
9
|
+
# Note: To turn off all caching provided by Action Controller, set
|
10
|
+
# config.action_controller.perform_caching = false
|
11
|
+
#
|
12
|
+
# == \Caching stores
|
13
|
+
#
|
14
|
+
# All the caching stores from ActiveSupport::Cache are available to be used as backends
|
15
|
+
# for Action Controller caching.
|
16
|
+
#
|
17
|
+
# Configuration examples (FileStore is the default):
|
18
|
+
#
|
19
|
+
# config.action_controller.cache_store = :memory_store
|
20
|
+
# config.action_controller.cache_store = :file_store, '/path/to/cache/directory'
|
21
|
+
# config.action_controller.cache_store = :mem_cache_store, 'localhost'
|
22
|
+
# config.action_controller.cache_store = :mem_cache_store, Memcached::Rails.new('localhost:11211')
|
23
|
+
# config.action_controller.cache_store = MyOwnStore.new('parameter')
|
24
|
+
module Caching
|
25
|
+
extend ActiveSupport::Autoload
|
26
|
+
extend ActiveSupport::Concern
|
27
|
+
|
28
|
+
included do
|
29
|
+
include AbstractController::Caching
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def instrument_payload(key)
|
35
|
+
{
|
36
|
+
controller: controller_name,
|
37
|
+
action: action_name,
|
38
|
+
key: key
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def instrument_name
|
43
|
+
"action_controller"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|