actionpack 4.2.11.3 → 5.0.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.

Files changed (125) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +379 -462
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +2 -3
  5. data/lib/abstract_controller.rb +0 -2
  6. data/lib/abstract_controller/base.rb +17 -32
  7. data/lib/abstract_controller/callbacks.rb +52 -19
  8. data/lib/abstract_controller/collector.rb +4 -9
  9. data/lib/abstract_controller/helpers.rb +2 -2
  10. data/lib/abstract_controller/railties/routes_helpers.rb +2 -2
  11. data/lib/abstract_controller/rendering.rb +27 -22
  12. data/lib/abstract_controller/translation.rb +8 -7
  13. data/lib/action_controller.rb +4 -3
  14. data/lib/action_controller/api.rb +146 -0
  15. data/lib/action_controller/base.rb +6 -10
  16. data/lib/action_controller/caching.rb +1 -3
  17. data/lib/action_controller/caching/fragments.rb +48 -3
  18. data/lib/action_controller/form_builder.rb +48 -0
  19. data/lib/action_controller/log_subscriber.rb +1 -10
  20. data/lib/action_controller/metal.rb +89 -62
  21. data/lib/action_controller/metal/basic_implicit_render.rb +11 -0
  22. data/lib/action_controller/metal/conditional_get.rb +65 -24
  23. data/lib/action_controller/metal/cookies.rb +0 -2
  24. data/lib/action_controller/metal/data_streaming.rb +2 -22
  25. data/lib/action_controller/metal/etag_with_template_digest.rb +1 -1
  26. data/lib/action_controller/metal/exceptions.rb +11 -6
  27. data/lib/action_controller/metal/force_ssl.rb +6 -6
  28. data/lib/action_controller/metal/head.rb +14 -7
  29. data/lib/action_controller/metal/helpers.rb +9 -5
  30. data/lib/action_controller/metal/http_authentication.rb +37 -38
  31. data/lib/action_controller/metal/implicit_render.rb +23 -6
  32. data/lib/action_controller/metal/instrumentation.rb +0 -1
  33. data/lib/action_controller/metal/live.rb +17 -55
  34. data/lib/action_controller/metal/mime_responds.rb +17 -37
  35. data/lib/action_controller/metal/params_wrapper.rb +8 -8
  36. data/lib/action_controller/metal/redirecting.rb +32 -9
  37. data/lib/action_controller/metal/renderers.rb +10 -8
  38. data/lib/action_controller/metal/rendering.rb +38 -6
  39. data/lib/action_controller/metal/request_forgery_protection.rb +67 -35
  40. data/lib/action_controller/metal/rescue.rb +2 -4
  41. data/lib/action_controller/metal/streaming.rb +4 -4
  42. data/lib/action_controller/metal/strong_parameters.rb +231 -78
  43. data/lib/action_controller/metal/testing.rb +1 -12
  44. data/lib/action_controller/metal/url_for.rb +12 -5
  45. data/lib/action_controller/renderer.rb +111 -0
  46. data/lib/action_controller/template_assertions.rb +9 -0
  47. data/lib/action_controller/test_case.rb +267 -363
  48. data/lib/action_dispatch.rb +2 -1
  49. data/lib/action_dispatch/http/cache.rb +23 -26
  50. data/lib/action_dispatch/http/filter_parameters.rb +6 -8
  51. data/lib/action_dispatch/http/filter_redirect.rb +7 -8
  52. data/lib/action_dispatch/http/headers.rb +28 -11
  53. data/lib/action_dispatch/http/mime_negotiation.rb +40 -26
  54. data/lib/action_dispatch/http/mime_type.rb +92 -61
  55. data/lib/action_dispatch/http/mime_types.rb +1 -4
  56. data/lib/action_dispatch/http/parameter_filter.rb +18 -8
  57. data/lib/action_dispatch/http/parameters.rb +45 -41
  58. data/lib/action_dispatch/http/request.rb +146 -82
  59. data/lib/action_dispatch/http/response.rb +180 -99
  60. data/lib/action_dispatch/http/url.rb +117 -8
  61. data/lib/action_dispatch/journey/formatter.rb +34 -28
  62. data/lib/action_dispatch/journey/gtg/transition_table.rb +1 -1
  63. data/lib/action_dispatch/journey/nfa/dot.rb +0 -2
  64. data/lib/action_dispatch/journey/nfa/transition_table.rb +1 -46
  65. data/lib/action_dispatch/journey/nodes/node.rb +14 -4
  66. data/lib/action_dispatch/journey/parser_extras.rb +4 -0
  67. data/lib/action_dispatch/journey/path/pattern.rb +37 -41
  68. data/lib/action_dispatch/journey/route.rb +71 -17
  69. data/lib/action_dispatch/journey/router.rb +5 -6
  70. data/lib/action_dispatch/journey/router/utils.rb +5 -5
  71. data/lib/action_dispatch/journey/routes.rb +14 -15
  72. data/lib/action_dispatch/journey/visitors.rb +86 -43
  73. data/lib/action_dispatch/middleware/cookies.rb +184 -135
  74. data/lib/action_dispatch/middleware/debug_exceptions.rb +115 -45
  75. data/lib/action_dispatch/middleware/exception_wrapper.rb +21 -20
  76. data/lib/action_dispatch/middleware/flash.rb +61 -45
  77. data/lib/action_dispatch/middleware/load_interlock.rb +21 -0
  78. data/lib/action_dispatch/middleware/params_parser.rb +30 -46
  79. data/lib/action_dispatch/middleware/public_exceptions.rb +2 -2
  80. data/lib/action_dispatch/middleware/reloader.rb +2 -4
  81. data/lib/action_dispatch/middleware/remote_ip.rb +29 -19
  82. data/lib/action_dispatch/middleware/request_id.rb +11 -6
  83. data/lib/action_dispatch/middleware/session/abstract_store.rb +23 -11
  84. data/lib/action_dispatch/middleware/session/cache_store.rb +9 -6
  85. data/lib/action_dispatch/middleware/session/cookie_store.rb +29 -23
  86. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +4 -0
  87. data/lib/action_dispatch/middleware/show_exceptions.rb +11 -9
  88. data/lib/action_dispatch/middleware/ssl.rb +93 -36
  89. data/lib/action_dispatch/middleware/stack.rb +43 -48
  90. data/lib/action_dispatch/middleware/static.rb +52 -40
  91. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +2 -14
  92. data/lib/action_dispatch/middleware/templates/rescues/{_source.erb → _source.html.erb} +0 -0
  93. data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
  94. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -1
  95. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +1 -1
  96. data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +4 -4
  97. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +59 -63
  98. data/lib/action_dispatch/railtie.rb +0 -2
  99. data/lib/action_dispatch/request/session.rb +66 -34
  100. data/lib/action_dispatch/request/utils.rb +51 -19
  101. data/lib/action_dispatch/routing.rb +3 -8
  102. data/lib/action_dispatch/routing/inspector.rb +6 -30
  103. data/lib/action_dispatch/routing/mapper.rb +447 -322
  104. data/lib/action_dispatch/routing/polymorphic_routes.rb +8 -14
  105. data/lib/action_dispatch/routing/redirection.rb +3 -3
  106. data/lib/action_dispatch/routing/route_set.rb +124 -227
  107. data/lib/action_dispatch/routing/url_for.rb +27 -10
  108. data/lib/action_dispatch/testing/assertions.rb +1 -1
  109. data/lib/action_dispatch/testing/assertions/response.rb +27 -9
  110. data/lib/action_dispatch/testing/assertions/routing.rb +9 -9
  111. data/lib/action_dispatch/testing/integration.rb +237 -76
  112. data/lib/action_dispatch/testing/test_process.rb +5 -5
  113. data/lib/action_dispatch/testing/test_request.rb +12 -21
  114. data/lib/action_dispatch/testing/test_response.rb +1 -4
  115. data/lib/action_pack.rb +1 -1
  116. data/lib/action_pack/gem_version.rb +4 -4
  117. metadata +26 -25
  118. data/lib/action_controller/metal/hide_actions.rb +0 -40
  119. data/lib/action_controller/metal/rack_delegation.rb +0 -32
  120. data/lib/action_controller/middleware.rb +0 -39
  121. data/lib/action_controller/model_naming.rb +0 -12
  122. data/lib/action_dispatch/journey/router/strexp.rb +0 -27
  123. data/lib/action_dispatch/testing/assertions/dom.rb +0 -3
  124. data/lib/action_dispatch/testing/assertions/selector.rb +0 -3
  125. data/lib/action_dispatch/testing/assertions/tag.rb +0 -3
