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.

Files changed (166) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +285 -444
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +6 -7
  5. data/lib/abstract_controller.rb +12 -5
  6. data/lib/abstract_controller/asset_paths.rb +2 -0
  7. data/lib/abstract_controller/base.rb +45 -49
  8. data/lib/abstract_controller/caching.rb +66 -0
  9. data/lib/{action_controller → abstract_controller}/caching/fragments.rb +78 -15
  10. data/lib/abstract_controller/callbacks.rb +47 -31
  11. data/lib/abstract_controller/collector.rb +8 -11
  12. data/lib/abstract_controller/error.rb +6 -0
  13. data/lib/abstract_controller/helpers.rb +25 -25
  14. data/lib/abstract_controller/logger.rb +2 -0
  15. data/lib/abstract_controller/railties/routes_helpers.rb +4 -2
  16. data/lib/abstract_controller/rendering.rb +42 -41
  17. data/lib/abstract_controller/translation.rb +10 -7
  18. data/lib/abstract_controller/url_for.rb +2 -0
  19. data/lib/action_controller.rb +29 -21
  20. data/lib/action_controller/api.rb +149 -0
  21. data/lib/action_controller/api/api_rendering.rb +16 -0
  22. data/lib/action_controller/base.rb +27 -19
  23. data/lib/action_controller/caching.rb +14 -57
  24. data/lib/action_controller/form_builder.rb +50 -0
  25. data/lib/action_controller/log_subscriber.rb +10 -15
  26. data/lib/action_controller/metal.rb +98 -83
  27. data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
  28. data/lib/action_controller/metal/conditional_get.rb +118 -44
  29. data/lib/action_controller/metal/content_security_policy.rb +52 -0
  30. data/lib/action_controller/metal/cookies.rb +3 -3
  31. data/lib/action_controller/metal/data_streaming.rb +27 -46
  32. data/lib/action_controller/metal/etag_with_flash.rb +18 -0
  33. data/lib/action_controller/metal/etag_with_template_digest.rb +20 -13
  34. data/lib/action_controller/metal/exceptions.rb +8 -14
  35. data/lib/action_controller/metal/flash.rb +4 -3
  36. data/lib/action_controller/metal/force_ssl.rb +23 -21
  37. data/lib/action_controller/metal/head.rb +21 -19
  38. data/lib/action_controller/metal/helpers.rb +24 -14
  39. data/lib/action_controller/metal/http_authentication.rb +64 -57
  40. data/lib/action_controller/metal/implicit_render.rb +62 -8
  41. data/lib/action_controller/metal/instrumentation.rb +19 -21
  42. data/lib/action_controller/metal/live.rb +90 -106
  43. data/lib/action_controller/metal/mime_responds.rb +33 -46
  44. data/lib/action_controller/metal/parameter_encoding.rb +51 -0
  45. data/lib/action_controller/metal/params_wrapper.rb +61 -53
  46. data/lib/action_controller/metal/redirecting.rb +49 -28
  47. data/lib/action_controller/metal/renderers.rb +87 -44
  48. data/lib/action_controller/metal/rendering.rb +72 -50
  49. data/lib/action_controller/metal/request_forgery_protection.rb +203 -92
  50. data/lib/action_controller/metal/rescue.rb +9 -16
  51. data/lib/action_controller/metal/streaming.rb +12 -10
  52. data/lib/action_controller/metal/strong_parameters.rb +582 -165
  53. data/lib/action_controller/metal/testing.rb +2 -17
  54. data/lib/action_controller/metal/url_for.rb +19 -10
  55. data/lib/action_controller/railtie.rb +28 -10
  56. data/lib/action_controller/railties/helpers.rb +2 -0
  57. data/lib/action_controller/renderer.rb +117 -0
  58. data/lib/action_controller/template_assertions.rb +11 -0
  59. data/lib/action_controller/test_case.rb +280 -411
  60. data/lib/action_dispatch.rb +27 -19
  61. data/lib/action_dispatch/http/cache.rb +93 -47
  62. data/lib/action_dispatch/http/content_security_policy.rb +272 -0
  63. data/lib/action_dispatch/http/filter_parameters.rb +26 -20
  64. data/lib/action_dispatch/http/filter_redirect.rb +10 -11
  65. data/lib/action_dispatch/http/headers.rb +55 -22
  66. data/lib/action_dispatch/http/mime_negotiation.rb +60 -41
  67. data/lib/action_dispatch/http/mime_type.rb +134 -121
  68. data/lib/action_dispatch/http/mime_types.rb +20 -6
  69. data/lib/action_dispatch/http/parameter_filter.rb +25 -11
  70. data/lib/action_dispatch/http/parameters.rb +98 -39
  71. data/lib/action_dispatch/http/rack_cache.rb +2 -0
  72. data/lib/action_dispatch/http/request.rb +200 -118
  73. data/lib/action_dispatch/http/response.rb +225 -110
  74. data/lib/action_dispatch/http/upload.rb +12 -6
  75. data/lib/action_dispatch/http/url.rb +110 -28
  76. data/lib/action_dispatch/journey.rb +7 -5
  77. data/lib/action_dispatch/journey/formatter.rb +55 -32
  78. data/lib/action_dispatch/journey/gtg/builder.rb +7 -5
  79. data/lib/action_dispatch/journey/gtg/simulator.rb +3 -9
  80. data/lib/action_dispatch/journey/gtg/transition_table.rb +17 -16
  81. data/lib/action_dispatch/journey/nfa/builder.rb +5 -3
  82. data/lib/action_dispatch/journey/nfa/dot.rb +13 -13
  83. data/lib/action_dispatch/journey/nfa/simulator.rb +3 -1
  84. data/lib/action_dispatch/journey/nfa/transition_table.rb +5 -48
  85. data/lib/action_dispatch/journey/nodes/node.rb +18 -6
  86. data/lib/action_dispatch/journey/parser.rb +23 -22
  87. data/lib/action_dispatch/journey/parser.y +3 -2
  88. data/lib/action_dispatch/journey/parser_extras.rb +12 -4
  89. data/lib/action_dispatch/journey/path/pattern.rb +50 -44
  90. data/lib/action_dispatch/journey/route.rb +106 -28
  91. data/lib/action_dispatch/journey/router.rb +35 -23
  92. data/lib/action_dispatch/journey/router/utils.rb +20 -11
  93. data/lib/action_dispatch/journey/routes.rb +18 -16
  94. data/lib/action_dispatch/journey/scanner.rb +18 -15
  95. data/lib/action_dispatch/journey/visitors.rb +99 -52
  96. data/lib/action_dispatch/middleware/callbacks.rb +1 -2
  97. data/lib/action_dispatch/middleware/cookies.rb +304 -193
  98. data/lib/action_dispatch/middleware/debug_exceptions.rb +152 -57
  99. data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
  100. data/lib/action_dispatch/middleware/exception_wrapper.rb +68 -69
  101. data/lib/action_dispatch/middleware/executor.rb +21 -0
  102. data/lib/action_dispatch/middleware/flash.rb +78 -54
  103. data/lib/action_dispatch/middleware/public_exceptions.rb +27 -25
  104. data/lib/action_dispatch/middleware/reloader.rb +5 -91
  105. data/lib/action_dispatch/middleware/remote_ip.rb +41 -31
  106. data/lib/action_dispatch/middleware/request_id.rb +17 -9
  107. data/lib/action_dispatch/middleware/session/abstract_store.rb +41 -25
  108. data/lib/action_dispatch/middleware/session/cache_store.rb +24 -14
  109. data/lib/action_dispatch/middleware/session/cookie_store.rb +72 -67
  110. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +8 -2
  111. data/lib/action_dispatch/middleware/show_exceptions.rb +26 -22
  112. data/lib/action_dispatch/middleware/ssl.rb +114 -36
  113. data/lib/action_dispatch/middleware/stack.rb +31 -44
  114. data/lib/action_dispatch/middleware/static.rb +57 -50
  115. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +2 -14
  116. data/lib/action_dispatch/middleware/templates/rescues/{_source.erb → _source.html.erb} +0 -0
  117. data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
  118. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +21 -0
  119. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +13 -0
  120. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +1 -0
  121. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +1 -1
  122. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +1 -1
  123. data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +4 -4
  124. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +64 -64
  125. data/lib/action_dispatch/railtie.rb +19 -11
  126. data/lib/action_dispatch/request/session.rb +106 -59
  127. data/lib/action_dispatch/request/utils.rb +67 -24
  128. data/lib/action_dispatch/routing.rb +17 -18
  129. data/lib/action_dispatch/routing/endpoint.rb +9 -2
  130. data/lib/action_dispatch/routing/inspector.rb +58 -67
  131. data/lib/action_dispatch/routing/mapper.rb +734 -447
  132. data/lib/action_dispatch/routing/polymorphic_routes.rb +161 -139
  133. data/lib/action_dispatch/routing/redirection.rb +36 -26
  134. data/lib/action_dispatch/routing/route_set.rb +321 -291
  135. data/lib/action_dispatch/routing/routes_proxy.rb +32 -5
  136. data/lib/action_dispatch/routing/url_for.rb +65 -25
  137. data/lib/action_dispatch/system_test_case.rb +147 -0
  138. data/lib/action_dispatch/system_testing/browser.rb +49 -0
  139. data/lib/action_dispatch/system_testing/driver.rb +59 -0
  140. data/lib/action_dispatch/system_testing/server.rb +31 -0
  141. data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +96 -0
  142. data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +31 -0
  143. data/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb +26 -0
  144. data/lib/action_dispatch/testing/assertion_response.rb +47 -0
  145. data/lib/action_dispatch/testing/assertions.rb +6 -4
  146. data/lib/action_dispatch/testing/assertions/response.rb +45 -20
  147. data/lib/action_dispatch/testing/assertions/routing.rb +30 -26
  148. data/lib/action_dispatch/testing/integration.rb +347 -209
  149. data/lib/action_dispatch/testing/request_encoder.rb +55 -0
  150. data/lib/action_dispatch/testing/test_process.rb +28 -22
  151. data/lib/action_dispatch/testing/test_request.rb +27 -34
  152. data/lib/action_dispatch/testing/test_response.rb +35 -7
  153. data/lib/action_pack.rb +4 -2
  154. data/lib/action_pack/gem_version.rb +5 -3
  155. data/lib/action_pack/version.rb +3 -1
  156. metadata +56 -39
  157. data/lib/action_controller/metal/hide_actions.rb +0 -40
  158. data/lib/action_controller/metal/rack_delegation.rb +0 -32
  159. data/lib/action_controller/middleware.rb +0 -39
  160. data/lib/action_controller/model_naming.rb +0 -12
  161. data/lib/action_dispatch/journey/backwards.rb +0 -5
  162. data/lib/action_dispatch/journey/router/strexp.rb +0 -27
  163. data/lib/action_dispatch/middleware/params_parser.rb +0 -60
  164. data/lib/action_dispatch/testing/assertions/dom.rb +0 -3
  165. data/lib/action_dispatch/testing/assertions/selector.rb +0 -3
  166. data/lib/action_dispatch/testing/assertions/tag.rb +0 -3
