actionpack 4.2.8 → 5.2.4.2
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 +285 -444
- data/MIT-LICENSE +1 -1
- data/README.rdoc +6 -7
- data/lib/abstract_controller.rb +12 -5
- data/lib/abstract_controller/asset_paths.rb +2 -0
- data/lib/abstract_controller/base.rb +45 -49
- data/lib/abstract_controller/caching.rb +66 -0
- data/lib/{action_controller → abstract_controller}/caching/fragments.rb +78 -15
- data/lib/abstract_controller/callbacks.rb +47 -31
- data/lib/abstract_controller/collector.rb +8 -11
- data/lib/abstract_controller/error.rb +6 -0
- data/lib/abstract_controller/helpers.rb +25 -25
- data/lib/abstract_controller/logger.rb +2 -0
- data/lib/abstract_controller/railties/routes_helpers.rb +4 -2
- data/lib/abstract_controller/rendering.rb +42 -41
- data/lib/abstract_controller/translation.rb +10 -7
- data/lib/abstract_controller/url_for.rb +2 -0
- data/lib/action_controller.rb +29 -21
- data/lib/action_controller/api.rb +149 -0
- data/lib/action_controller/api/api_rendering.rb +16 -0
- data/lib/action_controller/base.rb +27 -19
- data/lib/action_controller/caching.rb +14 -57
- data/lib/action_controller/form_builder.rb +50 -0
- data/lib/action_controller/log_subscriber.rb +10 -15
- data/lib/action_controller/metal.rb +98 -83
- data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
- data/lib/action_controller/metal/conditional_get.rb +118 -44
- data/lib/action_controller/metal/content_security_policy.rb +52 -0
- data/lib/action_controller/metal/cookies.rb +3 -3
- data/lib/action_controller/metal/data_streaming.rb +27 -46
- data/lib/action_controller/metal/etag_with_flash.rb +18 -0
- data/lib/action_controller/metal/etag_with_template_digest.rb +20 -13
- data/lib/action_controller/metal/exceptions.rb +8 -14
- data/lib/action_controller/metal/flash.rb +4 -3
- data/lib/action_controller/metal/force_ssl.rb +23 -21
- data/lib/action_controller/metal/head.rb +21 -19
- data/lib/action_controller/metal/helpers.rb +24 -14
- data/lib/action_controller/metal/http_authentication.rb +64 -57
- data/lib/action_controller/metal/implicit_render.rb +62 -8
- data/lib/action_controller/metal/instrumentation.rb +19 -21
- data/lib/action_controller/metal/live.rb +90 -106
- data/lib/action_controller/metal/mime_responds.rb +33 -46
- data/lib/action_controller/metal/parameter_encoding.rb +51 -0
- data/lib/action_controller/metal/params_wrapper.rb +61 -53
- data/lib/action_controller/metal/redirecting.rb +49 -28
- data/lib/action_controller/metal/renderers.rb +87 -44
- data/lib/action_controller/metal/rendering.rb +72 -50
- data/lib/action_controller/metal/request_forgery_protection.rb +203 -92
- data/lib/action_controller/metal/rescue.rb +9 -16
- data/lib/action_controller/metal/streaming.rb +12 -10
- data/lib/action_controller/metal/strong_parameters.rb +582 -165
- data/lib/action_controller/metal/testing.rb +2 -17
- data/lib/action_controller/metal/url_for.rb +19 -10
- data/lib/action_controller/railtie.rb +28 -10
- data/lib/action_controller/railties/helpers.rb +2 -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 +280 -411
- data/lib/action_dispatch.rb +27 -19
- data/lib/action_dispatch/http/cache.rb +93 -47
- data/lib/action_dispatch/http/content_security_policy.rb +272 -0
- data/lib/action_dispatch/http/filter_parameters.rb +26 -20
- data/lib/action_dispatch/http/filter_redirect.rb +10 -11
- data/lib/action_dispatch/http/headers.rb +55 -22
- data/lib/action_dispatch/http/mime_negotiation.rb +60 -41
- data/lib/action_dispatch/http/mime_type.rb +134 -121
- data/lib/action_dispatch/http/mime_types.rb +20 -6
- data/lib/action_dispatch/http/parameter_filter.rb +25 -11
- data/lib/action_dispatch/http/parameters.rb +98 -39
- data/lib/action_dispatch/http/rack_cache.rb +2 -0
- data/lib/action_dispatch/http/request.rb +200 -118
- data/lib/action_dispatch/http/response.rb +225 -110
- data/lib/action_dispatch/http/upload.rb +12 -6
- data/lib/action_dispatch/http/url.rb +110 -28
- data/lib/action_dispatch/journey.rb +7 -5
- data/lib/action_dispatch/journey/formatter.rb +55 -32
- data/lib/action_dispatch/journey/gtg/builder.rb +7 -5
- data/lib/action_dispatch/journey/gtg/simulator.rb +3 -9
- data/lib/action_dispatch/journey/gtg/transition_table.rb +17 -16
- data/lib/action_dispatch/journey/nfa/builder.rb +5 -3
- data/lib/action_dispatch/journey/nfa/dot.rb +13 -13
- data/lib/action_dispatch/journey/nfa/simulator.rb +3 -1
- data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -48
- data/lib/action_dispatch/journey/nodes/node.rb +18 -6
- data/lib/action_dispatch/journey/parser.rb +23 -22
- data/lib/action_dispatch/journey/parser.y +3 -2
- data/lib/action_dispatch/journey/parser_extras.rb +12 -4
- data/lib/action_dispatch/journey/path/pattern.rb +50 -44
- data/lib/action_dispatch/journey/route.rb +106 -28
- data/lib/action_dispatch/journey/router.rb +35 -23
- data/lib/action_dispatch/journey/router/utils.rb +20 -11
- data/lib/action_dispatch/journey/routes.rb +18 -16
- data/lib/action_dispatch/journey/scanner.rb +18 -15
- data/lib/action_dispatch/journey/visitors.rb +99 -52
- data/lib/action_dispatch/middleware/callbacks.rb +1 -2
- data/lib/action_dispatch/middleware/cookies.rb +304 -193
- data/lib/action_dispatch/middleware/debug_exceptions.rb +152 -57
- data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
- data/lib/action_dispatch/middleware/exception_wrapper.rb +68 -69
- data/lib/action_dispatch/middleware/executor.rb +21 -0
- data/lib/action_dispatch/middleware/flash.rb +78 -54
- data/lib/action_dispatch/middleware/public_exceptions.rb +27 -25
- data/lib/action_dispatch/middleware/reloader.rb +5 -91
- data/lib/action_dispatch/middleware/remote_ip.rb +41 -31
- data/lib/action_dispatch/middleware/request_id.rb +17 -9
- data/lib/action_dispatch/middleware/session/abstract_store.rb +41 -25
- data/lib/action_dispatch/middleware/session/cache_store.rb +24 -14
- data/lib/action_dispatch/middleware/session/cookie_store.rb +72 -67
- data/lib/action_dispatch/middleware/session/mem_cache_store.rb +8 -2
- data/lib/action_dispatch/middleware/show_exceptions.rb +26 -22
- data/lib/action_dispatch/middleware/ssl.rb +114 -36
- data/lib/action_dispatch/middleware/stack.rb +31 -44
- data/lib/action_dispatch/middleware/static.rb +57 -50
- data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +2 -14
- data/lib/action_dispatch/middleware/templates/rescues/{_source.erb → _source.html.erb} +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -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 +1 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -1
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +1 -1
- data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +4 -4
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +64 -64
- data/lib/action_dispatch/railtie.rb +19 -11
- data/lib/action_dispatch/request/session.rb +106 -59
- data/lib/action_dispatch/request/utils.rb +67 -24
- data/lib/action_dispatch/routing.rb +17 -18
- data/lib/action_dispatch/routing/endpoint.rb +9 -2
- data/lib/action_dispatch/routing/inspector.rb +58 -67
- data/lib/action_dispatch/routing/mapper.rb +734 -447
- data/lib/action_dispatch/routing/polymorphic_routes.rb +161 -139
- data/lib/action_dispatch/routing/redirection.rb +36 -26
- data/lib/action_dispatch/routing/route_set.rb +321 -291
- data/lib/action_dispatch/routing/routes_proxy.rb +32 -5
- data/lib/action_dispatch/routing/url_for.rb +65 -25
- 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 +6 -4
- data/lib/action_dispatch/testing/assertions/response.rb +45 -20
- data/lib/action_dispatch/testing/assertions/routing.rb +30 -26
- data/lib/action_dispatch/testing/integration.rb +347 -209
- data/lib/action_dispatch/testing/request_encoder.rb +55 -0
- data/lib/action_dispatch/testing/test_process.rb +28 -22
- data/lib/action_dispatch/testing/test_request.rb +27 -34
- data/lib/action_dispatch/testing/test_response.rb +35 -7
- data/lib/action_pack.rb +4 -2
- data/lib/action_pack/gem_version.rb +5 -3
- data/lib/action_pack/version.rb +3 -1
- metadata +56 -39
- 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/journey/backwards.rb +0 -5
- data/lib/action_dispatch/journey/router/strexp.rb +0 -27
- data/lib/action_dispatch/middleware/params_parser.rb +0 -60
- 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,4 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AbstractController
|
4
|
+
# = Abstract Controller Callbacks
|
5
|
+
#
|
6
|
+
# Abstract Controller provides hooks during the life cycle of a controller action.
|
7
|
+
# Callbacks allow you to trigger logic during this cycle. Available callbacks are:
|
8
|
+
#
|
9
|
+
# * <tt>after_action</tt>
|
10
|
+
# * <tt>append_after_action</tt>
|
11
|
+
# * <tt>append_around_action</tt>
|
12
|
+
# * <tt>append_before_action</tt>
|
13
|
+
# * <tt>around_action</tt>
|
14
|
+
# * <tt>before_action</tt>
|
15
|
+
# * <tt>prepend_after_action</tt>
|
16
|
+
# * <tt>prepend_around_action</tt>
|
17
|
+
# * <tt>prepend_before_action</tt>
|
18
|
+
# * <tt>skip_after_action</tt>
|
19
|
+
# * <tt>skip_around_action</tt>
|
20
|
+
# * <tt>skip_before_action</tt>
|
21
|
+
#
|
22
|
+
# NOTE: Calling the same callback multiple times will overwrite previous callback definitions.
|
23
|
+
#
|
2
24
|
module Callbacks
|
3
25
|
extend ActiveSupport::Concern
|
4
26
|
|
@@ -9,12 +31,12 @@ module AbstractController
|
|
9
31
|
|
10
32
|
included do
|
11
33
|
define_callbacks :process_action,
|
12
|
-
terminator: ->(controller,
|
34
|
+
terminator: ->(controller, result_lambda) { result_lambda.call; controller.performed? },
|
13
35
|
skip_after_callbacks_if_terminated: true
|
14
36
|
end
|
15
37
|
|
16
|
-
# Override AbstractController::Base
|
17
|
-
# process_action callbacks around the normal behavior.
|
38
|
+
# Override <tt>AbstractController::Base#process_action</tt> to run the
|
39
|
+
# <tt>process_action</tt> callbacks around the normal behavior.
|
18
40
|
def process_action(*args)
|
19
41
|
run_callbacks(:process_action) do
|
20
42
|
super
|
@@ -22,14 +44,25 @@ module AbstractController
|
|
22
44
|
end
|
23
45
|
|
24
46
|
module ClassMethods
|
25
|
-
# If
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
47
|
+
# If +:only+ or +:except+ are used, convert the options into the
|
48
|
+
# +:if+ and +:unless+ options of ActiveSupport::Callbacks.
|
49
|
+
#
|
50
|
+
# The basic idea is that <tt>:only => :index</tt> gets converted to
|
51
|
+
# <tt>:if => proc {|c| c.action_name == "index" }</tt>.
|
52
|
+
#
|
53
|
+
# Note that <tt>:only</tt> has priority over <tt>:if</tt> in case they
|
54
|
+
# are used together.
|
55
|
+
#
|
56
|
+
# only: :index, if: -> { true } # the :if option will be ignored.
|
57
|
+
#
|
58
|
+
# Note that <tt>:if</tt> has priority over <tt>:except</tt> in case they
|
59
|
+
# are used together.
|
60
|
+
#
|
61
|
+
# except: :index, if: -> { true } # the :except option will be ignored.
|
29
62
|
#
|
30
63
|
# ==== Options
|
31
|
-
# * <tt>only</tt> - The callback should be run only for this action
|
32
|
-
# * <tt>except</tt> - The callback should be run for all actions except this action
|
64
|
+
# * <tt>only</tt> - The callback should be run only for this action.
|
65
|
+
# * <tt>except</tt> - The callback should be run for all actions except this action.
|
33
66
|
def _normalize_callback_options(options)
|
34
67
|
_normalize_callback_option(options, :only, :if)
|
35
68
|
_normalize_callback_option(options, :except, :unless)
|
@@ -37,25 +70,12 @@ module AbstractController
|
|
37
70
|
|
38
71
|
def _normalize_callback_option(options, from, to) # :nodoc:
|
39
72
|
if from = options[from]
|
40
|
-
|
73
|
+
_from = Array(from).map(&:to_s).to_set
|
74
|
+
from = proc { |c| _from.include? c.action_name }
|
41
75
|
options[to] = Array(options[to]).unshift(from)
|
42
76
|
end
|
43
77
|
end
|
44
78
|
|
45
|
-
# Skip before, after, and around action callbacks matching any of the names.
|
46
|
-
#
|
47
|
-
# ==== Parameters
|
48
|
-
# * <tt>names</tt> - A list of valid names that could be used for
|
49
|
-
# callbacks. Note that skipping uses Ruby equality, so it's
|
50
|
-
# impossible to skip a callback defined using an anonymous proc
|
51
|
-
# using #skip_action_callback
|
52
|
-
def skip_action_callback(*names)
|
53
|
-
skip_before_action(*names)
|
54
|
-
skip_after_action(*names)
|
55
|
-
skip_around_action(*names)
|
56
|
-
end
|
57
|
-
alias_method :skip_filter, :skip_action_callback
|
58
|
-
|
59
79
|
# Take callback names and an optional callback proc, normalize them,
|
60
80
|
# then call the block with each callback. This allows us to abstract
|
61
81
|
# the normalization across several methods that use it.
|
@@ -66,8 +86,8 @@ module AbstractController
|
|
66
86
|
# * <tt>block</tt> - A proc that should be added to the callbacks.
|
67
87
|
#
|
68
88
|
# ==== Block Parameters
|
69
|
-
# * <tt>name</tt> - The callback to be added
|
70
|
-
# * <tt>options</tt> - A hash of options to be used when adding the callback
|
89
|
+
# * <tt>name</tt> - The callback to be added.
|
90
|
+
# * <tt>options</tt> - A hash of options to be used when adding the callback.
|
71
91
|
def _insert_callbacks(callbacks, block = nil)
|
72
92
|
options = callbacks.extract_options!
|
73
93
|
_normalize_callback_options(options)
|
@@ -169,14 +189,12 @@ module AbstractController
|
|
169
189
|
set_callback(:process_action, callback, name, options)
|
170
190
|
end
|
171
191
|
end
|
172
|
-
alias_method :"#{callback}_filter", :"#{callback}_action"
|
173
192
|
|
174
193
|
define_method "prepend_#{callback}_action" do |*names, &blk|
|
175
194
|
_insert_callbacks(names, blk) do |name, options|
|
176
|
-
set_callback(:process_action, callback, name, options.merge(:
|
195
|
+
set_callback(:process_action, callback, name, options.merge(prepend: true))
|
177
196
|
end
|
178
197
|
end
|
179
|
-
alias_method :"prepend_#{callback}_filter", :"prepend_#{callback}_action"
|
180
198
|
|
181
199
|
# Skip a before, after or around callback. See _insert_callbacks
|
182
200
|
# for details on the allowed parameters.
|
@@ -185,11 +203,9 @@ module AbstractController
|
|
185
203
|
skip_callback(:process_action, callback, name, options)
|
186
204
|
end
|
187
205
|
end
|
188
|
-
alias_method :"skip_#{callback}_filter", :"skip_#{callback}_action"
|
189
206
|
|
190
207
|
# *_action is the same as append_*_action
|
191
208
|
alias_method :"append_#{callback}_action", :"#{callback}_action"
|
192
|
-
alias_method :"append_#{callback}_filter", :"#{callback}_action"
|
193
209
|
end
|
194
210
|
end
|
195
211
|
end
|
@@ -1,14 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "action_dispatch/http/mime_type"
|
2
4
|
|
3
5
|
module AbstractController
|
4
6
|
module Collector
|
5
7
|
def self.generate_method_for_mime(mime)
|
6
8
|
sym = mime.is_a?(Symbol) ? mime : mime.to_sym
|
7
|
-
const = sym.upcase
|
8
9
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
9
|
-
def #{sym}(*args, &block)
|
10
|
-
custom(Mime
|
11
|
-
end
|
10
|
+
def #{sym}(*args, &block)
|
11
|
+
custom(Mime[:#{sym}], *args, &block)
|
12
|
+
end
|
12
13
|
RUBY
|
13
14
|
end
|
14
15
|
|
@@ -17,15 +18,13 @@ module AbstractController
|
|
17
18
|
end
|
18
19
|
|
19
20
|
Mime::Type.register_callback do |mime|
|
20
|
-
generate_method_for_mime(mime) unless
|
21
|
+
generate_method_for_mime(mime) unless instance_methods.include?(mime.to_sym)
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
|
+
private
|
24
25
|
|
25
26
|
def method_missing(symbol, &block)
|
26
|
-
|
27
|
-
|
28
|
-
unless Mime.const_defined?(const_name)
|
27
|
+
unless mime_constant = Mime[symbol]
|
29
28
|
raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
|
30
29
|
"http://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \
|
31
30
|
"If you meant to respond to a variant like :tablet or :phone, not a custom format, " \
|
@@ -33,8 +32,6 @@ module AbstractController
|
|
33
32
|
"format.html { |html| html.tablet { ... } }"
|
34
33
|
end
|
35
34
|
|
36
|
-
mime_constant = Mime.const_get(const_name)
|
37
|
-
|
38
35
|
if Mime::SET.include?(mime_constant)
|
39
36
|
AbstractController::Collector.generate_method_for_mime(mime_constant)
|
40
37
|
send(symbol, &block)
|
@@ -1,15 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/dependencies"
|
2
4
|
|
3
5
|
module AbstractController
|
4
6
|
module Helpers
|
5
7
|
extend ActiveSupport::Concern
|
6
8
|
|
7
9
|
included do
|
8
|
-
class_attribute :_helpers
|
9
|
-
|
10
|
-
|
11
|
-
class_attribute :_helper_methods
|
12
|
-
self._helper_methods = Array.new
|
10
|
+
class_attribute :_helpers, default: Module.new
|
11
|
+
class_attribute :_helper_methods, default: Array.new
|
13
12
|
end
|
14
13
|
|
15
14
|
class MissingHelperError < LoadError
|
@@ -38,7 +37,8 @@ module AbstractController
|
|
38
37
|
end
|
39
38
|
|
40
39
|
# Declare a controller method as a helper. For example, the following
|
41
|
-
# makes the +current_user+ controller
|
40
|
+
# makes the +current_user+ and +logged_in?+ controller methods available
|
41
|
+
# to the view:
|
42
42
|
# class ApplicationController < ActionController::Base
|
43
43
|
# helper_method :current_user, :logged_in?
|
44
44
|
#
|
@@ -170,25 +170,25 @@ module AbstractController
|
|
170
170
|
end
|
171
171
|
|
172
172
|
private
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
173
|
+
# Makes all the (instance) methods in the helper module available to templates
|
174
|
+
# rendered through this controller.
|
175
|
+
#
|
176
|
+
# ==== Parameters
|
177
|
+
# * <tt>module</tt> - The module to include into the current helper module
|
178
|
+
# for the class
|
179
|
+
def add_template_helper(mod)
|
180
|
+
_helpers.module_eval { include mod }
|
181
|
+
end
|
182
182
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
183
|
+
def default_helper_module!
|
184
|
+
module_name = name.sub(/Controller$/, "".freeze)
|
185
|
+
module_path = module_name.underscore
|
186
|
+
helper module_path
|
187
|
+
rescue LoadError => e
|
188
|
+
raise e unless e.is_missing? "helpers/#{module_path}_helper"
|
189
|
+
rescue NameError => e
|
190
|
+
raise e unless e.missing_name? "#{module_name}Helper"
|
191
|
+
end
|
192
192
|
end
|
193
193
|
end
|
194
194
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AbstractController
|
2
4
|
module Railties
|
3
5
|
module RoutesHelpers
|
@@ -6,9 +8,9 @@ module AbstractController
|
|
6
8
|
define_method(:inherited) do |klass|
|
7
9
|
super(klass)
|
8
10
|
if namespace = klass.parents.detect { |m| m.respond_to?(:railtie_routes_url_helpers) }
|
9
|
-
klass.
|
11
|
+
klass.include(namespace.railtie_routes_url_helpers(include_path_helpers))
|
10
12
|
else
|
11
|
-
klass.
|
13
|
+
klass.include(routes.url_helpers(include_path_helpers))
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "abstract_controller/error"
|
4
|
+
require "action_view"
|
5
|
+
require "action_view/view_paths"
|
6
|
+
require "set"
|
6
7
|
|
7
8
|
module AbstractController
|
8
9
|
class DoubleRenderError < Error
|
@@ -17,52 +18,49 @@ module AbstractController
|
|
17
18
|
extend ActiveSupport::Concern
|
18
19
|
include ActionView::ViewPaths
|
19
20
|
|
20
|
-
#
|
21
|
-
# sticks the result in self.response_body
|
22
|
-
# :api: public
|
21
|
+
# Normalizes arguments, options and then delegates render_to_body and
|
22
|
+
# sticks the result in <tt>self.response_body</tt>.
|
23
23
|
def render(*args, &block)
|
24
24
|
options = _normalize_render(*args, &block)
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
28
32
|
end
|
29
33
|
|
30
34
|
# Raw rendering of a template to a string.
|
31
35
|
#
|
32
36
|
# It is similar to render, except that it does not
|
33
|
-
# set the response_body and it should be guaranteed
|
37
|
+
# set the +response_body+ and it should be guaranteed
|
34
38
|
# to always return a string.
|
35
39
|
#
|
36
|
-
# If a component extends the semantics of response_body
|
37
|
-
# (as
|
40
|
+
# If a component extends the semantics of +response_body+
|
41
|
+
# (as ActionController extends it to be anything that
|
38
42
|
# responds to the method each), this method needs to be
|
39
43
|
# overridden in order to still return a string.
|
40
|
-
# :api: plugin
|
41
44
|
def render_to_string(*args, &block)
|
42
45
|
options = _normalize_render(*args, &block)
|
43
46
|
render_to_body(options)
|
44
47
|
end
|
45
48
|
|
46
49
|
# Performs the actual template rendering.
|
47
|
-
# :api: public
|
48
50
|
def render_to_body(options = {})
|
49
51
|
end
|
50
52
|
|
51
|
-
# Returns Content-Type of rendered content
|
52
|
-
# :api: public
|
53
|
+
# Returns Content-Type of rendered content.
|
53
54
|
def rendered_format
|
54
|
-
Mime
|
55
|
+
Mime[:text]
|
55
56
|
end
|
56
57
|
|
57
|
-
DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %
|
58
|
-
@_action_name @_response_body @_formats @_prefixes
|
59
|
-
|
60
|
-
@_routes @_db_runtime
|
61
|
-
).map(&:to_sym)
|
58
|
+
DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set.new %i(
|
59
|
+
@_action_name @_response_body @_formats @_prefixes
|
60
|
+
)
|
62
61
|
|
63
62
|
# This method should return a hash with assigns.
|
64
63
|
# You can overwrite this configuration per controller.
|
65
|
-
# :api: public
|
66
64
|
def view_assigns
|
67
65
|
protected_vars = _protected_ivars
|
68
66
|
variables = instance_variables
|
@@ -73,10 +71,11 @@ module AbstractController
|
|
73
71
|
}
|
74
72
|
end
|
75
73
|
|
76
|
-
|
77
|
-
#
|
78
|
-
# :
|
79
|
-
|
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:
|
80
79
|
if action.respond_to?(:permitted?)
|
81
80
|
if action.permitted?
|
82
81
|
action
|
@@ -91,30 +90,32 @@ module AbstractController
|
|
91
90
|
end
|
92
91
|
|
93
92
|
# Normalize options.
|
94
|
-
# :
|
95
|
-
def _normalize_options(options)
|
93
|
+
def _normalize_options(options) # :doc:
|
96
94
|
options
|
97
95
|
end
|
98
96
|
|
99
97
|
# Process extra options.
|
100
|
-
# :
|
101
|
-
def _process_options(options)
|
98
|
+
def _process_options(options) # :doc:
|
102
99
|
options
|
103
100
|
end
|
104
101
|
|
105
102
|
# Process the rendered format.
|
106
|
-
# :
|
107
|
-
|
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:
|
108
113
|
end
|
109
114
|
|
110
115
|
# Normalize args and options.
|
111
|
-
# :
|
112
|
-
def _normalize_render(*args, &block)
|
116
|
+
def _normalize_render(*args, &block) # :nodoc:
|
113
117
|
options = _normalize_args(*args, &block)
|
114
|
-
|
115
|
-
if defined?(request) && request && request.variant.present?
|
116
|
-
options[:variant] = request.variant
|
117
|
-
end
|
118
|
+
_process_variant(options)
|
118
119
|
_normalize_options(options)
|
119
120
|
options
|
120
121
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module AbstractController
|
2
4
|
module Translation
|
3
5
|
# Delegates to <tt>I18n.translate</tt>. Also aliased as <tt>t</tt>.
|
@@ -8,14 +10,15 @@ module AbstractController
|
|
8
10
|
# <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
|
9
11
|
# to translate many keys within the same controller / action and gives you a
|
10
12
|
# simple framework for scoping them consistently.
|
11
|
-
def translate(
|
12
|
-
key
|
13
|
-
|
14
|
-
|
15
|
-
|
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}"
|
16
20
|
end
|
17
|
-
|
18
|
-
I18n.translate(*args)
|
21
|
+
I18n.translate(key, options)
|
19
22
|
end
|
20
23
|
alias :t :translate
|
21
24
|
|