@@ -1,13 +1,30 @@
1
1
  module ActionController
2
2
  module ImplicitRender
3
- def send_action(method, *args)
4
- ret = super
5
- default_render unless performed?
6
- ret
7
- end
8
3
 
4
+ include BasicImplicitRender
5
+
6
+ # Renders the template corresponding to the controller action, if it exists.
7
+ # The action name, format, and variant are all taken into account.
8
+ # For example, the "new" action with an HTML format and variant "phone"
9
+ # would try to render the <tt>new.html+phone.erb</tt> template.
10
+ #
11
+ # If no template is found <tt>ActionController::BasicImplicitRender</tt>'s implementation is called, unless
12
+ # a block is passed. In that case, it will override the super implementation.
13
+ #
14
+ # default_render do
15
+ # head 404 # No template was found
16
+ # end
9
17
  def default_render(*args)
10
- render(*args)
18
+ if template_exists?(action_name.to_s, _prefixes, variants: request.variant)
19
+ render(*args)
20
+ else
21
+ if block_given?
22
+ yield(*args)
23
+ else
24
+ logger.info "No template found for #{self.class.name}\##{action_name}, rendering head :no_content" if logger
25
+ super
26
+ end
27
+ end
11
28
  end
12
29
 
13
30
  def method_for_action(action_name)