@@ -1,4 +1,4 @@
1
- require 'action_controller/model_naming'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ActionDispatch
4
4
  module Routing
@@ -6,7 +6,7 @@ module ActionDispatch
6
6
  # given an Active Record model instance. They are to be used in combination with
7
7
  # ActionController::Resources.
8
8
  #
9
- # These methods are useful when you want to generate correct URL or path to a RESTful
9
+ # These methods are useful when you want to generate the correct URL or path to a RESTful
10
10
  # resource without having to know the exact type of the record in question.
11
11
  #
12
12
  # Nested resources and/or namespaces are also supported, as illustrated in the example:
@@ -42,7 +42,7 @@ module ActionDispatch
42
42
  #
43
43
  # Example usage:
44
44
  #
45
- # edit_polymorphic_path(@post) # => "/posts/1/edit"
45
+ # edit_polymorphic_path(@post) # => "/posts/1/edit"
46
46
  # polymorphic_path(@post, format: :pdf) # => "/posts/1.pdf"
47
47
  #
48
48
  # == Usage with mounted engines
@@ -55,8 +55,6 @@ module ActionDispatch
55
55
  # form_for([blog, @post]) # => "/blog/posts/1"
56
56
  #
57
57
  module PolymorphicRoutes
58
- include ActionController::ModelNaming
59
-
60
58
  # Constructs a call to a named RESTful route for the given record and returns the
