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.

Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +311 -0
  3. data/MIT-LICENSE +21 -0
  4. data/README.rdoc +58 -0
  5. data/lib/abstract_controller.rb +27 -0
  6. data/lib/abstract_controller/asset_paths.rb +12 -0
  7. data/lib/abstract_controller/base.rb +267 -0
  8. data/lib/abstract_controller/caching.rb +66 -0
  9. data/lib/abstract_controller/caching/fragments.rb +150 -0
  10. data/lib/abstract_controller/callbacks.rb +224 -0
  11. data/lib/abstract_controller/collector.rb +43 -0
  12. data/lib/abstract_controller/error.rb +6 -0
  13. data/lib/abstract_controller/helpers.rb +194 -0
  14. data/lib/abstract_controller/logger.rb +14 -0
  15. data/lib/abstract_controller/railties/routes_helpers.rb +20 -0
  16. data/lib/abstract_controller/rendering.rb +127 -0
  17. data/lib/abstract_controller/translation.rb +32 -0
  18. data/lib/abstract_controller/url_for.rb +35 -0
  19. data/lib/action_controller.rb +67 -0
  20. data/lib/action_controller/api.rb +150 -0
  21. data/lib/action_controller/api/api_rendering.rb +16 -0
  22. data/lib/action_controller/base.rb +271 -0
  23. data/lib/action_controller/caching.rb +46 -0
  24. data/lib/action_controller/form_builder.rb +50 -0
  25. data/lib/action_controller/log_subscriber.rb +81 -0
  26. data/lib/action_controller/metal.rb +256 -0
  27. data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
  28. data/lib/action_controller/metal/conditional_get.rb +280 -0
  29. data/lib/action_controller/metal/content_security_policy.rb +52 -0
  30. data/lib/action_controller/metal/cookies.rb +16 -0
  31. data/lib/action_controller/metal/data_streaming.rb +151 -0
  32. data/lib/action_controller/metal/default_headers.rb +17 -0
  33. data/lib/action_controller/metal/etag_with_flash.rb +18 -0
  34. data/lib/action_controller/metal/etag_with_template_digest.rb +57 -0
  35. data/lib/action_controller/metal/exceptions.rb +74 -0
  36. data/lib/action_controller/metal/flash.rb +61 -0
  37. data/lib/action_controller/metal/force_ssl.rb +58 -0
  38. data/lib/action_controller/metal/head.rb +60 -0
  39. data/lib/action_controller/metal/helpers.rb +122 -0
  40. data/lib/action_controller/metal/http_authentication.rb +518 -0
  41. data/lib/action_controller/metal/implicit_render.rb +63 -0
  42. data/lib/action_controller/metal/instrumentation.rb +105 -0
  43. data/lib/action_controller/metal/live.rb +314 -0
  44. data/lib/action_controller/metal/mime_responds.rb +324 -0
  45. data/lib/action_controller/metal/parameter_encoding.rb +51 -0
  46. data/lib/action_controller/metal/params_wrapper.rb +297 -0
  47. data/lib/action_controller/metal/redirecting.rb +133 -0
  48. data/lib/action_controller/metal/renderers.rb +181 -0
  49. data/lib/action_controller/metal/rendering.rb +122 -0
  50. data/lib/action_controller/metal/request_forgery_protection.rb +456 -0
  51. data/lib/action_controller/metal/rescue.rb +28 -0
  52. data/lib/action_controller/metal/streaming.rb +223 -0
  53. data/lib/action_controller/metal/strong_parameters.rb +1105 -0
  54. data/lib/action_controller/metal/testing.rb +16 -0
  55. data/lib/action_controller/metal/url_for.rb +58 -0
  56. data/lib/action_controller/railtie.rb +89 -0
  57. data/lib/action_controller/railties/helpers.rb +24 -0
  58. data/lib/action_controller/renderer.rb +130 -0
  59. data/lib/action_controller/template_assertions.rb +11 -0
  60. data/lib/action_controller/test_case.rb +626 -0
  61. data/lib/action_dispatch.rb +114 -0
  62. data/lib/action_dispatch/http/cache.rb +226 -0
  63. data/lib/action_dispatch/http/content_disposition.rb +45 -0
  64. data/lib/action_dispatch/http/content_security_policy.rb +284 -0
  65. data/lib/action_dispatch/http/filter_parameters.rb +86 -0
  66. data/lib/action_dispatch/http/filter_redirect.rb +37 -0
  67. data/lib/action_dispatch/http/headers.rb +132 -0
  68. data/lib/action_dispatch/http/mime_negotiation.rb +177 -0
  69. data/lib/action_dispatch/http/mime_type.rb +350 -0
  70. data/lib/action_dispatch/http/mime_types.rb +50 -0
  71. data/lib/action_dispatch/http/parameter_filter.rb +12 -0
  72. data/lib/action_dispatch/http/parameters.rb +136 -0
  73. data/lib/action_dispatch/http/rack_cache.rb +63 -0
  74. data/lib/action_dispatch/http/request.rb +427 -0
  75. data/lib/action_dispatch/http/response.rb +534 -0
  76. data/lib/action_dispatch/http/upload.rb +92 -0
  77. data/lib/action_dispatch/http/url.rb +350 -0
  78. data/lib/action_dispatch/journey.rb +7 -0
  79. data/lib/action_dispatch/journey/formatter.rb +189 -0
  80. data/lib/action_dispatch/journey/gtg/builder.rb +164 -0
  81. data/lib/action_dispatch/journey/gtg/simulator.rb +41 -0
  82. data/lib/action_dispatch/journey/gtg/transition_table.rb +158 -0
  83. data/lib/action_dispatch/journey/nfa/builder.rb +78 -0
  84. data/lib/action_dispatch/journey/nfa/dot.rb +36 -0
  85. data/lib/action_dispatch/journey/nfa/simulator.rb +47 -0
  86. data/lib/action_dispatch/journey/nfa/transition_table.rb +120 -0
  87. data/lib/action_dispatch/journey/nodes/node.rb +141 -0
  88. data/lib/action_dispatch/journey/parser.rb +199 -0
  89. data/lib/action_dispatch/journey/parser.y +50 -0
  90. data/lib/action_dispatch/journey/parser_extras.rb +31 -0
  91. data/lib/action_dispatch/journey/path/pattern.rb +203 -0
  92. data/lib/action_dispatch/journey/route.rb +204 -0
  93. data/lib/action_dispatch/journey/router.rb +153 -0
  94. data/lib/action_dispatch/journey/router/utils.rb +102 -0
  95. data/lib/action_dispatch/journey/routes.rb +81 -0
  96. data/lib/action_dispatch/journey/scanner.rb +71 -0
  97. data/lib/action_dispatch/journey/visitors.rb +268 -0
  98. data/lib/action_dispatch/journey/visualizer/fsm.css +30 -0
  99. data/lib/action_dispatch/journey/visualizer/fsm.js +134 -0
  100. data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
  101. data/lib/action_dispatch/middleware/actionable_exceptions.rb +39 -0
  102. data/lib/action_dispatch/middleware/callbacks.rb +34 -0
  103. data/lib/action_dispatch/middleware/cookies.rb +663 -0
  104. data/lib/action_dispatch/middleware/debug_exceptions.rb +185 -0
  105. data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
  106. data/lib/action_dispatch/middleware/debug_view.rb +68 -0
  107. data/lib/action_dispatch/middleware/exception_wrapper.rb +181 -0
  108. data/lib/action_dispatch/middleware/executor.rb +21 -0
  109. data/lib/action_dispatch/middleware/flash.rb +300 -0
  110. data/lib/action_dispatch/middleware/host_authorization.rb +103 -0
  111. data/lib/action_dispatch/middleware/public_exceptions.rb +61 -0
  112. data/lib/action_dispatch/middleware/reloader.rb +12 -0
  113. data/lib/action_dispatch/middleware/remote_ip.rb +181 -0
  114. data/lib/action_dispatch/middleware/request_id.rb +43 -0
  115. data/lib/action_dispatch/middleware/session/abstract_store.rb +92 -0
  116. data/lib/action_dispatch/middleware/session/cache_store.rb +54 -0
  117. data/lib/action_dispatch/middleware/session/cookie_store.rb +113 -0
  118. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +28 -0
  119. data/lib/action_dispatch/middleware/show_exceptions.rb +62 -0
  120. data/lib/action_dispatch/middleware/ssl.rb +150 -0
  121. data/lib/action_dispatch/middleware/stack.rb +148 -0
  122. data/lib/action_dispatch/middleware/static.rb +129 -0
  123. data/lib/action_dispatch/middleware/templates/rescues/_actions.html.erb +13 -0
  124. data/lib/action_dispatch/middleware/templates/rescues/_actions.text.erb +0 -0
  125. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +24 -0
  126. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
  127. data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +29 -0
  128. data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
  129. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +62 -0
  130. data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
  131. data/lib/action_dispatch/middleware/templates/rescues/blocked_host.html.erb +7 -0
  132. data/lib/action_dispatch/middleware/templates/rescues/blocked_host.text.erb +5 -0
  133. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +38 -0
  134. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
  135. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +24 -0
  136. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +15 -0
  137. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +165 -0
  138. data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.html.erb +19 -0
  139. data/lib/action_dispatch/middleware/templates/rescues/missing_exact_template.text.erb +3 -0
  140. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
  141. data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
  142. data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +32 -0
  143. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
  144. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
  145. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
  146. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +6 -0
  147. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
  148. data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +16 -0
  149. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +203 -0
  150. data/lib/action_dispatch/railtie.rb +58 -0
  151. data/lib/action_dispatch/request/session.rb +242 -0
  152. data/lib/action_dispatch/request/utils.rb +78 -0
  153. data/lib/action_dispatch/routing.rb +261 -0
  154. data/lib/action_dispatch/routing/endpoint.rb +17 -0
  155. data/lib/action_dispatch/routing/inspector.rb +274 -0
  156. data/lib/action_dispatch/routing/mapper.rb +2289 -0
  157. data/lib/action_dispatch/routing/polymorphic_routes.rb +351 -0
  158. data/lib/action_dispatch/routing/redirection.rb +201 -0
  159. data/lib/action_dispatch/routing/route_set.rb +887 -0
  160. data/lib/action_dispatch/routing/routes_proxy.rb +69 -0
  161. data/lib/action_dispatch/routing/url_for.rb +237 -0
  162. data/lib/action_dispatch/system_test_case.rb +168 -0
  163. data/lib/action_dispatch/system_testing/browser.rb +80 -0
  164. data/lib/action_dispatch/system_testing/driver.rb +68 -0
  165. data/lib/action_dispatch/system_testing/server.rb +31 -0
  166. data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +97 -0
  167. data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +33 -0
  168. data/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb +26 -0
  169. data/lib/action_dispatch/testing/assertion_response.rb +47 -0
  170. data/lib/action_dispatch/testing/assertions.rb +24 -0
  171. data/lib/action_dispatch/testing/assertions/response.rb +106 -0
  172. data/lib/action_dispatch/testing/assertions/routing.rb +234 -0
  173. data/lib/action_dispatch/testing/integration.rb +659 -0
  174. data/lib/action_dispatch/testing/request_encoder.rb +55 -0
  175. data/lib/action_dispatch/testing/test_process.rb +50 -0
  176. data/lib/action_dispatch/testing/test_request.rb +71 -0
  177. data/lib/action_dispatch/testing/test_response.rb +25 -0
  178. data/lib/action_pack.rb +26 -0
  179. data/lib/action_pack/gem_version.rb +17 -0
  180. data/lib/action_pack/version.rb +10 -0
  181. metadata +329 -0
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController #:nodoc:
4
+ module ContentSecurityPolicy
5
+ # TODO: Documentation
6
+ extend ActiveSupport::Concern
7
+
8
+ include AbstractController::Helpers
9
+ include AbstractController::Callbacks
10
+
11
+ included do
12
+ helper_method :content_security_policy?
13
+ helper_method :content_security_policy_nonce
14
+ end
15
+
16
+ module ClassMethods
17
+ def content_security_policy(enabled = true, **options, &block)
18
+ before_action(options) do
19
+ if block_given?
20
+ policy = current_content_security_policy
21
+ yield policy
22
+ request.content_security_policy = policy
23
+ end
24
+
25
+ unless enabled
26
+ request.content_security_policy = nil
27
+ end
28
+ end
29
+ end
30
+
31
+ def content_security_policy_report_only(report_only = true, **options)
32
+ before_action(options) do
33
+ request.content_security_policy_report_only = report_only
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def content_security_policy?
41
+ request.content_security_policy
42
+ end
43
+
44
+ def content_security_policy_nonce
45
+ request.content_security_policy_nonce
46
+ end
47
+
48
+ def current_content_security_policy
49
+ request.content_security_policy.try(:clone) || ActionDispatch::ContentSecurityPolicy.new
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController #:nodoc:
4
+ module Cookies
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ helper_method :cookies if defined?(helper_method)
9
+ end
10
+
11
+ private
12
+ def cookies
13
+ request.cookie_jar
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "action_controller/metal/exceptions"
4
+ require "action_dispatch/http/content_disposition"
5
+
6
+ module ActionController #:nodoc:
7
+ # Methods for sending arbitrary data and for streaming files to the browser,
8
+ # instead of rendering.
9
+ module DataStreaming
10
+ extend ActiveSupport::Concern
11
+
12
+ include ActionController::Rendering
13
+
14
+ DEFAULT_SEND_FILE_TYPE = "application/octet-stream" #:nodoc:
15
+ DEFAULT_SEND_FILE_DISPOSITION = "attachment" #:nodoc:
16
+
17
+ private
18
+ # Sends the file. This uses a server-appropriate method (such as X-Sendfile)
19
+ # via the Rack::Sendfile middleware. The header to use is set via
20
+ # +config.action_dispatch.x_sendfile_header+.
21
+ # Your server can also configure this for you by setting the X-Sendfile-Type header.
22
+ #
23
+ # Be careful to sanitize the path parameter if it is coming from a web
24
+ # page. <tt>send_file(params[:path])</tt> allows a malicious user to
25
+ # download any file on your server.
26
+ #
27
+ # Options:
28
+ # * <tt>:filename</tt> - suggests a filename for the browser to use.
29
+ # Defaults to <tt>File.basename(path)</tt>.
30
+ # * <tt>:type</tt> - specifies an HTTP content type.
31
+ # You can specify either a string or a symbol for a registered type with <tt>Mime::Type.register</tt>, for example :json.
32
+ # If omitted, the type will be inferred from the file extension specified in <tt>:filename</tt>.
33
+ # If no content type is registered for the extension, the default type 'application/octet-stream' will be used.
34
+ # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
35
+ # Valid values are 'inline' and 'attachment' (default).
36
+ # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to 200.
37
+ # * <tt>:url_based_filename</tt> - set to +true+ if you want the browser to guess the filename from
38
+ # the URL, which is necessary for i18n filenames on certain browsers
39
+ # (setting <tt>:filename</tt> overrides this option).
40
+ #
41
+ # The default Content-Type and Content-Disposition headers are
42
+ # set to download arbitrary binary files in as many browsers as
43
+ # possible. IE versions 4, 5, 5.5, and 6 are all known to have
44
+ # a variety of quirks (especially when downloading over SSL).
45
+ #
46
+ # Simple download:
47
+ #
48
+ # send_file '/path/to.zip'
49
+ #
50
+ # Show a JPEG in the browser:
51
+ #
52
+ # send_file '/path/to.jpeg', type: 'image/jpeg', disposition: 'inline'
53
+ #
54
+ # Show a 404 page in the browser:
55
+ #
56
+ # send_file '/path/to/404.html', type: 'text/html; charset=utf-8', status: 404
57
+ #
58
+ # Read about the other Content-* HTTP headers if you'd like to
59
+ # provide the user with more information (such as Content-Description) in
60
+ # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.11.
61
+ #
62
+ # Also be aware that the document may be cached by proxies and browsers.
63
+ # The Pragma and Cache-Control headers declare how the file may be cached
64
+ # by intermediaries. They default to require clients to validate with
65
+ # the server before releasing cached responses. See
66
+ # https://www.mnot.net/cache_docs/ for an overview of web caching and
67
+ # https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
68
+ # for the Cache-Control header spec.
69
+ def send_file(path, options = {}) #:doc:
70
+ raise MissingFile, "Cannot read file #{path}" unless File.file?(path) && File.readable?(path)
71
+
72
+ options[:filename] ||= File.basename(path) unless options[:url_based_filename]
73
+ send_file_headers! options
74
+
75
+ self.status = options[:status] || 200
76
+ self.content_type = options[:content_type] if options.key?(:content_type)
77
+ response.send_file path
78
+ end
79
+
80
+ # Sends the given binary data to the browser. This method is similar to
81
+ # <tt>render plain: data</tt>, but also allows you to specify whether
82
+ # the browser should display the response as a file attachment (i.e. in a
83
+ # download dialog) or as inline data. You may also set the content type,
84
+ # the file name, and other things.
85
+ #
86
+ # Options:
87
+ # * <tt>:filename</tt> - suggests a filename for the browser to use.
88
+ # * <tt>:type</tt> - specifies an HTTP content type. Defaults to 'application/octet-stream'.
89
+ # You can specify either a string or a symbol for a registered type with <tt>Mime::Type.register</tt>, for example :json.
90
+ # If omitted, type will be inferred from the file extension specified in <tt>:filename</tt>.
91
+ # If no content type is registered for the extension, the default type 'application/octet-stream' will be used.
92
+ # * <tt>:disposition</tt> - specifies whether the file will be shown inline or downloaded.
93
+ # Valid values are 'inline' and 'attachment' (default).
94
+ # * <tt>:status</tt> - specifies the status code to send with the response. Defaults to 200.
95
+ #
96
+ # Generic data download:
97
+ #
98
+ # send_data buffer
99
+ #
100
+ # Download a dynamically-generated tarball:
101
+ #
102
+ # send_data generate_tgz('dir'), filename: 'dir.tgz'
103
+ #
104
+ # Display an image Active Record in the browser:
105
+ #
106
+ # send_data image.data, type: image.content_type, disposition: 'inline'
107
+ #
108
+ # See +send_file+ for more information on HTTP Content-* headers and caching.
109
+ def send_data(data, options = {}) #:doc:
110
+ send_file_headers! options
111
+ render options.slice(:status, :content_type).merge(body: data)
112
+ end
113
+
114
+ def send_file_headers!(options)
115
+ type_provided = options.has_key?(:type)
116
+
117
+ content_type = options.fetch(:type, DEFAULT_SEND_FILE_TYPE)
118
+ self.content_type = content_type
119
+ response.sending_file = true
120
+
121
+ raise ArgumentError, ":type option required" if content_type.nil?
122
+
123
+ if content_type.is_a?(Symbol)
124
+ extension = Mime[content_type]
125
+ raise ArgumentError, "Unknown MIME type #{options[:type]}" unless extension
126
+ self.content_type = extension
127
+ else
128
+ if !type_provided && options[:filename]
129
+ # If type wasn't provided, try guessing from file extension.
130
+ content_type = Mime::Type.lookup_by_extension(File.extname(options[:filename]).downcase.delete(".")) || content_type
131
+ end
132
+ self.content_type = content_type
133
+ end
134
+
135
+ disposition = options.fetch(:disposition, DEFAULT_SEND_FILE_DISPOSITION)
136
+ if disposition
137
+ headers["Content-Disposition"] = ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: options[:filename])
138
+ end
139
+
140
+ headers["Content-Transfer-Encoding"] = "binary"
141
+
142
+ # Fix a problem with IE 6.0 on opening downloaded files:
143
+ # If Cache-Control: no-cache is set (which Rails does by default),
144
+ # IE removes the file it just downloaded from its cache immediately
145
+ # after it displays the "open/save" dialog, which means that if you
146
+ # hit "open" the file isn't there anymore when the application that
147
+ # is called for handling the download is run, so let's workaround that
148
+ response.cache_control[:public] ||= false
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ # Allows configuring default headers that will be automatically merged into
5
+ # each response.
6
+ module DefaultHeaders
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def make_response!(request)
11
+ ActionDispatch::Response.create.tap do |res|
12
+ res.request = request
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ # When you're using the flash, it's generally used as a conditional on the view.
5
+ # This means the content of the view depends on the flash. Which in turn means
6
+ # that the ETag for a response should be computed with the content of the flash
7
+ # in mind. This does that by including the content of the flash as a component
8
+ # in the ETag that's generated for a response.
9
+ module EtagWithFlash
10
+ extend ActiveSupport::Concern
11
+
12
+ include ActionController::ConditionalGet
13
+
14
+ included do
15
+ etag { flash unless flash.empty? }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ # When our views change, they should bubble up into HTTP cache freshness
5
+ # and bust browser caches. So the template digest for the current action
6
+ # is automatically included in the ETag.
7
+ #
8
+ # Enabled by default for apps that use Action View. Disable by setting
9
+ #
10
+ # config.action_controller.etag_with_template_digest = false
11
+ #
12
+ # Override the template to digest by passing +:template+ to +fresh_when+
13
+ # and +stale?+ calls. For example:
14
+ #
15
+ # # We're going to render widgets/show, not posts/show
16
+ # fresh_when @post, template: 'widgets/show'
17
+ #
18
+ # # We're not going to render a template, so omit it from the ETag.
19
+ # fresh_when @post, template: false
20
+ #
21
+ module EtagWithTemplateDigest
22
+ extend ActiveSupport::Concern
23
+
24
+ include ActionController::ConditionalGet
25
+
26
+ included do
27
+ class_attribute :etag_with_template_digest, default: true
28
+
29
+ ActiveSupport.on_load :action_view, yield: true do
30
+ etag do |options|
31
+ determine_template_etag(options) if etag_with_template_digest
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+ def determine_template_etag(options)
38
+ if template = pick_template_for_etag(options)
39
+ lookup_and_digest_template(template)
40
+ end
41
+ end
42
+
43
+ # Pick the template digest to include in the ETag. If the +:template+ option
44
+ # is present, use the named template. If +:template+ is +nil+ or absent, use
45
+ # the default controller/action template. If +:template+ is false, omit the
46
+ # template digest from the ETag.
47
+ def pick_template_for_etag(options)
48
+ unless options[:template] == false
49
+ options[:template] || "#{controller_path}/#{action_name}"
50
+ end
51
+ end
52
+
53
+ def lookup_and_digest_template(template)
54
+ ActionView::Digestor.digest name: template, format: nil, finder: lookup_context
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ class ActionControllerError < StandardError #:nodoc:
5
+ end
6
+
7
+ class BadRequest < ActionControllerError #:nodoc:
8
+ def initialize(msg = nil)
9
+ super(msg)
10
+ set_backtrace $!.backtrace if $!
11
+ end
12
+ end
13
+
14
+ class RenderError < ActionControllerError #:nodoc:
15
+ end
16
+
17
+ class RoutingError < ActionControllerError #:nodoc:
18
+ attr_reader :failures
19
+ def initialize(message, failures = [])
20
+ super(message)
21
+ @failures = failures
22
+ end
23
+ end
24
+
25
+ class UrlGenerationError < ActionControllerError #:nodoc:
26
+ end
27
+
28
+ class MethodNotAllowed < ActionControllerError #:nodoc:
29
+ def initialize(*allowed_methods)
30
+ super("Only #{allowed_methods.to_sentence(locale: :en)} requests are allowed.")
31
+ end
32
+ end
33
+
34
+ class NotImplemented < MethodNotAllowed #:nodoc:
35
+ end
36
+
37
+ class MissingFile < ActionControllerError #:nodoc:
38
+ end
39
+
40
+ class SessionOverflowError < ActionControllerError #:nodoc:
41
+ DEFAULT_MESSAGE = "Your session data is larger than the data column in which it is to be stored. You must increase the size of your data column if you intend to store large data."
42
+
43
+ def initialize(message = nil)
44
+ super(message || DEFAULT_MESSAGE)
45
+ end
46
+ end
47
+
48
+ class UnknownHttpMethod < ActionControllerError #:nodoc:
49
+ end
50
+
51
+ class UnknownFormat < ActionControllerError #:nodoc:
52
+ end
53
+
54
+ # Raised when a nested respond_to is triggered and the content types of each
55
+ # are incompatible. For example:
56
+ #
57
+ # respond_to do |outer_type|
58
+ # outer_type.js do
59
+ # respond_to do |inner_type|
60
+ # inner_type.html { render body: "HTML" }
61
+ # end
62
+ # end
63
+ # end
64
+ class RespondToMismatchError < ActionControllerError
65
+ DEFAULT_MESSAGE = "respond_to was called multiple times and matched with conflicting formats in this action. Please note that you may only call respond_to and match on a single format per action."
66
+
67
+ def initialize(message = nil)
68
+ super(message || DEFAULT_MESSAGE)
69
+ end
70
+ end
71
+
72
+ class MissingExactTemplate < UnknownFormat #:nodoc:
73
+ end
74
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController #:nodoc:
4
+ module Flash
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :_flash_types, instance_accessor: false, default: []
9
+
10
+ delegate :flash, to: :request
11
+ add_flash_types(:alert, :notice)
12
+ end
13
+
14
+ module ClassMethods
15
+ # Creates new flash types. You can pass as many types as you want to create
16
+ # flash types other than the default <tt>alert</tt> and <tt>notice</tt> in
17
+ # your controllers and views. For instance:
18
+ #
19
+ # # in application_controller.rb
20
+ # class ApplicationController < ActionController::Base
21
+ # add_flash_types :warning
22
+ # end
23
+ #
24
+ # # in your controller
25
+ # redirect_to user_path(@user), warning: "Incomplete profile"
26
+ #
27
+ # # in your view
28
+ # <%= warning %>
29
+ #
30
+ # This method will automatically define a new method for each of the given
31
+ # names, and it will be available in your views.
32
+ def add_flash_types(*types)
33
+ types.each do |type|
34
+ next if _flash_types.include?(type)
35
+
36
+ define_method(type) do
37
+ request.flash[type]
38
+ end
39
+ helper_method(type) if respond_to?(:helper_method)
40
+
41
+ self._flash_types += [type]
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+ def redirect_to(options = {}, response_options_and_flash = {}) #:doc:
48
+ self.class._flash_types.each do |flash_type|
49
+ if type = response_options_and_flash.delete(flash_type)
50
+ flash[flash_type] = type
51
+ end
52
+ end
53
+
54
+ if other_flashes = response_options_and_flash.delete(:flash)
55
+ flash.update(other_flashes)
56
+ end
57
+
58
+ super(options, response_options_and_flash)
59
+ end
60
+ end
61
+ end