@@ -11,7 +11,6 @@ module ActionController
11
11
  extend ActiveSupport::Concern
12
12
 
13
13
  include AbstractController::Logger
14
- include ActionController::RackDelegation
15
14
 
16
15
  attr_internal :view_runtime
17
16
 
@@ -33,6 +33,20 @@ module ActionController
33
33
  # the main thread. Make sure your actions are thread safe, and this shouldn't
34
34
  # be a problem (don't share state across threads, etc).
35
35
  module Live
36
+ extend ActiveSupport::Concern
37
+
38
+ module ClassMethods
39
+ def make_response!(request)
40
+ if request.get_header("HTTP_VERSION") == "HTTP/1.0"
41
+ super
42
+ else
43
+ Live::Response.new.tap do |res|
44
+ res.request = request
45
+ end
46
+ end
47
+ end
48
+ end
49
+
36
50
  # This class provides the ability to write an SSE (Server Sent Event)
37
51
  # to an IO stream. The class is initialized with a stream and can be used
38
52
  # to either write a JSON string or an object which can be converted to JSON.
@@ -102,7 +116,7 @@ module ActionController
102
116
  end
103
117
  end
104
118
 
105
- message = json.gsub(/\n/, "\ndata: ")
119
+ message = json.gsub("\n".freeze, "\ndata: ".freeze)
106
120
  @stream.write "data: #{message}\n\n"
107
121
  end
108
122
  end
@@ -131,8 +145,8 @@ module ActionController
131
145
 
132
146
  def write(string)
133
147
  unless @response.committed?
134
- @response.headers["Cache-Control"] = "no-cache"
135
- @response.headers.delete "Content-Length"
148
+ @response.set_header "Cache-Control", "no-cache"
149
+ @response.delete_header "Content-Length"
136
150
  end
137
151
 
138
152
  super
@@ -189,12 +203,6 @@ module ActionController
189
203
  !@aborted
190
204
  end
191
205
 
192
- def await_close
193
- synchronize do
194
- @cv.wait_until { @closed }
195
- end
196
- end
197
-
198
206
  def on_error(&block)
199
207
  @error_callback = block
200
208
  end