61
59
  # resulting URL string. For example:
62
60
  #
@@ -83,7 +81,7 @@ module ActionDispatch
83
81
  # polymorphic_url([blog, post], anchor: 'my_anchor', script_name: "/my_app")
84
82
  # # => "http://example.com/my_app/blogs/1/posts/1#my_anchor"
85
83
  #
86
- # For all of these options, see the documentation for <tt>url_for</tt>.
84
+ # For all of these options, see the documentation for {url_for}[rdoc-ref:ActionDispatch::Routing::UrlFor].
87
85
  #
88
86
  # ==== Functionality
89
87
  #
@@ -107,6 +105,10 @@ module ActionDispatch
107
105
  return polymorphic_url record, options
108
106
  end
109
107
 
108
+ if mapping = polymorphic_mapping(record_or_hash_or_array)
109
+ return mapping.call(self, [record_or_hash_or_array, options], false)
110
+ end
111
+
110
112
  opts = options.dup
111
113
  action = opts.delete :action
112
114
  type = opts.delete(:routing_type) || :url
@@ -127,6 +129,10 @@ module ActionDispatch
127
129
  return polymorphic_path record, options
128
130
  end
129
131
 
132
+ if mapping = polymorphic_mapping(record_or_hash_or_array)
133
+ return mapping.call(self, [record_or_hash_or_array, options], true)
134
+ end
135
+
130
136
  opts = options.dup
