actionpack 5.2.3
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 +429 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +57 -0
- data/lib/abstract_controller.rb +27 -0
- data/lib/abstract_controller/asset_paths.rb +12 -0
- data/lib/abstract_controller/base.rb +265 -0
- data/lib/abstract_controller/caching.rb +66 -0
- data/lib/abstract_controller/caching/fragments.rb +166 -0
- data/lib/abstract_controller/callbacks.rb +212 -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 +31 -0
- data/lib/abstract_controller/url_for.rb +35 -0
- data/lib/action_controller.rb +66 -0
- data/lib/action_controller/api.rb +149 -0
- data/lib/action_controller/api/api_rendering.rb +16 -0
- data/lib/action_controller/base.rb +276 -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 +78 -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 +274 -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 +152 -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 +53 -0
- data/lib/action_controller/metal/flash.rb +61 -0
- data/lib/action_controller/metal/force_ssl.rb +99 -0
- data/lib/action_controller/metal/head.rb +60 -0
- data/lib/action_controller/metal/helpers.rb +123 -0
- data/lib/action_controller/metal/http_authentication.rb +519 -0
- data/lib/action_controller/metal/implicit_render.rb +73 -0
- data/lib/action_controller/metal/instrumentation.rb +107 -0
- data/lib/action_controller/metal/live.rb +312 -0
- data/lib/action_controller/metal/mime_responds.rb +313 -0
- data/lib/action_controller/metal/parameter_encoding.rb +51 -0
- data/lib/action_controller/metal/params_wrapper.rb +293 -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 +445 -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 +1086 -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 +117 -0
- data/lib/action_controller/template_assertions.rb +11 -0
- data/lib/action_controller/test_case.rb +629 -0
- data/lib/action_dispatch.rb +112 -0
- data/lib/action_dispatch/http/cache.rb +222 -0
- data/lib/action_dispatch/http/content_security_policy.rb +272 -0
- data/lib/action_dispatch/http/filter_parameters.rb +84 -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 +175 -0
- data/lib/action_dispatch/http/mime_type.rb +342 -0
- data/lib/action_dispatch/http/mime_types.rb +50 -0
- data/lib/action_dispatch/http/parameter_filter.rb +86 -0
- data/lib/action_dispatch/http/parameters.rb +126 -0
- data/lib/action_dispatch/http/rack_cache.rb +63 -0
- data/lib/action_dispatch/http/request.rb +430 -0
- data/lib/action_dispatch/http/response.rb +519 -0
- data/lib/action_dispatch/http/upload.rb +84 -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 +49 -0
- data/lib/action_dispatch/journey/nfa/transition_table.rb +120 -0
- data/lib/action_dispatch/journey/nodes/node.rb +140 -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 +198 -0
- data/lib/action_dispatch/journey/route.rb +203 -0
- data/lib/action_dispatch/journey/router.rb +156 -0
- data/lib/action_dispatch/journey/router/utils.rb +102 -0
- data/lib/action_dispatch/journey/routes.rb +82 -0
- data/lib/action_dispatch/journey/scanner.rb +64 -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/callbacks.rb +36 -0
- data/lib/action_dispatch/middleware/cookies.rb +685 -0
- data/lib/action_dispatch/middleware/debug_exceptions.rb +205 -0
- data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +147 -0
- data/lib/action_dispatch/middleware/executor.rb +21 -0
- data/lib/action_dispatch/middleware/flash.rb +300 -0
- data/lib/action_dispatch/middleware/public_exceptions.rb +57 -0
- data/lib/action_dispatch/middleware/reloader.rb +12 -0
- data/lib/action_dispatch/middleware/remote_ip.rb +183 -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 +118 -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 +116 -0
- data/lib/action_dispatch/middleware/static.rb +130 -0
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +22 -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 +27 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +16 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +21 -0
- data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +13 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +161 -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 +200 -0
- data/lib/action_dispatch/railtie.rb +55 -0
- data/lib/action_dispatch/request/session.rb +234 -0
- data/lib/action_dispatch/request/utils.rb +78 -0
- data/lib/action_dispatch/routing.rb +260 -0
- data/lib/action_dispatch/routing/endpoint.rb +17 -0
- data/lib/action_dispatch/routing/inspector.rb +225 -0
- data/lib/action_dispatch/routing/mapper.rb +2267 -0
- data/lib/action_dispatch/routing/polymorphic_routes.rb +352 -0
- data/lib/action_dispatch/routing/redirection.rb +201 -0
- data/lib/action_dispatch/routing/route_set.rb +890 -0
- data/lib/action_dispatch/routing/routes_proxy.rb +69 -0
- data/lib/action_dispatch/routing/url_for.rb +236 -0
- data/lib/action_dispatch/system_test_case.rb +147 -0
- data/lib/action_dispatch/system_testing/browser.rb +49 -0
- data/lib/action_dispatch/system_testing/driver.rb +59 -0
- data/lib/action_dispatch/system_testing/server.rb +31 -0
- data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +96 -0
- data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +31 -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 +107 -0
- data/lib/action_dispatch/testing/assertions/routing.rb +222 -0
- data/lib/action_dispatch/testing/integration.rb +652 -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 +53 -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 +318 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/benchmarkable"
|
4
|
+
|
5
|
+
module AbstractController
|
6
|
+
module Logger #:nodoc:
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
config_accessor :logger
|
11
|
+
include ActiveSupport::Benchmarkable
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AbstractController
|
4
|
+
module Railties
|
5
|
+
module RoutesHelpers
|
6
|
+
def self.with(routes, include_path_helpers = true)
|
7
|
+
Module.new do
|
8
|
+
define_method(:inherited) do |klass|
|
9
|
+
super(klass)
|
10
|
+
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_routes_url_helpers) }
|
11
|
+
klass.include(namespace.railtie_routes_url_helpers(include_path_helpers))
|
12
|
+
else
|
13
|
+
klass.include(routes.url_helpers(include_path_helpers))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "abstract_controller/error"
|
4
|
+
require "action_view"
|
5
|
+
require "action_view/view_paths"
|
6
|
+
require "set"
|
7
|
+
|
8
|
+
module AbstractController
|
9
|
+
class DoubleRenderError < Error
|
10
|
+
DEFAULT_MESSAGE = "Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \"redirect_to(...) and return\"."
|
11
|
+
|
12
|
+
def initialize(message = nil)
|
13
|
+
super(message || DEFAULT_MESSAGE)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Rendering
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
include ActionView::ViewPaths
|
20
|
+
|
21
|
+
# Normalizes arguments, options and then delegates render_to_body and
|
22
|
+
# sticks the result in <tt>self.response_body</tt>.
|
23
|
+
def render(*args, &block)
|
24
|
+
options = _normalize_render(*args, &block)
|
25
|
+
rendered_body = render_to_body(options)
|
26
|
+
if options[:html]
|
27
|
+
_set_html_content_type
|
28
|
+
else
|
29
|
+
_set_rendered_content_type rendered_format
|
30
|
+
end
|
31
|
+
self.response_body = rendered_body
|
32
|
+
end
|
33
|
+
|
34
|
+
# Raw rendering of a template to a string.
|
35
|
+
#
|
36
|
+
# It is similar to render, except that it does not
|
37
|
+
# set the +response_body+ and it should be guaranteed
|
38
|
+
# to always return a string.
|
39
|
+
#
|
40
|
+
# If a component extends the semantics of +response_body+
|
41
|
+
# (as ActionController extends it to be anything that
|
42
|
+
# responds to the method each), this method needs to be
|
43
|
+
# overridden in order to still return a string.
|
44
|
+
def render_to_string(*args, &block)
|
45
|
+
options = _normalize_render(*args, &block)
|
46
|
+
render_to_body(options)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Performs the actual template rendering.
|
50
|
+
def render_to_body(options = {})
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns Content-Type of rendered content.
|
54
|
+
def rendered_format
|
55
|
+
Mime[:text]
|
56
|
+
end
|
57
|
+
|
58
|
+
DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i(
|
59
|
+
@_action_name @_response_body @_formats @_prefixes
|
60
|
+
)
|
61
|
+
|
62
|
+
# This method should return a hash with assigns.
|
63
|
+
# You can overwrite this configuration per controller.
|
64
|
+
def view_assigns
|
65
|
+
protected_vars = _protected_ivars
|
66
|
+
variables = instance_variables
|
67
|
+
|
68
|
+
variables.reject! { |s| protected_vars.include? s }
|
69
|
+
variables.each_with_object({}) { |name, hash|
|
70
|
+
hash[name.slice(1, name.length)] = instance_variable_get(name)
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
# Normalize args by converting <tt>render "foo"</tt> to
|
76
|
+
# <tt>render :action => "foo"</tt> and <tt>render "foo/bar"</tt> to
|
77
|
+
# <tt>render :file => "foo/bar"</tt>.
|
78
|
+
def _normalize_args(action = nil, options = {}) # :doc:
|
79
|
+
if action.respond_to?(:permitted?)
|
80
|
+
if action.permitted?
|
81
|
+
action
|
82
|
+
else
|
83
|
+
raise ArgumentError, "render parameters are not permitted"
|
84
|
+
end
|
85
|
+
elsif action.is_a?(Hash)
|
86
|
+
action
|
87
|
+
else
|
88
|
+
options
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Normalize options.
|
93
|
+
def _normalize_options(options) # :doc:
|
94
|
+
options
|
95
|
+
end
|
96
|
+
|
97
|
+
# Process extra options.
|
98
|
+
def _process_options(options) # :doc:
|
99
|
+
options
|
100
|
+
end
|
101
|
+
|
102
|
+
# Process the rendered format.
|
103
|
+
def _process_format(format) # :nodoc:
|
104
|
+
end
|
105
|
+
|
106
|
+
def _process_variant(options)
|
107
|
+
end
|
108
|
+
|
109
|
+
def _set_html_content_type # :nodoc:
|
110
|
+
end
|
111
|
+
|
112
|
+
def _set_rendered_content_type(format) # :nodoc:
|
113
|
+
end
|
114
|
+
|
115
|
+
# Normalize args and options.
|
116
|
+
def _normalize_render(*args, &block) # :nodoc:
|
117
|
+
options = _normalize_args(*args, &block)
|
118
|
+
_process_variant(options)
|
119
|
+
_normalize_options(options)
|
120
|
+
options
|
121
|
+
end
|
122
|
+
|
123
|
+
def _protected_ivars # :nodoc:
|
124
|
+
DEFAULT_PROTECTED_INSTANCE_VARIABLES
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AbstractController
|
4
|
+
module Translation
|
5
|
+
# Delegates to <tt>I18n.translate</tt>. Also aliased as <tt>t</tt>.
|
6
|
+
#
|
7
|
+
# When the given key starts with a period, it will be scoped by the current
|
8
|
+
# controller and action. So if you call <tt>translate(".foo")</tt> from
|
9
|
+
# <tt>PeopleController#index</tt>, it will convert the call to
|
10
|
+
# <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
|
11
|
+
# to translate many keys within the same controller / action and gives you a
|
12
|
+
# simple framework for scoping them consistently.
|
13
|
+
def translate(key, options = {})
|
14
|
+
if key.to_s.first == "."
|
15
|
+
path = controller_path.tr("/", ".")
|
16
|
+
defaults = [:"#{path}#{key}"]
|
17
|
+
defaults << options[:default] if options[:default]
|
18
|
+
options[:default] = defaults.flatten
|
19
|
+
key = "#{path}.#{action_name}#{key}"
|
20
|
+
end
|
21
|
+
I18n.translate(key, options)
|
22
|
+
end
|
23
|
+
alias :t :translate
|
24
|
+
|
25
|
+
# Delegates to <tt>I18n.localize</tt>. Also aliased as <tt>l</tt>.
|
26
|
+
def localize(*args)
|
27
|
+
I18n.localize(*args)
|
28
|
+
end
|
29
|
+
alias :l :localize
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module AbstractController
|
4
|
+
# Includes +url_for+ into the host class (e.g. an abstract controller or mailer). The class
|
5
|
+
# has to provide a +RouteSet+ by implementing the <tt>_routes</tt> methods. Otherwise, an
|
6
|
+
# exception will be raised.
|
7
|
+
#
|
8
|
+
# Note that this module is completely decoupled from HTTP - the only requirement is a valid
|
9
|
+
# <tt>_routes</tt> implementation.
|
10
|
+
module UrlFor
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
include ActionDispatch::Routing::UrlFor
|
13
|
+
|
14
|
+
def _routes
|
15
|
+
raise "In order to use #url_for, you must include routing helpers explicitly. " \
|
16
|
+
"For instance, `include Rails.application.routes.url_helpers`."
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def _routes
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def action_methods
|
25
|
+
@action_methods ||= begin
|
26
|
+
if _routes
|
27
|
+
super - _routes.named_routes.helper_names
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/rails"
|
4
|
+
require "abstract_controller"
|
5
|
+
require "action_dispatch"
|
6
|
+
require "action_controller/metal/live"
|
7
|
+
require "action_controller/metal/strong_parameters"
|
8
|
+
|
9
|
+
module ActionController
|
10
|
+
extend ActiveSupport::Autoload
|
11
|
+
|
12
|
+
autoload :API
|
13
|
+
autoload :Base
|
14
|
+
autoload :Metal
|
15
|
+
autoload :Middleware
|
16
|
+
autoload :Renderer
|
17
|
+
autoload :FormBuilder
|
18
|
+
|
19
|
+
eager_autoload do
|
20
|
+
autoload :Caching
|
21
|
+
end
|
22
|
+
|
23
|
+
autoload_under "metal" do
|
24
|
+
autoload :ConditionalGet
|
25
|
+
autoload :ContentSecurityPolicy
|
26
|
+
autoload :Cookies
|
27
|
+
autoload :DataStreaming
|
28
|
+
autoload :EtagWithTemplateDigest
|
29
|
+
autoload :EtagWithFlash
|
30
|
+
autoload :Flash
|
31
|
+
autoload :ForceSSL
|
32
|
+
autoload :Head
|
33
|
+
autoload :Helpers
|
34
|
+
autoload :HttpAuthentication
|
35
|
+
autoload :BasicImplicitRender
|
36
|
+
autoload :ImplicitRender
|
37
|
+
autoload :Instrumentation
|
38
|
+
autoload :MimeResponds
|
39
|
+
autoload :ParamsWrapper
|
40
|
+
autoload :Redirecting
|
41
|
+
autoload :Renderers
|
42
|
+
autoload :Rendering
|
43
|
+
autoload :RequestForgeryProtection
|
44
|
+
autoload :Rescue
|
45
|
+
autoload :Streaming
|
46
|
+
autoload :StrongParameters
|
47
|
+
autoload :ParameterEncoding
|
48
|
+
autoload :Testing
|
49
|
+
autoload :UrlFor
|
50
|
+
end
|
51
|
+
|
52
|
+
autoload_under "api" do
|
53
|
+
autoload :ApiRendering
|
54
|
+
end
|
55
|
+
|
56
|
+
autoload :TestCase, "action_controller/test_case"
|
57
|
+
autoload :TemplateAssertions, "action_controller/test_case"
|
58
|
+
end
|
59
|
+
|
60
|
+
# Common Active Support usage in Action Controller
|
61
|
+
require "active_support/core_ext/module/attribute_accessors"
|
62
|
+
require "active_support/core_ext/load_error"
|
63
|
+
require "active_support/core_ext/module/attr_internal"
|
64
|
+
require "active_support/core_ext/name_error"
|
65
|
+
require "active_support/core_ext/uri"
|
66
|
+
require "active_support/inflector"
|
@@ -0,0 +1,149 @@
|
|
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, cookies, sessions,
|
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
|
+
|
126
|
+
# Before callbacks should also be executed as early as possible, so
|
127
|
+
# also include them at the bottom.
|
128
|
+
AbstractController::Callbacks,
|
129
|
+
|
130
|
+
# Append rescue at the bottom to wrap as much as possible.
|
131
|
+
Rescue,
|
132
|
+
|
133
|
+
# Add instrumentations hooks at the bottom, to ensure they instrument
|
134
|
+
# all the methods properly.
|
135
|
+
Instrumentation,
|
136
|
+
|
137
|
+
# Params wrapper should come before instrumentation so they are
|
138
|
+
# properly showed in logs
|
139
|
+
ParamsWrapper
|
140
|
+
]
|
141
|
+
|
142
|
+
MODULES.each do |mod|
|
143
|
+
include mod
|
144
|
+
end
|
145
|
+
|
146
|
+
ActiveSupport.run_load_hooks(:action_controller_api, self)
|
147
|
+
ActiveSupport.run_load_hooks(:action_controller, self)
|
148
|
+
end
|
149
|
+
end
|