@@ -205,29 +213,6 @@ module ActionController
205
213
  end
206
214
 
207
215
  class Response < ActionDispatch::Response #:nodoc: all
208
- class Header < DelegateClass(Hash) # :nodoc:
209
- def initialize(response, header)
210
- @response = response
211
- super(header)
212
- end
213
-
214
- def []=(k,v)
215
- if @response.committed?
216
- raise ActionDispatch::IllegalStateError, 'header already sent'
217
- end
218
-
219
- super
220
- end
221
-
222
- def merge(other)
223
- self.class.new @response, __getobj__.merge(other)
224
- end
225
-
226
- def to_hash
227
- __getobj__.dup
228
- end
229
- end
230
-
231
216
  private
232
217
 
233
218
  def before_committed
@@ -237,25 +222,11 @@ module ActionController
237
222
  jar.write self unless committed?
238
223
  end
239
224
 
240
- def before_sending
241
- super
242
- request.cookie_jar.commit!
243
- headers.freeze
244
- end
245
-
246
225
  def build_buffer(response, body)
247
226
  buf = Live::Buffer.new response
248
227
  body.each { |part| buf.write part }
249
228
  buf
250
229
  end
251
-
252
- def merge_default_headers(original, default)
253
- Header.new self, super
254
- end
255
-
256
- def handle_conditional_get!
257
- super unless committed?
258
- end
259
230
  end
260
231
 
261
232
  def process(name)
@@ -315,14 +286,5 @@ module ActionController
315
286
  super
316
287
  response.close if response
317
288
  end
318
-
319
- def set_response!(request)
320
- if request.env["HTTP_VERSION"] == "HTTP/1.0"
321
- super
322
- else
323
- @_response = Live::Response.new
324
- @_response.request = request
325
- end
326
- end
327
289
  end
328
290
  end
@@ -2,28 +2,6 @@ require 'abstract_controller/collector'
2
2
 
3
3
  module ActionController #:nodoc:
4
4
  module MimeResponds
5
- extend ActiveSupport::Concern
6
-
7
- # :stopdoc:
8
- module ClassMethods
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."
15
- end
16
- end
17
-
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."
24
- end
25
- # :startdoc:
26
-
27
5
  # Without web-service support, an action which collects the data for displaying a list of people
28
6
  # might look something like this:
29
7
  #
@@ -113,11 +91,11 @@ module ActionController #:nodoc:
113
91
  # and accept Rails' defaults, life will be much easier.
114
92
  #
115
93
  # If you need to use a MIME type which isn't supported by default, you can register your own handlers in
116
- # config/initializers/mime_types.rb as follows.
94
+ # +config/initializers/mime_types.rb+ as follows.
117
95
  #
118
96
  # Mime::Type.register "image/jpg", :jpg
119
97
  #
120
- # Respond to also allows you to specify a common block for different formats by using any:
98
+ # Respond to also allows you to specify a common block for different formats by using +any+:
121
99
  #
122
100
  # def index
123
101
  # @people = Person.all
@@ -173,21 +151,21 @@ module ActionController #:nodoc:
173
151
  # format.html.none { render "trash" }
174
152
  # end
175
153
  #
176
- # Variants also support common `any`/`all` block that formats have.
154
+ # Variants also support common +any+/+all+ block that formats have.
177
155
  #
178
156
  # It works for both inline:
179
157
  #
180
158
  # respond_to do |format|
181
- # format.html.any { render text: "any" }
182
- # format.html.phone { render text: "phone" }
159
+ # format.html.any { render html: "any" }
160
+ # format.html.phone { render html: "phone" }
183
161
  # end
184
162
  #
185
163
  # and block syntax:
186
164
  #
187
165
  # respond_to do |format|
188
166
  # format.html do |variant|
189
- # variant.any(:tablet, :phablet){ render text: "any" }
190
- # variant.phone { render text: "phone" }
167
+ # variant.any(:tablet, :phablet){ render html: "any" }
168
+ # variant.phone { render html: "phone" }
191
169
  # end
192
170
  # end
193
171
  #