131
137
  action = opts.delete :action
132
138
  type = :path
@@ -138,7 +144,6 @@ module ActionDispatch
138
144
  opts
139
145
  end
140
146
 
141
-
142
147
  %w(edit new).each do |action|
143
148
  module_eval <<-EOT, __FILE__, __LINE__ + 1
144
149
  def #{action}_polymorphic_url(record_or_hash, options = {})
@@ -153,178 +158,195 @@ module ActionDispatch
153
158
 
154
159
  private
155
160
 
156
- def polymorphic_url_for_action(action, record_or_hash, options)
157
- polymorphic_url(record_or_hash, options.merge(:action => action))
158
- end
159
-
160
- def polymorphic_path_for_action(action, record_or_hash, options)
161
- polymorphic_path(record_or_hash, options.merge(:action => action))
162
- end
163
-
164
- class HelperMethodBuilder # :nodoc:
165
- CACHE = { 'path' => {}, 'url' => {} }
166
-
167
- def self.get(action, type)
168
- type = type.to_s
169
- CACHE[type].fetch(action) { build action, type }
161
+ def polymorphic_url_for_action(action, record_or_hash, options)
162
+ polymorphic_url(record_or_hash, options.merge(action: action))
170
163
  end
171
164
 
172
- def self.url; CACHE['url'.freeze][nil]; end
173
- def self.path; CACHE['path'.freeze][nil]; end
165
+ def polymorphic_path_for_action(action, record_or_hash, options)
166
+ polymorphic_path(record_or_hash, options.merge(action: action))
167
+ end
174
168
 
175
- def self.build(action, type)
176
- prefix = action ? "#{action}_" : ""
177
- suffix = type
178
- if action.to_s == 'new'
179
- HelperMethodBuilder.singular prefix, suffix
169
+ def polymorphic_mapping(record)
170
+ if record.respond_to?(:to_model)
171
+ _routes.polymorphic_mappings[record.to_model.model_name.name]
180
172
  else
