actionpack 4.1.16 → 4.2.0.beta1
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 +4 -4
- data/CHANGELOG.md +163 -690
- data/README.rdoc +7 -2
- data/lib/abstract_controller/base.rb +16 -6
- data/lib/abstract_controller/callbacks.rb +28 -51
- data/lib/abstract_controller/helpers.rb +0 -3
- data/lib/abstract_controller/railties/routes_helpers.rb +3 -3
- data/lib/abstract_controller/rendering.rb +1 -7
- data/lib/abstract_controller/url_for.rb +1 -1
- data/lib/action_controller.rb +1 -0
- data/lib/action_controller/base.rb +2 -1
- data/lib/action_controller/caching.rb +1 -1
- data/lib/action_controller/caching/fragments.rb +7 -1
- data/lib/action_controller/log_subscriber.rb +26 -25
- data/lib/action_controller/metal.rb +11 -7
- data/lib/action_controller/metal/conditional_get.rb +31 -6
- data/lib/action_controller/metal/etag_with_template_digest.rb +50 -0
- data/lib/action_controller/metal/force_ssl.rb +1 -1
- data/lib/action_controller/metal/head.rb +2 -0
- data/lib/action_controller/metal/http_authentication.rb +3 -15
- data/lib/action_controller/metal/instrumentation.rb +4 -7
- data/lib/action_controller/metal/live.rb +57 -6
- data/lib/action_controller/metal/mime_responds.rb +17 -227
- data/lib/action_controller/metal/redirecting.rb +14 -8
- data/lib/action_controller/metal/renderers.rb +19 -3
- data/lib/action_controller/metal/rendering.rb +2 -6
- data/lib/action_controller/metal/request_forgery_protection.rb +75 -7
- data/lib/action_controller/metal/streaming.rb +1 -1
- data/lib/action_controller/metal/strong_parameters.rb +111 -11
- data/lib/action_controller/metal/url_for.rb +11 -12
- data/lib/action_controller/model_naming.rb +1 -1
- data/lib/action_controller/railtie.rb +4 -0
- data/lib/action_controller/test_case.rb +87 -75
- data/lib/action_dispatch/http/cache.rb +1 -1
- data/lib/action_dispatch/http/filter_parameters.rb +2 -2
- data/lib/action_dispatch/http/headers.rb +43 -9
- data/lib/action_dispatch/http/mime_negotiation.rb +10 -4
- data/lib/action_dispatch/http/mime_type.rb +2 -16
- data/lib/action_dispatch/http/parameter_filter.rb +1 -1
- data/lib/action_dispatch/http/parameters.rb +11 -26
- data/lib/action_dispatch/http/request.rb +30 -10
- data/lib/action_dispatch/http/response.rb +52 -17
- data/lib/action_dispatch/http/upload.rb +3 -8
- data/lib/action_dispatch/http/url.rb +87 -70
- data/lib/action_dispatch/journey/formatter.rb +18 -17
- data/lib/action_dispatch/journey/gtg/builder.rb +3 -3
- data/lib/action_dispatch/journey/gtg/simulator.rb +10 -7
- data/lib/action_dispatch/journey/gtg/transition_table.rb +18 -26
- data/lib/action_dispatch/journey/nfa/dot.rb +2 -2
- data/lib/action_dispatch/journey/nfa/simulator.rb +1 -1
- data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -5
- data/lib/action_dispatch/journey/nodes/node.rb +4 -0
- data/lib/action_dispatch/journey/parser.rb +52 -60
- data/lib/action_dispatch/journey/parser.y +11 -10
- data/lib/action_dispatch/journey/path/pattern.rb +16 -19
- data/lib/action_dispatch/journey/route.rb +3 -18
- data/lib/action_dispatch/journey/router.rb +34 -65
- data/lib/action_dispatch/journey/router/strexp.rb +9 -6
- data/lib/action_dispatch/journey/routes.rb +0 -4
- data/lib/action_dispatch/journey/visitors.rb +81 -92
- data/lib/action_dispatch/journey/visualizer/index.html.erb +2 -2
- data/lib/action_dispatch/middleware/cookies.rb +27 -31
- data/lib/action_dispatch/middleware/debug_exceptions.rb +32 -3
- data/lib/action_dispatch/middleware/exception_wrapper.rb +19 -17
- data/lib/action_dispatch/middleware/flash.rb +7 -4
- data/lib/action_dispatch/middleware/public_exceptions.rb +13 -8
- data/lib/action_dispatch/middleware/remote_ip.rb +3 -3
- data/lib/action_dispatch/middleware/request_id.rb +1 -1
- data/lib/action_dispatch/middleware/session/cookie_store.rb +1 -1
- data/lib/action_dispatch/middleware/show_exceptions.rb +1 -0
- data/lib/action_dispatch/middleware/static.rb +22 -23
- data/lib/action_dispatch/middleware/templates/rescues/_source.erb +22 -18
- data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +36 -8
- data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +2 -8
- data/lib/action_dispatch/middleware/templates/rescues/{diagnostics.erb → diagnostics.html.erb} +0 -0
- data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
- data/lib/action_dispatch/middleware/templates/rescues/layout.erb +6 -0
- data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -24
- data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +0 -1
- data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +119 -63
- data/lib/action_dispatch/routing/endpoint.rb +10 -0
- data/lib/action_dispatch/routing/inspector.rb +4 -11
- data/lib/action_dispatch/routing/mapper.rb +399 -278
- data/lib/action_dispatch/routing/polymorphic_routes.rb +190 -78
- data/lib/action_dispatch/routing/redirection.rb +10 -12
- data/lib/action_dispatch/routing/route_set.rb +224 -177
- data/lib/action_dispatch/routing/url_for.rb +9 -4
- data/lib/action_dispatch/testing/assertions.rb +11 -7
- data/lib/action_dispatch/testing/assertions/dom.rb +2 -26
- data/lib/action_dispatch/testing/assertions/response.rb +2 -7
- data/lib/action_dispatch/testing/assertions/routing.rb +9 -9
- data/lib/action_dispatch/testing/assertions/selector.rb +2 -429
- data/lib/action_dispatch/testing/assertions/tag.rb +2 -134
- data/lib/action_dispatch/testing/integration.rb +15 -18
- data/lib/action_dispatch/testing/test_request.rb +1 -1
- data/lib/action_dispatch/testing/test_response.rb +5 -1
- data/lib/action_pack/gem_version.rb +3 -3
- metadata +57 -15
- data/lib/action_controller/metal/responder.rb +0 -297
@@ -41,6 +41,11 @@ module ActionController
|
|
41
41
|
# * <tt>:last_modified</tt>.
|
42
42
|
# * <tt>:public</tt> By default the Cache-Control header is private, set this to
|
43
43
|
# +true+ if you want your application to be cachable by other devices (proxy caches).
|
44
|
+
# * <tt>:template</tt> By default, the template digest for the current
|
45
|
+
# controller/action is included in ETags. If the action renders a
|
46
|
+
# different template, you can include its digest instead. If the action
|
47
|
+
# doesn't render a template at all, you can pass <tt>template: false</tt>
|
48
|
+
# to skip any attempt to check for a template digest.
|
44
49
|
#
|
45
50
|
# === Example:
|
46
51
|
#
|
@@ -66,18 +71,24 @@ module ActionController
|
|
66
71
|
# @article = Article.find(params[:id])
|
67
72
|
# fresh_when(@article, public: true)
|
68
73
|
# end
|
74
|
+
#
|
75
|
+
# When rendering a different template than the default controller/action
|
76
|
+
# style, you can indicate which digest to include in the ETag:
|
77
|
+
#
|
78
|
+
# before_action { fresh_when @article, template: 'widgets/show' }
|
79
|
+
#
|
69
80
|
def fresh_when(record_or_options, additional_options = {})
|
70
81
|
if record_or_options.is_a? Hash
|
71
82
|
options = record_or_options
|
72
|
-
options.assert_valid_keys(:etag, :last_modified, :public)
|
83
|
+
options.assert_valid_keys(:etag, :last_modified, :public, :template)
|
73
84
|
else
|
74
85
|
record = record_or_options
|
75
86
|
options = { etag: record, last_modified: record.try(:updated_at) }.merge!(additional_options)
|
76
87
|
end
|
77
88
|
|
78
|
-
response.etag = combine_etags(options[:etag]
|
79
|
-
response.last_modified = options[:last_modified]
|
80
|
-
response.cache_control[:public] = true
|
89
|
+
response.etag = combine_etags(options) if options[:etag] || options[:template]
|
90
|
+
response.last_modified = options[:last_modified] if options[:last_modified]
|
91
|
+
response.cache_control[:public] = true if options[:public]
|
81
92
|
|
82
93
|
head :not_modified if request.fresh?(response)
|
83
94
|
end
|
@@ -93,6 +104,11 @@ module ActionController
|
|
93
104
|
# * <tt>:last_modified</tt>.
|
94
105
|
# * <tt>:public</tt> By default the Cache-Control header is private, set this to
|
95
106
|
# +true+ if you want your application to be cachable by other devices (proxy caches).
|
107
|
+
# * <tt>:template</tt> By default, the template digest for the current
|
108
|
+
# controller/action is included in ETags. If the action renders a
|
109
|
+
# different template, you can include its digest instead. If the action
|
110
|
+
# doesn't render a template at all, you can pass <tt>template: false</tt>
|
111
|
+
# to skip any attempt to check for a template digest.
|
96
112
|
#
|
97
113
|
# === Example:
|
98
114
|
#
|
@@ -133,6 +149,14 @@ module ActionController
|
|
133
149
|
# end
|
134
150
|
# end
|
135
151
|
# end
|
152
|
+
#
|
153
|
+
# When rendering a different template than the default controller/action
|
154
|
+
# style, you can indicate which digest to include in the ETag:
|
155
|
+
#
|
156
|
+
# def show
|
157
|
+
# super if stale? @article, template: 'widgets/show'
|
158
|
+
# end
|
159
|
+
#
|
136
160
|
def stale?(record_or_options, additional_options = {})
|
137
161
|
fresh_when(record_or_options, additional_options)
|
138
162
|
!request.fresh?(response)
|
@@ -168,8 +192,9 @@ module ActionController
|
|
168
192
|
end
|
169
193
|
|
170
194
|
private
|
171
|
-
def combine_etags(
|
172
|
-
|
195
|
+
def combine_etags(options)
|
196
|
+
etags = etaggers.map { |etagger| instance_exec(options, &etagger) }.compact
|
197
|
+
etags.unshift options[:etag]
|
173
198
|
end
|
174
199
|
end
|
175
200
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActionController
|
2
|
+
# When our views change, they should bubble up into HTTP cache freshness
|
3
|
+
# and bust browser caches. So the template digest for the current action
|
4
|
+
# is automatically included in the ETag.
|
5
|
+
#
|
6
|
+
# Enabled by default for apps that use Action View. Disable by setting
|
7
|
+
#
|
8
|
+
# config.action_controller.etag_with_template_digest = false
|
9
|
+
#
|
10
|
+
# Override the template to digest by passing `:template` to `fresh_when`
|
11
|
+
# and `stale?` calls. For example:
|
12
|
+
#
|
13
|
+
# # We're going to render widgets/show, not posts/show
|
14
|
+
# fresh_when @post, template: 'widgets/show'
|
15
|
+
#
|
16
|
+
# # We're not going to render a template, so omit it from the ETag.
|
17
|
+
# fresh_when @post, template: false
|
18
|
+
#
|
19
|
+
module EtagWithTemplateDigest
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
include ActionController::ConditionalGet
|
23
|
+
|
24
|
+
included do
|
25
|
+
class_attribute :etag_with_template_digest
|
26
|
+
self.etag_with_template_digest = true
|
27
|
+
|
28
|
+
ActiveSupport.on_load :action_view, yield: true do |action_view_base|
|
29
|
+
etag do |options|
|
30
|
+
determine_template_etag(options) if etag_with_template_digest
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def determine_template_etag(options)
|
37
|
+
if template = pick_template_for_etag(options)
|
38
|
+
lookup_and_digest_template(template)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def pick_template_for_etag(options)
|
43
|
+
options.fetch(:template) { "#{controller_name}/#{action_name}" }
|
44
|
+
end
|
45
|
+
|
46
|
+
def lookup_and_digest_template(template)
|
47
|
+
ActionView::Digestor.digest name: template, finder: lookup_context
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -85,7 +85,7 @@ module ActionController
|
|
85
85
|
if host_or_options.is_a?(Hash)
|
86
86
|
options.merge!(host_or_options)
|
87
87
|
elsif host_or_options
|
88
|
-
options
|
88
|
+
options[:host] = host_or_options
|
89
89
|
end
|
90
90
|
|
91
91
|
secure_url = ActionDispatch::Http::URL.url_for(options.slice(*URL_OPTIONS))
|
@@ -14,6 +14,8 @@ module ActionController
|
|
14
14
|
# return head(:method_not_allowed) unless request.post?
|
15
15
|
# return head(:bad_request) unless valid_request?
|
16
16
|
# render
|
17
|
+
#
|
18
|
+
# See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list of valid +status+ symbols.
|
17
19
|
def head(status, options = {})
|
18
20
|
options, status = status, nil if status.is_a?(Hash)
|
19
21
|
status ||= options.delete(:status) || :ok
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'base64'
|
2
|
-
require 'active_support/security_utils'
|
3
2
|
|
4
3
|
module ActionController
|
5
4
|
# Makes it dead easy to do HTTP Basic, Digest and Token authentication.
|
@@ -71,11 +70,7 @@ module ActionController
|
|
71
70
|
def http_basic_authenticate_with(options = {})
|
72
71
|
before_action(options.except(:name, :password, :realm)) do
|
73
72
|
authenticate_or_request_with_http_basic(options[:realm] || "Application") do |name, password|
|
74
|
-
|
75
|
-
# uses `variable_size_secure_compare` so that length information
|
76
|
-
# isn't leaked.
|
77
|
-
ActiveSupport::SecurityUtils.variable_size_secure_compare(name, options[:name]) &
|
78
|
-
ActiveSupport::SecurityUtils.variable_size_secure_compare(password, options[:password])
|
73
|
+
name == options[:name] && password == options[:password]
|
79
74
|
end
|
80
75
|
end
|
81
76
|
end
|
@@ -402,7 +397,6 @@ module ActionController
|
|
402
397
|
#
|
403
398
|
# RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
|
404
399
|
module Token
|
405
|
-
TOKEN_KEY = 'token='
|
406
400
|
TOKEN_REGEX = /^Token /
|
407
401
|
AUTHN_PAIR_DELIMITERS = /(?:,|;|\t+)/
|
408
402
|
extend self
|
@@ -477,13 +471,7 @@ module ActionController
|
|
477
471
|
# pairs by the standardized `:`, `;`, or `\t` delimiters defined in
|
478
472
|
# `AUTHN_PAIR_DELIMITERS`.
|
479
473
|
def raw_params(auth)
|
480
|
-
|
481
|
-
|
482
|
-
if !(_raw_params.first =~ %r{\A#{TOKEN_KEY}})
|
483
|
-
_raw_params[0] = "#{TOKEN_KEY}#{_raw_params.first}"
|
484
|
-
end
|
485
|
-
|
486
|
-
_raw_params
|
474
|
+
auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
|
487
475
|
end
|
488
476
|
|
489
477
|
# Encodes the given token and options into an Authorization header value.
|
@@ -493,7 +481,7 @@ module ActionController
|
|
493
481
|
#
|
494
482
|
# Returns String.
|
495
483
|
def encode_credentials(token, options = {})
|
496
|
-
values = ["
|
484
|
+
values = ["token=#{token.to_s.inspect}"] + options.map do |key, value|
|
497
485
|
"#{key}=#{value.to_s.inspect}"
|
498
486
|
end
|
499
487
|
"Token #{values * ", "}"
|
@@ -28,13 +28,10 @@ module ActionController
|
|
28
28
|
ActiveSupport::Notifications.instrument("start_processing.action_controller", raw_payload.dup)
|
29
29
|
|
30
30
|
ActiveSupport::Notifications.instrument("process_action.action_controller", raw_payload) do |payload|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
ensure
|
36
|
-
append_info_to_payload(payload)
|
37
|
-
end
|
31
|
+
result = super
|
32
|
+
payload[:status] = response.status
|
33
|
+
append_info_to_payload(payload)
|
34
|
+
result
|
38
35
|
end
|
39
36
|
end
|
40
37
|
|
@@ -102,16 +102,30 @@ module ActionController
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
-
|
105
|
+
message = json.gsub("\n", "\ndata: ")
|
106
|
+
@stream.write "data: #{message}\n\n"
|
106
107
|
end
|
107
108
|
end
|
108
109
|
|
110
|
+
class ClientDisconnected < RuntimeError
|
111
|
+
end
|
112
|
+
|
109
113
|
class Buffer < ActionDispatch::Response::Buffer #:nodoc:
|
110
114
|
include MonitorMixin
|
111
115
|
|
116
|
+
# Ignore that the client has disconnected.
|
117
|
+
#
|
118
|
+
# If this value is `true`, calling `write` after the client
|
119
|
+
# disconnects will result in the written content being silently
|
120
|
+
# discarded. If this value is `false` (the default), a
|
121
|
+
# ClientDisconnected exception will be raised.
|
122
|
+
attr_accessor :ignore_disconnect
|
123
|
+
|
112
124
|
def initialize(response)
|
113
125
|
@error_callback = lambda { true }
|
114
126
|
@cv = new_cond
|
127
|
+
@aborted = false
|
128
|
+
@ignore_disconnect = false
|
115
129
|
super(response, SizedQueue.new(10))
|
116
130
|
end
|
117
131
|
|
@@ -122,6 +136,17 @@ module ActionController
|
|
122
136
|
end
|
123
137
|
|
124
138
|
super
|
139
|
+
|
140
|
+
unless connected?
|
141
|
+
@buf.clear
|
142
|
+
|
143
|
+
unless @ignore_disconnect
|
144
|
+
# Raise ClientDisconnected, which is a RuntimeError (not an
|
145
|
+
# IOError), because that's more appropriate for something beyond
|
146
|
+
# the developer's control.
|
147
|
+
raise ClientDisconnected, "client disconnected"
|
148
|
+
end
|
149
|
+
end
|
125
150
|
end
|
126
151
|
|
127
152
|
def each
|
@@ -132,6 +157,10 @@ module ActionController
|
|
132
157
|
@response.sent!
|
133
158
|
end
|
134
159
|
|
160
|
+
# Write a 'close' event to the buffer; the producer/writing thread
|
161
|
+
# uses this to notify us that it's finished supplying content.
|
162
|
+
#
|
163
|
+
# See also #abort.
|
135
164
|
def close
|
136
165
|
synchronize do
|
137
166
|
super
|
@@ -140,6 +169,26 @@ module ActionController
|
|
140
169
|
end
|
141
170
|
end
|
142
171
|
|
172
|
+
# Inform the producer/writing thread that the client has
|
173
|
+
# disconnected; the reading thread is no longer interested in
|
174
|
+
# anything that's being written.
|
175
|
+
#
|
176
|
+
# See also #close.
|
177
|
+
def abort
|
178
|
+
synchronize do
|
179
|
+
@aborted = true
|
180
|
+
@buf.clear
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# Is the client still connected and waiting for content?
|
185
|
+
#
|
186
|
+
# The result of calling `write` when this is `false` is determined
|
187
|
+
# by `ignore_disconnect`.
|
188
|
+
def connected?
|
189
|
+
!@aborted
|
190
|
+
end
|
191
|
+
|
143
192
|
def await_close
|
144
193
|
synchronize do
|
145
194
|
@cv.wait_until { @closed }
|
@@ -156,7 +205,7 @@ module ActionController
|
|
156
205
|
end
|
157
206
|
|
158
207
|
class Response < ActionDispatch::Response #:nodoc: all
|
159
|
-
class Header < DelegateClass(Hash)
|
208
|
+
class Header < DelegateClass(Hash) # :nodoc:
|
160
209
|
def initialize(response, header)
|
161
210
|
@response = response
|
162
211
|
super(header)
|
@@ -254,10 +303,12 @@ module ActionController
|
|
254
303
|
logger = ActionController::Base.logger
|
255
304
|
return unless logger
|
256
305
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
306
|
+
logger.fatal do
|
307
|
+
message = "\n#{exception.class} (#{exception.message}):\n"
|
308
|
+
message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
|
309
|
+
message << " " << exception.backtrace.join("\n ")
|
310
|
+
"#{message}\n\n"
|
311
|
+
end
|
261
312
|
end
|
262
313
|
|
263
314
|
def response_body=(body)
|
@@ -5,56 +5,22 @@ module ActionController #:nodoc:
|
|
5
5
|
module MimeResponds
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
included do
|
9
|
-
class_attribute :responder, :mimes_for_respond_to
|
10
|
-
self.responder = ActionController::Responder
|
11
|
-
clear_respond_to
|
12
|
-
end
|
13
|
-
|
14
8
|
module ClassMethods
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# for <tt>:html</tt>, <tt>:xml</tt> and <tt>:json</tt>.
|
22
|
-
#
|
23
|
-
# To specify on per-action basis, use <tt>:only</tt> and
|
24
|
-
# <tt>:except</tt> with an array of actions or a single action:
|
25
|
-
#
|
26
|
-
# respond_to :html
|
27
|
-
# respond_to :xml, :json, except: [ :edit ]
|
28
|
-
#
|
29
|
-
# This specifies that all actions respond to <tt>:html</tt>
|
30
|
-
# and all actions except <tt>:edit</tt> respond to <tt>:xml</tt> and
|
31
|
-
# <tt>:json</tt>.
|
32
|
-
#
|
33
|
-
# respond_to :json, only: :create
|
34
|
-
#
|
35
|
-
# This specifies that the <tt>:create</tt> action and no other responds
|
36
|
-
# to <tt>:json</tt>.
|
37
|
-
def respond_to(*mimes)
|
38
|
-
options = mimes.extract_options!
|
39
|
-
|
40
|
-
only_actions = Array(options.delete(:only)).map(&:to_s)
|
41
|
-
except_actions = Array(options.delete(:except)).map(&:to_s)
|
42
|
-
|
43
|
-
new = mimes_for_respond_to.dup
|
44
|
-
mimes.each do |mime|
|
45
|
-
mime = mime.to_sym
|
46
|
-
new[mime] = {}
|
47
|
-
new[mime][:only] = only_actions unless only_actions.empty?
|
48
|
-
new[mime][:except] = except_actions unless except_actions.empty?
|
49
|
-
end
|
50
|
-
self.mimes_for_respond_to = new.freeze
|
9
|
+
def respond_to(*)
|
10
|
+
raise NoMethodError, "The controller-level `respond_to' feature has " \
|
11
|
+
"been extracted to the `responders` gem. Add it to your Gemfile to " \
|
12
|
+
"continue using this feature:\n" \
|
13
|
+
" gem 'responders', '~> 2.0'\n" \
|
14
|
+
"Consult the Rails upgrade guide for details."
|
51
15
|
end
|
16
|
+
end
|
52
17
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
18
|
+
def respond_with(*)
|
19
|
+
raise NoMethodError, "The `respond_with' feature has been extracted " \
|
20
|
+
"to the `responders` gem. Add it to your Gemfile to continue using " \
|
21
|
+
"this feature:\n" \
|
22
|
+
" gem 'responders', '~> 2.0'\n" \
|
23
|
+
"Consult the Rails upgrade guide for details."
|
58
24
|
end
|
59
25
|
|
60
26
|
# Without web-service support, an action which collects the data for displaying a list of people
|
@@ -217,7 +183,7 @@ module ActionController #:nodoc:
|
|
217
183
|
# format.html.phone { redirect_to progress_path }
|
218
184
|
# format.html.none { render "trash" }
|
219
185
|
# end
|
220
|
-
#
|
186
|
+
#
|
221
187
|
# Variants also support common `any`/`all` block that formats have.
|
222
188
|
#
|
223
189
|
# It works for both inline:
|
@@ -253,189 +219,13 @@ module ActionController #:nodoc:
|
|
253
219
|
def respond_to(*mimes, &block)
|
254
220
|
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
|
255
221
|
|
256
|
-
if collector = retrieve_collector_from_mimes(mimes, &block)
|
257
|
-
response = collector.response
|
258
|
-
response ? response.call : render({})
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
# For a given controller action, respond_with generates an appropriate
|
263
|
-
# response based on the mime-type requested by the client.
|
264
|
-
#
|
265
|
-
# If the method is called with just a resource, as in this example -
|
266
|
-
#
|
267
|
-
# class PeopleController < ApplicationController
|
268
|
-
# respond_to :html, :xml, :json
|
269
|
-
#
|
270
|
-
# def index
|
271
|
-
# @people = Person.all
|
272
|
-
# respond_with @people
|
273
|
-
# end
|
274
|
-
# end
|
275
|
-
#
|
276
|
-
# then the mime-type of the response is typically selected based on the
|
277
|
-
# request's Accept header and the set of available formats declared
|
278
|
-
# by previous calls to the controller's class method +respond_to+. Alternatively
|
279
|
-
# the mime-type can be selected by explicitly setting <tt>request.format</tt> in
|
280
|
-
# the controller.
|
281
|
-
#
|
282
|
-
# If an acceptable format is not identified, the application returns a
|
283
|
-
# '406 - not acceptable' status. Otherwise, the default response is to render
|
284
|
-
# a template named after the current action and the selected format,
|
285
|
-
# e.g. <tt>index.html.erb</tt>. If no template is available, the behavior
|
286
|
-
# depends on the selected format:
|
287
|
-
#
|
288
|
-
# * for an html response - if the request method is +get+, an exception
|
289
|
-
# is raised but for other requests such as +post+ the response
|
290
|
-
# depends on whether the resource has any validation errors (i.e.
|
291
|
-
# assuming that an attempt has been made to save the resource,
|
292
|
-
# e.g. by a +create+ action) -
|
293
|
-
# 1. If there are no errors, i.e. the resource
|
294
|
-
# was saved successfully, the response +redirect+'s to the resource
|
295
|
-
# i.e. its +show+ action.
|
296
|
-
# 2. If there are validation errors, the response
|
297
|
-
# renders a default action, which is <tt>:new</tt> for a
|
298
|
-
# +post+ request or <tt>:edit</tt> for +patch+ or +put+.
|
299
|
-
# Thus an example like this -
|
300
|
-
#
|
301
|
-
# respond_to :html, :xml
|
302
|
-
#
|
303
|
-
# def create
|
304
|
-
# @user = User.new(params[:user])
|
305
|
-
# flash[:notice] = 'User was successfully created.' if @user.save
|
306
|
-
# respond_with(@user)
|
307
|
-
# end
|
308
|
-
#
|
309
|
-
# is equivalent, in the absence of <tt>create.html.erb</tt>, to -
|
310
|
-
#
|
311
|
-
# def create
|
312
|
-
# @user = User.new(params[:user])
|
313
|
-
# respond_to do |format|
|
314
|
-
# if @user.save
|
315
|
-
# flash[:notice] = 'User was successfully created.'
|
316
|
-
# format.html { redirect_to(@user) }
|
317
|
-
# format.xml { render xml: @user }
|
318
|
-
# else
|
319
|
-
# format.html { render action: "new" }
|
320
|
-
# format.xml { render xml: @user }
|
321
|
-
# end
|
322
|
-
# end
|
323
|
-
# end
|
324
|
-
#
|
325
|
-
# * for a javascript request - if the template isn't found, an exception is
|
326
|
-
# raised.
|
327
|
-
# * for other requests - i.e. data formats such as xml, json, csv etc, if
|
328
|
-
# the resource passed to +respond_with+ responds to <code>to_<format></code>,
|
329
|
-
# the method attempts to render the resource in the requested format
|
330
|
-
# directly, e.g. for an xml request, the response is equivalent to calling
|
331
|
-
# <code>render xml: resource</code>.
|
332
|
-
#
|
333
|
-
# === Nested resources
|
334
|
-
#
|
335
|
-
# As outlined above, the +resources+ argument passed to +respond_with+
|
336
|
-
# can play two roles. It can be used to generate the redirect url
|
337
|
-
# for successful html requests (e.g. for +create+ actions when
|
338
|
-
# no template exists), while for formats other than html and javascript
|
339
|
-
# it is the object that gets rendered, by being converted directly to the
|
340
|
-
# required format (again assuming no template exists).
|
341
|
-
#
|
342
|
-
# For redirecting successful html requests, +respond_with+ also supports
|
343
|
-
# the use of nested resources, which are supplied in the same way as
|
344
|
-
# in <code>form_for</code> and <code>polymorphic_url</code>. For example -
|
345
|
-
#
|
346
|
-
# def create
|
347
|
-
# @project = Project.find(params[:project_id])
|
348
|
-
# @task = @project.comments.build(params[:task])
|
349
|
-
# flash[:notice] = 'Task was successfully created.' if @task.save
|
350
|
-
# respond_with(@project, @task)
|
351
|
-
# end
|
352
|
-
#
|
353
|
-
# This would cause +respond_with+ to redirect to <code>project_task_url</code>
|
354
|
-
# instead of <code>task_url</code>. For request formats other than html or
|
355
|
-
# javascript, if multiple resources are passed in this way, it is the last
|
356
|
-
# one specified that is rendered.
|
357
|
-
#
|
358
|
-
# === Customizing response behavior
|
359
|
-
#
|
360
|
-
# Like +respond_to+, +respond_with+ may also be called with a block that
|
361
|
-
# can be used to overwrite any of the default responses, e.g. -
|
362
|
-
#
|
363
|
-
# def create
|
364
|
-
# @user = User.new(params[:user])
|
365
|
-
# flash[:notice] = "User was successfully created." if @user.save
|
366
|
-
#
|
367
|
-
# respond_with(@user) do |format|
|
368
|
-
# format.html { render }
|
369
|
-
# end
|
370
|
-
# end
|
371
|
-
#
|
372
|
-
# The argument passed to the block is an ActionController::MimeResponds::Collector
|
373
|
-
# object which stores the responses for the formats defined within the
|
374
|
-
# block. Note that formats with responses defined explicitly in this way
|
375
|
-
# do not have to first be declared using the class method +respond_to+.
|
376
|
-
#
|
377
|
-
# Also, a hash passed to +respond_with+ immediately after the specified
|
378
|
-
# resource(s) is interpreted as a set of options relevant to all
|
379
|
-
# formats. Any option accepted by +render+ can be used, e.g.
|
380
|
-
# respond_with @people, status: 200
|
381
|
-
# However, note that these options are ignored after an unsuccessful attempt
|
382
|
-
# to save a resource, e.g. when automatically rendering <tt>:new</tt>
|
383
|
-
# after a post request.
|
384
|
-
#
|
385
|
-
# Two additional options are relevant specifically to +respond_with+ -
|
386
|
-
# 1. <tt>:location</tt> - overwrites the default redirect location used after
|
387
|
-
# a successful html +post+ request.
|
388
|
-
# 2. <tt>:action</tt> - overwrites the default render action used after an
|
389
|
-
# unsuccessful html +post+ request.
|
390
|
-
def respond_with(*resources, &block)
|
391
|
-
if self.class.mimes_for_respond_to.empty?
|
392
|
-
raise "In order to use respond_with, first you need to declare the " \
|
393
|
-
"formats your controller responds to in the class level."
|
394
|
-
end
|
395
|
-
|
396
|
-
if collector = retrieve_collector_from_mimes(&block)
|
397
|
-
options = resources.size == 1 ? {} : resources.extract_options!
|
398
|
-
options = options.clone
|
399
|
-
options[:default_response] = collector.response
|
400
|
-
(options.delete(:responder) || self.class.responder).call(self, resources, options)
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
protected
|
405
|
-
|
406
|
-
# Collect mimes declared in the class method respond_to valid for the
|
407
|
-
# current action.
|
408
|
-
def collect_mimes_from_class_level #:nodoc:
|
409
|
-
action = action_name.to_s
|
410
|
-
|
411
|
-
self.class.mimes_for_respond_to.keys.select do |mime|
|
412
|
-
config = self.class.mimes_for_respond_to[mime]
|
413
|
-
|
414
|
-
if config[:except]
|
415
|
-
!config[:except].include?(action)
|
416
|
-
elsif config[:only]
|
417
|
-
config[:only].include?(action)
|
418
|
-
else
|
419
|
-
true
|
420
|
-
end
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
# Returns a Collector object containing the appropriate mime-type response
|
425
|
-
# for the current request, based on the available responses defined by a block.
|
426
|
-
# In typical usage this is the block passed to +respond_with+ or +respond_to+.
|
427
|
-
#
|
428
|
-
# Sends :not_acceptable to the client and returns nil if no suitable format
|
429
|
-
# is available.
|
430
|
-
def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
|
431
|
-
mimes ||= collect_mimes_from_class_level
|
432
222
|
collector = Collector.new(mimes, request.variant)
|
433
223
|
block.call(collector) if block_given?
|
434
|
-
format = collector.negotiate_format(request)
|
435
224
|
|
436
|
-
if format
|
225
|
+
if format = collector.negotiate_format(request)
|
437
226
|
_process_format(format)
|
438
|
-
collector
|
227
|
+
response = collector.response
|
228
|
+
response ? response.call : render({})
|
439
229
|
else
|
440
230
|
raise ActionController::UnknownFormat
|
441
231
|
end
|