@@ -196,7 +174,7 @@ module ActionController #:nodoc:
196
174
  # request.variant = [:tablet, :phone]
197
175
  #
198
176
  # which will work similarly to formats and MIME types negotiation. If there will be no
199
- # :tablet variant declared, :phone variant will be picked:
177
+ # +:tablet+ variant declared, +:phone+ variant will be picked:
200
178
  #
201
179
  # respond_to do |format|
202
180
  # format.html.none
@@ -213,6 +191,7 @@ module ActionController #:nodoc:
213
191
 
214
192
  if format = collector.negotiate_format(request)
215
193
  _process_format(format)
194
+ _set_rendered_content_type format
216
195
  response = collector.response
217
196
  response ? response.call : render({})
218
197
  else
@@ -250,7 +229,7 @@ module ActionController #:nodoc:
250
229
  @responses = {}
251
230
  @variant = variant
252
231
 
253
- mimes.each { |mime| @responses["Mime::#{mime.upcase}".constantize] = nil }
232
+ mimes.each { |mime| @responses[Mime[mime]] = nil }
254
233
  end
255
234
 
256
235
  def any(*args, &block)
@@ -310,16 +289,17 @@ module ActionController #:nodoc:
310
289
  end
311
290
 
312
291
  def variant
313
- if @variant.nil?
292
+ if @variant.empty?
314
293
  @variants[:none] || @variants[:any]
315
- elsif (@variants.keys & @variant).any?
316
- @variant.each do |v|
317
- return @variants[v] if @variants.key?(v)
318
- end
319
294
  else
320
- @variants[:any]
295
+ @variants[variant_key]
321
296
  end
322
297
  end
298
+
299
+ private
300
+ def variant_key
301
+ @variant.find { |variant| @variants.key?(variant) } || :any
302
+ end
323
303
  end
324
304
  end
325
305
  end
@@ -1,7 +1,6 @@
1
1
  require 'active_support/core_ext/hash/slice'
2
2
  require 'active_support/core_ext/hash/except'
3
3
  require 'active_support/core_ext/module/anonymous'
4
- require 'active_support/core_ext/struct'
5
4
  require 'action_dispatch/http/mime_type'
6
5
 
7
6
  module ActionController
@@ -9,8 +8,7 @@ module ActionController
9
8
  # submit requests without having to specify any root elements.
10
9
  #
11
10
  # This functionality is enabled in +config/initializers/wrap_parameters.rb+
12
- # and can be customized. If you are upgrading to \Rails 3.1, this file will
13
- # need to be created for the functionality to be enabled.
11
+ # and can be customized.
14
12
  #
15
13
  # You could also turn it on per controller by setting the format array to
16
14
  # a non-empty array:
@@ -42,7 +40,7 @@ module ActionController
42
40
  # wrap_parameters :person, include: [:username, :password]
43
41
  # end
44
42
  #
45
- # On ActiveRecord models with no +:include+ or +:exclude+ option set,
43
+ # On Active Record models with no +:include+ or +:exclude+ option set,
46
44
  # it will only wrap the parameters returned by the class method
47
45
  # <tt>attribute_names</tt>.
48
46
  #
@@ -86,7 +84,7 @@ module ActionController
86
84
  new name, format, include, exclude, nil, nil
87
85
  end
88
86
 
89
- def initialize(name, format, include, exclude, klass, model) # nodoc
87
+ def initialize(name, format, include, exclude, klass, model) # :nodoc:
90
88
  super
91
89
  @include_set = include
92
90
  @name_set = name
@@ -132,7 +130,7 @@ module ActionController
132
130
  private
133
131
  # Determine the wrapper model from the controller's name. By convention,
134
132
  # this could be done by trying to find the defined model that has the
135
- # same singularize name as the controller. For example, +UsersController+
133
+ # same singular name as the controller. For example, +UsersController+
136
134
  # will try to find if the +User+ model exists.
137
135
  #
138
136
  # This method also does namespace lookup. Foo::Bar::UsersController will
@@ -252,7 +250,7 @@ module ActionController
252
250
 