181
- HelperMethodBuilder.plural prefix, suffix
173
+ _routes.polymorphic_mappings[record.class.name]
182
174
  end
183
175
  end
184
176
 
185
- def self.singular(prefix, suffix)
186
- new(->(name) { name.singular_route_key }, prefix, suffix)
187
- end
177
+ class HelperMethodBuilder # :nodoc:
178
+ CACHE = { "path" => {}, "url" => {} }
188
179
 
189
- def self.plural(prefix, suffix)
190
- new(->(name) { name.route_key }, prefix, suffix)
191
- end
180
+ def self.get(action, type)
181
+ type = type.to_s
182
+ CACHE[type].fetch(action) { build action, type }
183
+ end
192
184
 
193
- def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
194
- builder = get action, type
185
+ def self.url; CACHE["url".freeze][nil]; end
186
+ def self.path; CACHE["path".freeze][nil]; end
195
187
 
196
- case record_or_hash_or_array
197
- when Array
198
- record_or_hash_or_array = record_or_hash_or_array.compact
199
- if record_or_hash_or_array.empty?
200
- raise ArgumentError, "Nil location provided. Can't build URI."
201
- end
202
- if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
203
- recipient = record_or_hash_or_array.shift
188
+ def self.build(action, type)
189
+ prefix = action ? "#{action}_" : ""
190
+ suffix = type
191
+ if action.to_s == "new"
192
+ HelperMethodBuilder.singular prefix, suffix
193
+ else
194
+ HelperMethodBuilder.plural prefix, suffix
204
195
  end
205
-
206
- method, args = builder.handle_list record_or_hash_or_array
207
- when String, Symbol
208
- method, args = builder.handle_string record_or_hash_or_array
209
- when Class
210
- method, args = builder.handle_class record_or_hash_or_array
211
-
212
- when nil
213
- raise ArgumentError, "Nil location provided. Can't build URI."
214
- else
215
- method, args = builder.handle_model record_or_hash_or_array
216
196
  end
217
197
 
198
+ def self.singular(prefix, suffix)
199
+ new(->(name) { name.singular_route_key }, prefix, suffix)
200
+ end
218
201
 
219
- if options.empty?
220
- recipient.send(method, *args)
221
- else
222
- recipient.send(method, *args, options)
202
+ def self.plural(prefix, suffix)
203
+ new(->(name) { name.route_key }, prefix, suffix)
223
204
  end
224
- end
225
205
 
226
- attr_reader :suffix, :prefix
206
+ def self.polymorphic_method(recipient, record_or_hash_or_array, action, type, options)
207
+ builder = get action, type
208
+
209
+ case record_or_hash_or_array
210
+ when Array
211
+ record_or_hash_or_array = record_or_hash_or_array.compact
212
+ if record_or_hash_or_array.empty?
213
+ raise ArgumentError, "Nil location provided. Can't build URI."
214
+ end
215
+ if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
216
+ recipient = record_or_hash_or_array.shift
217
+ end
218
+
219
+ method, args = builder.handle_list record_or_hash_or_array
220
+ when String, Symbol
221
+ method, args = builder.handle_string record_or_hash_or_array
222
+ when Class
223
+ method, args = builder.handle_class record_or_hash_or_array
227
224
 
228
- def initialize(key_strategy, prefix, suffix)
229
- @key_strategy = key_strategy
230
- @prefix = prefix
231
- @suffix = suffix
232
- end
225
+ when nil
226
+ raise ArgumentError, "Nil location provided. Can't build URI."
227
+ else
228
+ method, args = builder.handle_model record_or_hash_or_array
229
+ end
233
230
 
234
- def handle_string(record)
235
- [get_method_for_string(record), []]
236
- end
231
+ if options.empty?
232
+ recipient.send(method, *args)
233
+ else
234
+ recipient.send(method, *args, options)
235
+ end
236
+ end
237
237
 
238
- def handle_string_call(target, str)
239
- target.send get_method_for_string str
240
- end
238
+ attr_reader :suffix, :prefix
241
239
 
242
- def handle_class(klass)
243
- [get_method_for_class(klass), []]
244
- end
240
+ def initialize(key_strategy, prefix, suffix)
241
+ @key_strategy = key_strategy
242
+ @prefix = prefix
243
+ @suffix = suffix
244
+ end
245
245
 
246
- def handle_class_call(target, klass)
247
- target.send get_method_for_class klass
248
- end
246
+ def handle_string(record)
247
+ [get_method_for_string(record), []]
248
+ end
249
249
 
250
- def handle_model(record)
251
- args = []
250
+ def handle_string_call(target, str)
251
+ target.send get_method_for_string str
252
+ end
252
253
 
253
- model = record.to_model
254
- name = if model.persisted?
255
- args << model
256
- model.model_name.singular_route_key
257
- else
258
- @key_strategy.call model.model_name
259
- end
254
+ def handle_class(klass)
255
+ [get_method_for_class(klass), []]
256
+ end
260
257
 
261
- named_route = prefix + "#{name}_#{suffix}"
258
+ def handle_class_call(target, klass)
259
+ target.send get_method_for_class klass
260
+ end
262
261
 
263
- [named_route, args]
264
- end
262
+ def handle_model(record)
263
+ args = []
265
264
 
266
- def handle_model_call(target, model)
267
- method, args = handle_model model
268
- target.send(method, *args)
269
- end
265
+ model = record.to_model
266
+ named_route = if model.persisted?
267
+ args << model
268
+ get_method_for_string model.model_name.singular_route_key
269
+ else
270
+ get_method_for_class model
271
+ end
270
272
 
271
- def handle_list(list)
272
- record_list = list.dup
273
- record = record_list.pop
273
+ [named_route, args]
274
+ end
274
275
 
275
- args = []
276
+ def handle_model_call(target, record)
277
+ if mapping = polymorphic_mapping(target, record)
278
+ mapping.call(target, [record], suffix == "path")
279
+ else
280
+ method, args = handle_model(record)
281
+ target.send(method, *args)
282
+ end
283
+ end
276
284
 
277
- route = record_list.map { |parent|
278
- case parent
285
+ def handle_list(list)
286
+ record_list = list.dup
287
+ record = record_list.pop
288
+
289
+ args = []
290
+
291
+ route = record_list.map { |parent|
292
+ case parent
293
+ when Symbol, String
294
+ parent.to_s
295
+ when Class
296
+ args << parent
297
+ parent.model_name.singular_route_key
298
+ else
299
+ args << parent.to_model
300
+ parent.to_model.model_name.singular_route_key
301
+ end
302
+ }
303
+
304
+ route <<
305
+ case record
279
306
  when Symbol, String
280
- parent.to_s
307
+ record.to_s
281
308
  when Class
282
- args << parent
283
- parent.model_name.singular_route_key
309
+ @key_strategy.call record.model_name
284
310
  else
285
- args << parent.to_model
286
- parent.to_model.model_name.singular_route_key
311
+ model = record.to_model
312
+ if model.persisted?
313
+ args << model
314
+ model.model_name.singular_route_key
315
+ else
316
+ @key_strategy.call model.model_name
317
+ end
287
318
  end
288
- }
289
-
290
- route <<
291
- case record
292
- when Symbol, String
293
- record.to_s
294
- when Class
295
- @key_strategy.call record.model_name
296
- else
297
- model = record.to_model
298
- if model.persisted?
299
- args << model
300
- model.model_name.singular_route_key
301
- else
302
- @key_strategy.call model.model_name
303
- end
304
- end
305
319
 
306
- route << suffix
320
+ route << suffix
307
321
 
308
- named_route = prefix + route.join("_")
309
- [named_route, args]
310
- end
322
+ named_route = prefix + route.join("_")
323
+ [named_route, args]
324
+ end
311
325
 