253
251
  private
254
252
 
255
- # Returns the wrapper key which will be used to stored wrapped parameters.
253
+ # Returns the wrapper key which will be used to store wrapped parameters.
256
254
  def _wrapper_key
257
255
  _wrapper_options.name
258
256
  end
@@ -278,7 +276,9 @@ module ActionController
278
276
 
279
277
  # Checks if we should perform parameters wrapping.
280
278
  def _wrapper_enabled?
281
- ref = request.content_mime_type.try(:ref)
279
+ return false unless request.has_content_type?
280
+
281
+ ref = request.content_mime_type.ref
282
282
  _wrapper_formats.include?(ref) && _wrapper_key && !request.request_parameters[_wrapper_key]
283
283
  end
284
284
  end
@@ -11,7 +11,6 @@ module ActionController
11
11
  extend ActiveSupport::Concern
12
12
 
13
13
  include AbstractController::Logger
14
- include ActionController::RackDelegation
15
14
  include ActionController::UrlFor
16
15
 
17
16
  # Redirects the browser to the target specified in +options+. This parameter can be any one of:
@@ -21,8 +20,6 @@ module ActionController
21
20
  # * <tt>String</tt> starting with <tt>protocol://</tt> (like <tt>http://</tt>) or a protocol relative reference (like <tt>//</tt>) - Is passed straight through as the target for redirection.
22
21
  # * <tt>String</tt> not containing a protocol - The current protocol and host is prepended to the string.
23
22
  # * <tt>Proc</tt> - A block that will be executed in the controller's context. Should return any option accepted by +redirect_to+.
24
- # * <tt>:back</tt> - Back to the page that issued the request. Useful for forms that are triggered from multiple places.
25
- # Short-hand for <tt>redirect_to(request.env["HTTP_REFERER"])</tt>
26
23
  #
27
24
  # === Examples:
28
25
  #
@@ -31,7 +28,6 @@ module ActionController
31
28
  # redirect_to "http://www.rubyonrails.org"
32
29
  # redirect_to "/images/screenshot.jpg"
33
30
  # redirect_to articles_url
34
- # redirect_to :back
35
31
  # redirect_to proc { edit_post_url(@post) }
36
32
  #
37
33
  # The redirection happens as a "302 Found" header unless otherwise specified using the <tt>:status</tt> option:
@@ -62,13 +58,8 @@ module ActionController
62
58
  # redirect_to post_url(@post), status: 301, flash: { updated_post_id: @post.id }
63
59
  # redirect_to({ action: 'atom' }, alert: "Something serious happened")
64
60
  #
65
- # When using <tt>redirect_to :back</tt>, if there is no referrer,
66
- # <tt>ActionController::RedirectBackError</tt> will be raised. You
67
- # may specify some fallback behavior for this case by rescuing
68
- # <tt>ActionController::RedirectBackError</tt>.
69
61
  def redirect_to(options = {}, response_status = {}) #:doc:
70
62
  raise ActionControllerError.new("Cannot redirect to nil!") unless options
71
- raise ActionControllerError.new("Cannot redirect to a parameter hash!") if options.is_a?(ActionController::Parameters)
72
63
  raise AbstractController::DoubleRenderError if response_body
73
64
 
74
65
  self.status = _extract_redirect_to_status(options, response_status)
@@ -76,6 +67,32 @@ module ActionController
76
67
  self.response_body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>"
77
68
  end
78
69
 