312
- private
326
+ private
313
327
 
314
- def get_method_for_class(klass)
315
- name = @key_strategy.call klass.model_name
316
- prefix + "#{name}_#{suffix}"
317
- end
328
+ def polymorphic_mapping(target, record)
329
+ if record.respond_to?(:to_model)
330
+ target._routes.polymorphic_mappings[record.to_model.model_name.name]
331
+ else
332
+ target._routes.polymorphic_mappings[record.class.name]
333
+ end
334
+ end
318
335
 
319
- def get_method_for_string(str)
320
- prefix + "#{str}_#{suffix}"
321
- end
336
+ def get_method_for_class(klass)
337
+ name = @key_strategy.call klass.model_name
338
+ get_method_for_string name
339
+ end
322
340
 
323
- [nil, 'new', 'edit'].each do |action|
324
- CACHE['url'][action] = build action, 'url'
325
- CACHE['path'][action] = build action, 'path'
341
+ def get_method_for_string(str)
342
+ "#{prefix}#{str}_#{suffix}"
343
+ end
344
+
345
+ [nil, "new", "edit"].each do |action|
346
+ CACHE["url"][action] = build action, "url"
347
+ CACHE["path"][action] = build action, "path"
348
+ end
326
349
  end
327
- end
328
350
  end
329
351
  end
330
352
  end
@@ -1,9 +1,11 @@
1
- require 'action_dispatch/http/request'
2
- require 'active_support/core_ext/uri'
3
- require 'active_support/core_ext/array/extract_options'
4
- require 'rack/utils'
5
- require 'action_controller/metal/exceptions'
6
- require 'action_dispatch/routing/endpoint'
1
+ # frozen_string_literal: true
2
+
3
+ require "action_dispatch/http/request"
4
+ require "active_support/core_ext/uri"
5
+ require "active_support/core_ext/array/extract_options"
6
+ require "rack/utils"
7
+ require "action_controller/metal/exceptions"
8
+ require "action_dispatch/routing/endpoint"
7
9
 
8
10
  module ActionDispatch
9
11
  module Routing
@@ -22,9 +24,8 @@ module ActionDispatch
22
24
  end
23
25
 
24
26
  def serve(req)
25
- req.check_path_parameters!
26
27
  uri = URI.parse(path(req.path_parameters, req))
27
-
28
+
28
29
  unless uri.host
29
30
  if relative_path?(uri.path)
30
31
  uri.path = "#{req.script_name}/#{uri.path}"
@@ -32,17 +33,19 @@ module ActionDispatch
32
33
  uri.path = req.script_name.empty? ? "/" : req.script_name
33
34
  end
34
35
  end
35
-
36
+
36
37
  uri.scheme ||= req.scheme
37
38
  uri.host ||= req.host
38
39
  uri.port ||= req.port unless req.standard_port?
39
40
 
41
+ req.commit_flash
42
+
40
43
  body = %(<html><body>You are being <a href="#{ERB::Util.unwrapped_html_escape(uri.to_s)}">redirected</a>.</body></html>)
41
44
 
42
45
  headers = {
43
- 'Location' => uri.to_s,
44
- 'Content-Type' => 'text/html',
45
- 'Content-Length' => body.length.to_s
46
+ "Location" => uri.to_s,
47
+ "Content-Type" => "text/html",
48
+ "Content-Length" => body.length.to_s
46
49
  }
47
50
 
48
51
  [ status, headers, [body] ]
@@ -58,19 +61,19 @@ module ActionDispatch
58
61
 
59
62
  private
60
63
  def relative_path?(path)
61
- path && !path.empty? && path[0] != '/'
64
+ path && !path.empty? && path[0] != "/"
62
65
  end
63
66
 
64
67
  def escape(params)
65
- Hash[params.map{ |k,v| [k, Rack::Utils.escape(v)] }]
68
+ Hash[params.map { |k, v| [k, Rack::Utils.escape(v)] }]
66
69
  end
67
70
 