70
+ # Redirects the browser to the page that issued the request (the referrer)
71
+ # if possible, otherwise redirects to the provided default fallback
72
+ # location.
73
+ #
74
+ # The referrer information is pulled from the HTTP `Referer` (sic) header on
75
+ # the request. This is an optional header and its presence on the request is
76
+ # subject to browser security settings and user preferences. If the request
77
+ # is missing this header, the <tt>fallback_location</tt> will be used.
78
+ #
79
+ # redirect_back fallback_location: { action: "show", id: 5 }
80
+ # redirect_back fallback_location: post
81
+ # redirect_back fallback_location: "http://www.rubyonrails.org"
82
+ # redirect_back fallback_location: "/images/screenshot.jpg"
83
+ # redirect_back fallback_location: articles_url
84
+ # redirect_back fallback_location: proc { edit_post_url(@post) }
85
+ #
86
+ # All options that can be passed to <tt>redirect_to</tt> are accepted as
87
+ # options and the behavior is indetical.
88
+ def redirect_back(fallback_location:, **args)
89
+ if referer = request.headers["Referer"]
90
+ redirect_to referer, **args
91
+ else
92
+ redirect_to fallback_location, **args
93
+ end
94
+ end
95
+
79
96
  def _compute_redirect_to_location(request, options) #:nodoc:
80
97
  case options
81
98
  # The scheme name consist of a letter followed by any combination of
@@ -88,6 +105,12 @@ module ActionController
88
105
  when String
89
106
  request.protocol + request.host_with_port + options
90
107
  when :back
108
+ ActiveSupport::Deprecation.warn(<<-MESSAGE.squish)
109
+ `redirect_to :back` is deprecated and will be removed from Rails 5.1.
110
+ Please use `redirect_back(fallback_location: fallback_location)` where
111
+ `fallback_location` represents the location to use if the request has
112
+ no HTTP referer information.
113
+ MESSAGE
91
114
  request.headers["Referer"] or raise RedirectBackError
92
115
  when Proc
93
116
  _compute_redirect_to_location request, options.call
@@ -68,11 +68,11 @@ module ActionController
68
68
  # ActionController::Renderers.add :csv do |obj, options|
69
69
  # filename = options[:filename] || 'data'
70
70
  # str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
71
- # send_data str, type: Mime::CSV,
71
+ # send_data str, type: Mime[:csv],
72
72
  # disposition: "attachment; filename=#{filename}.csv"
73
73
  # end
74
74
  #
75
- # Note that we used Mime::CSV for the csv mime type as it comes with Rails.
75
+ # Note that we used Mime[:csv] for the csv mime type as it comes with Rails.
76
76
  # For a custom renderer, you'll need to register a mime type with
77
77
  # <tt>Mime::Type.register</tt>.
78
78
  #
@@ -85,6 +85,8 @@ module ActionController
85
85
  # format.csv { render csv: @csvable, filename: @csvable.name }
86
86
  # end
87
87
  # end
88
+ # To use renderers and their mime types in more concise ways, see
89
+ # <tt>ActionController::MimeResponds::ClassMethods.respond_to</tt>
88
90
  def self.add(key, &block)
89
91
  define_method(_render_with_renderer_method_name(key), &block)
90
92
  RENDERERS << key.to_sym
@@ -92,7 +94,7 @@ module ActionController
92
94
 
93
95
  # This method is the opposite of add method.
94
96
  #
95
- # Usage:
97
+ # To remove a csv renderer:
96
98
  #
97
99
  # ActionController::Renderers.remove(:csv)
98
100
  def self.remove(key)
@@ -114,24 +116,24 @@ module ActionController
114
116
  json = json.to_json(options) unless json.kind_of?(String)
115
117
 
116
118
  if options[:callback].present?
117
- if content_type.nil? || content_type == Mime::JSON
118
- self.content_type = Mime::JS
119
+ if content_type.nil? || content_type == Mime[:json]
120
+ self.content_type = Mime[:js]
119
121
  end
120
122
 
121
123
  "/**/#{options[:callback]}(#{json})"
122
124
  else
123
- self.content_type ||= Mime::JSON
125
+ self.content_type ||= Mime[:json]
124
126
  json
125
127
  end
126
128
  end
127
129
 
128
130
  add :js do |js, options|
129
- self.content_type ||= Mime::JS
131
+ self.content_type ||= Mime[:js]
130
132
  js.respond_to?(:to_js) ? js.to_js(options) : js
131
133
  end
132
134
 
133
135
  add :xml do |xml, options|
134
- self.content_type ||= Mime::XML
136
+ self.content_type ||= Mime[:xml]
135
137
  xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml
136
138
  end
137
139
  end