68
71
  def escape_fragment(params)
69
- Hash[params.map{ |k,v| [k, Journey::Router::Utils.escape_fragment(v)] }]
72
+ Hash[params.map { |k, v| [k, Journey::Router::Utils.escape_fragment(v)] }]
70
73
  end
71
74
 
72
75
  def escape_path(params)
73
- Hash[params.map{ |k,v| [k, Journey::Router::Utils.escape_path(v)] }]
76
+ Hash[params.map { |k, v| [k, Journey::Router::Utils.escape_path(v)] }]
74
77
  end
75
78
  end
76
79
 
@@ -104,11 +107,11 @@ module ActionDispatch
104
107
 
105
108
  def path(params, request)
106
109
  url_options = {
107
- :protocol => request.protocol,
108
- :host => request.host,
109
- :port => request.optional_port,
110
- :path => request.path,
111
- :params => request.query_parameters
110
+ protocol: request.protocol,
111
+ host: request.host,
112
+ port: request.optional_port,
113
+ path: request.path,
114
+ params: request.query_parameters
112
115
  }.merge! options
113
116
 
114
117
  if !params.empty? && url_options[:path].match(/%\{\w*\}/)
@@ -124,26 +127,28 @@ module ActionDispatch
124
127
  url_options[:script_name] = request.script_name
125
128
  end
126
129
  end
127
-
130
+
128
131
  ActionDispatch::Http::URL.url_for url_options
129
132
  end
130
133
 
131
134
  def inspect
132
- "redirect(#{status}, #{options.map{ |k,v| "#{k}: #{v}" }.join(', ')})"
135
+ "redirect(#{status}, #{options.map { |k, v| "#{k}: #{v}" }.join(', ')})"
133
136
  end
134
137
  end
135
138
 
136
139
  module Redirection
137
-
138
140
  # Redirect any path to another path:
139
141
  #
140
142
  # get "/stories" => redirect("/posts")
141
143
  #
144
+ # This will redirect the user, while ignoring certain parts of the request, including query string, etc.
145
+ # <tt>/stories</tt>, <tt>/stories?foo=bar</tt>, etc all redirect to <tt>/posts</tt>.
146
+ #
142
147
  # You can also use interpolation in the supplied redirect argument:
143
148
  #
144
149
  # get 'docs/:article', to: redirect('/wiki/%{article}')
145
150
  #
146
- # Note that if you return a path without a leading slash then the url is prefixed with the
151
+ # Note that if you return a path without a leading slash then the URL is prefixed with the
147
152
  # current SCRIPT_NAME environment variable. This is typically '/' but may be different in
148
153
  # a mounted engine or where the application is deployed to a subdirectory of a website.
149
154
  #
@@ -162,11 +167,16 @@ module ActionDispatch
162
167
  # Note that the +do end+ syntax for the redirect block wouldn't work, as Ruby would pass
163
168
  # the block to +get+ instead of +redirect+. Use <tt>{ ... }</tt> instead.
164
169
  #
165
- # The options version of redirect allows you to supply only the parts of the url which need
170
+ # The options version of redirect allows you to supply only the parts of the URL which need
166
171
  # to change, it also supports interpolation of the path similar to the first example.
167
172
  #
168
173
  # get 'stores/:name', to: redirect(subdomain: 'stores', path: '/%{name}')
169
174
  # get 'stores/:name(*all)', to: redirect(subdomain: 'stores', path: '/%{name}%{all}')
175
+ # get '/stories', to: redirect(path: '/posts')
176
+ #
177
+ # This will redirect the user, while changing only the specified parts of the request,
178
+ # for example the +path+ option in the last example.
179
+ # <tt>/stories</tt>, <tt>/stories?foo=bar</tt>, redirect to <tt>/posts</tt> and <tt>/posts?foo=bar</tt> respectively.
170
180
  #
171
181
  # Finally, an object which responds to call can be supplied to redirect, allowing you to reuse
172
182
  # common redirect routes. The call method must accept two arguments, params and request, and return