actionpack 3.2.22.5 → 5.2.4

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 (271) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +279 -603
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +13 -297
  5. data/lib/abstract_controller/asset_paths.rb +4 -2
  6. data/lib/abstract_controller/base.rb +82 -52
  7. data/lib/abstract_controller/caching/fragments.rb +166 -0
  8. data/lib/abstract_controller/caching.rb +66 -0
  9. data/lib/abstract_controller/callbacks.rb +117 -103
  10. data/lib/abstract_controller/collector.rb +18 -7
  11. data/lib/abstract_controller/error.rb +6 -0
  12. data/lib/abstract_controller/helpers.rb +65 -38
  13. data/lib/abstract_controller/logger.rb +3 -2
  14. data/lib/abstract_controller/railties/routes_helpers.rb +5 -3
  15. data/lib/abstract_controller/rendering.rb +77 -129
  16. data/lib/abstract_controller/translation.rb +21 -3
  17. data/lib/abstract_controller/url_for.rb +9 -7
  18. data/lib/abstract_controller.rb +12 -13
  19. data/lib/action_controller/api/api_rendering.rb +16 -0
  20. data/lib/action_controller/api.rb +149 -0
  21. data/lib/action_controller/base.rb +81 -40
  22. data/lib/action_controller/caching.rb +22 -62
  23. data/lib/action_controller/form_builder.rb +50 -0
  24. data/lib/action_controller/log_subscriber.rb +30 -18
  25. data/lib/action_controller/metal/basic_implicit_render.rb +13 -0
  26. data/lib/action_controller/metal/conditional_get.rb +190 -47
  27. data/lib/action_controller/metal/content_security_policy.rb +52 -0
  28. data/lib/action_controller/metal/cookies.rb +3 -3
  29. data/lib/action_controller/metal/data_streaming.rb +40 -65
  30. data/lib/action_controller/metal/etag_with_flash.rb +18 -0
  31. data/lib/action_controller/metal/etag_with_template_digest.rb +57 -0
  32. data/lib/action_controller/metal/exceptions.rb +19 -12
  33. data/lib/action_controller/metal/flash.rb +42 -9
  34. data/lib/action_controller/metal/force_ssl.rb +79 -19
  35. data/lib/action_controller/metal/head.rb +35 -10
  36. data/lib/action_controller/metal/helpers.rb +31 -21
  37. data/lib/action_controller/metal/http_authentication.rb +182 -134
  38. data/lib/action_controller/metal/implicit_render.rb +62 -8
  39. data/lib/action_controller/metal/instrumentation.rb +28 -26
  40. data/lib/action_controller/metal/live.rb +312 -0
  41. data/lib/action_controller/metal/mime_responds.rb +159 -163
  42. data/lib/action_controller/metal/parameter_encoding.rb +51 -0
  43. data/lib/action_controller/metal/params_wrapper.rb +146 -93
  44. data/lib/action_controller/metal/redirecting.rb +80 -56
  45. data/lib/action_controller/metal/renderers.rb +119 -47
  46. data/lib/action_controller/metal/rendering.rb +89 -32
  47. data/lib/action_controller/metal/request_forgery_protection.rb +373 -41
  48. data/lib/action_controller/metal/rescue.rb +9 -16
  49. data/lib/action_controller/metal/streaming.rb +39 -45
  50. data/lib/action_controller/metal/strong_parameters.rb +1086 -0
  51. data/lib/action_controller/metal/testing.rb +8 -29
  52. data/lib/action_controller/metal/url_for.rb +43 -32
  53. data/lib/action_controller/metal.rb +112 -106
  54. data/lib/action_controller/railtie.rb +56 -18
  55. data/lib/action_controller/railties/helpers.rb +24 -0
  56. data/lib/action_controller/renderer.rb +117 -0
  57. data/lib/action_controller/template_assertions.rb +11 -0
  58. data/lib/action_controller/test_case.rb +402 -347
  59. data/lib/action_controller.rb +31 -30
  60. data/lib/action_dispatch/http/cache.rb +133 -34
  61. data/lib/action_dispatch/http/content_security_policy.rb +272 -0
  62. data/lib/action_dispatch/http/filter_parameters.rb +40 -24
  63. data/lib/action_dispatch/http/filter_redirect.rb +37 -0
  64. data/lib/action_dispatch/http/headers.rb +117 -16
  65. data/lib/action_dispatch/http/mime_negotiation.rb +98 -33
  66. data/lib/action_dispatch/http/mime_type.rb +198 -146
  67. data/lib/action_dispatch/http/mime_types.rb +22 -7
  68. data/lib/action_dispatch/http/parameter_filter.rb +61 -49
  69. data/lib/action_dispatch/http/parameters.rb +94 -51
  70. data/lib/action_dispatch/http/rack_cache.rb +4 -3
  71. data/lib/action_dispatch/http/request.rb +262 -117
  72. data/lib/action_dispatch/http/response.rb +400 -86
  73. data/lib/action_dispatch/http/upload.rb +66 -29
  74. data/lib/action_dispatch/http/url.rb +232 -60
  75. data/lib/action_dispatch/journey/formatter.rb +189 -0
  76. data/lib/action_dispatch/journey/gtg/builder.rb +164 -0
  77. data/lib/action_dispatch/journey/gtg/simulator.rb +41 -0
  78. data/lib/action_dispatch/journey/gtg/transition_table.rb +158 -0
  79. data/lib/action_dispatch/journey/nfa/builder.rb +78 -0
  80. data/lib/action_dispatch/journey/nfa/dot.rb +36 -0
  81. data/lib/action_dispatch/journey/nfa/simulator.rb +49 -0
  82. data/lib/action_dispatch/journey/nfa/transition_table.rb +120 -0
  83. data/lib/action_dispatch/journey/nodes/node.rb +140 -0
  84. data/lib/action_dispatch/journey/parser.rb +199 -0
  85. data/lib/action_dispatch/journey/parser.y +50 -0
  86. data/lib/action_dispatch/journey/parser_extras.rb +31 -0
  87. data/lib/action_dispatch/journey/path/pattern.rb +199 -0
  88. data/lib/action_dispatch/journey/route.rb +203 -0
  89. data/lib/action_dispatch/journey/router/utils.rb +102 -0
  90. data/lib/action_dispatch/journey/router.rb +156 -0
  91. data/lib/action_dispatch/journey/routes.rb +82 -0
  92. data/lib/action_dispatch/journey/scanner.rb +64 -0
  93. data/lib/action_dispatch/journey/visitors.rb +268 -0
  94. data/lib/action_dispatch/journey/visualizer/fsm.css +30 -0
  95. data/lib/action_dispatch/journey/visualizer/fsm.js +134 -0
  96. data/lib/action_dispatch/journey/visualizer/index.html.erb +52 -0
  97. data/lib/action_dispatch/journey.rb +7 -0
  98. data/lib/action_dispatch/middleware/callbacks.rb +17 -13
  99. data/lib/action_dispatch/middleware/cookies.rb +494 -162
  100. data/lib/action_dispatch/middleware/debug_exceptions.rb +176 -53
  101. data/lib/action_dispatch/middleware/debug_locks.rb +124 -0
  102. data/lib/action_dispatch/middleware/exception_wrapper.rb +103 -38
  103. data/lib/action_dispatch/middleware/executor.rb +21 -0
  104. data/lib/action_dispatch/middleware/flash.rb +128 -91
  105. data/lib/action_dispatch/middleware/public_exceptions.rb +43 -16
  106. data/lib/action_dispatch/middleware/reloader.rb +6 -83
  107. data/lib/action_dispatch/middleware/remote_ip.rb +151 -49
  108. data/lib/action_dispatch/middleware/request_id.rb +19 -15
  109. data/lib/action_dispatch/middleware/session/abstract_store.rb +38 -34
  110. data/lib/action_dispatch/middleware/session/cache_store.rb +14 -9
  111. data/lib/action_dispatch/middleware/session/cookie_store.rb +94 -44
  112. data/lib/action_dispatch/middleware/session/mem_cache_store.rb +15 -4
  113. data/lib/action_dispatch/middleware/show_exceptions.rb +36 -61
  114. data/lib/action_dispatch/middleware/ssl.rb +150 -0
  115. data/lib/action_dispatch/middleware/stack.rb +33 -41
  116. data/lib/action_dispatch/middleware/static.rb +92 -48
  117. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb +22 -0
  118. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb +23 -0
  119. data/lib/action_dispatch/middleware/templates/rescues/_source.html.erb +27 -0
  120. data/lib/action_dispatch/middleware/templates/rescues/_source.text.erb +8 -0
  121. data/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb +52 -0
  122. data/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb +9 -0
  123. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb +16 -0
  124. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb +9 -0
  125. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.html.erb +21 -0
  126. data/lib/action_dispatch/middleware/templates/rescues/invalid_statement.text.erb +13 -0
  127. data/lib/action_dispatch/middleware/templates/rescues/layout.erb +134 -5
  128. data/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb +11 -0
  129. data/lib/action_dispatch/middleware/templates/rescues/missing_template.text.erb +3 -0
  130. data/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb +32 -0
  131. data/lib/action_dispatch/middleware/templates/rescues/routing_error.text.erb +11 -0
  132. data/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb +20 -0
  133. data/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb +7 -0
  134. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.html.erb +6 -0
  135. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.text.erb +3 -0
  136. data/lib/action_dispatch/middleware/templates/routes/_route.html.erb +16 -0
  137. data/lib/action_dispatch/middleware/templates/routes/_table.html.erb +200 -0
  138. data/lib/action_dispatch/railtie.rb +29 -8
  139. data/lib/action_dispatch/request/session.rb +234 -0
  140. data/lib/action_dispatch/request/utils.rb +78 -0
  141. data/lib/action_dispatch/routing/endpoint.rb +17 -0
  142. data/lib/action_dispatch/routing/inspector.rb +225 -0
  143. data/lib/action_dispatch/routing/mapper.rb +1329 -582
  144. data/lib/action_dispatch/routing/polymorphic_routes.rb +237 -94
  145. data/lib/action_dispatch/routing/redirection.rb +120 -50
  146. data/lib/action_dispatch/routing/route_set.rb +545 -322
  147. data/lib/action_dispatch/routing/routes_proxy.rb +37 -7
  148. data/lib/action_dispatch/routing/url_for.rb +103 -34
  149. data/lib/action_dispatch/routing.rb +66 -99
  150. data/lib/action_dispatch/system_test_case.rb +147 -0
  151. data/lib/action_dispatch/system_testing/browser.rb +49 -0
  152. data/lib/action_dispatch/system_testing/driver.rb +59 -0
  153. data/lib/action_dispatch/system_testing/server.rb +31 -0
  154. data/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb +96 -0
  155. data/lib/action_dispatch/system_testing/test_helpers/setup_and_teardown.rb +31 -0
  156. data/lib/action_dispatch/system_testing/test_helpers/undef_methods.rb +26 -0
  157. data/lib/action_dispatch/testing/assertion_response.rb +47 -0
  158. data/lib/action_dispatch/testing/assertions/response.rb +53 -42
  159. data/lib/action_dispatch/testing/assertions/routing.rb +79 -74
  160. data/lib/action_dispatch/testing/assertions.rb +15 -9
  161. data/lib/action_dispatch/testing/integration.rb +361 -207
  162. data/lib/action_dispatch/testing/request_encoder.rb +55 -0
  163. data/lib/action_dispatch/testing/test_process.rb +28 -19
  164. data/lib/action_dispatch/testing/test_request.rb +30 -33
  165. data/lib/action_dispatch/testing/test_response.rb +35 -11
  166. data/lib/action_dispatch.rb +42 -32
  167. data/lib/action_pack/gem_version.rb +17 -0
  168. data/lib/action_pack/version.rb +7 -7
  169. data/lib/action_pack.rb +4 -2
  170. metadata +116 -175
  171. data/lib/abstract_controller/layouts.rb +0 -423
  172. data/lib/abstract_controller/view_paths.rb +0 -96
  173. data/lib/action_controller/caching/actions.rb +0 -185
  174. data/lib/action_controller/caching/fragments.rb +0 -127
  175. data/lib/action_controller/caching/pages.rb +0 -187
  176. data/lib/action_controller/caching/sweeping.rb +0 -97
  177. data/lib/action_controller/deprecated/integration_test.rb +0 -2
  178. data/lib/action_controller/deprecated/performance_test.rb +0 -1
  179. data/lib/action_controller/deprecated.rb +0 -3
  180. data/lib/action_controller/metal/compatibility.rb +0 -65
  181. data/lib/action_controller/metal/hide_actions.rb +0 -41
  182. data/lib/action_controller/metal/rack_delegation.rb +0 -26
  183. data/lib/action_controller/metal/responder.rb +0 -286
  184. data/lib/action_controller/metal/session_management.rb +0 -14
  185. data/lib/action_controller/middleware.rb +0 -39
  186. data/lib/action_controller/railties/paths.rb +0 -25
  187. data/lib/action_controller/record_identifier.rb +0 -85
  188. data/lib/action_controller/vendor/html-scanner/html/document.rb +0 -68
  189. data/lib/action_controller/vendor/html-scanner/html/node.rb +0 -532
  190. data/lib/action_controller/vendor/html-scanner/html/sanitizer.rb +0 -177
  191. data/lib/action_controller/vendor/html-scanner/html/selector.rb +0 -830
  192. data/lib/action_controller/vendor/html-scanner/html/tokenizer.rb +0 -107
  193. data/lib/action_controller/vendor/html-scanner/html/version.rb +0 -11
  194. data/lib/action_controller/vendor/html-scanner.rb +0 -20
  195. data/lib/action_dispatch/middleware/best_standards_support.rb +0 -30
  196. data/lib/action_dispatch/middleware/body_proxy.rb +0 -30
  197. data/lib/action_dispatch/middleware/head.rb +0 -18
  198. data/lib/action_dispatch/middleware/params_parser.rb +0 -75
  199. data/lib/action_dispatch/middleware/rescue.rb +0 -26
  200. data/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb +0 -31
  201. data/lib/action_dispatch/middleware/templates/rescues/_trace.erb +0 -26
  202. data/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb +0 -10
  203. data/lib/action_dispatch/middleware/templates/rescues/missing_template.erb +0 -2
  204. data/lib/action_dispatch/middleware/templates/rescues/routing_error.erb +0 -15
  205. data/lib/action_dispatch/middleware/templates/rescues/template_error.erb +0 -17
  206. data/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb +0 -2
  207. data/lib/action_dispatch/testing/assertions/dom.rb +0 -37
  208. data/lib/action_dispatch/testing/assertions/selector.rb +0 -435
  209. data/lib/action_dispatch/testing/assertions/tag.rb +0 -138
  210. data/lib/action_dispatch/testing/performance_test.rb +0 -10
  211. data/lib/action_view/asset_paths.rb +0 -142
  212. data/lib/action_view/base.rb +0 -220
  213. data/lib/action_view/buffers.rb +0 -43
  214. data/lib/action_view/context.rb +0 -36
  215. data/lib/action_view/flows.rb +0 -79
  216. data/lib/action_view/helpers/active_model_helper.rb +0 -50
  217. data/lib/action_view/helpers/asset_paths.rb +0 -7
  218. data/lib/action_view/helpers/asset_tag_helper.rb +0 -457
  219. data/lib/action_view/helpers/asset_tag_helpers/asset_include_tag.rb +0 -146
  220. data/lib/action_view/helpers/asset_tag_helpers/asset_paths.rb +0 -93
  221. data/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb +0 -193
  222. data/lib/action_view/helpers/asset_tag_helpers/stylesheet_tag_helpers.rb +0 -148
  223. data/lib/action_view/helpers/atom_feed_helper.rb +0 -200
  224. data/lib/action_view/helpers/cache_helper.rb +0 -64
  225. data/lib/action_view/helpers/capture_helper.rb +0 -203
  226. data/lib/action_view/helpers/controller_helper.rb +0 -25
  227. data/lib/action_view/helpers/csrf_helper.rb +0 -32
  228. data/lib/action_view/helpers/date_helper.rb +0 -1062
  229. data/lib/action_view/helpers/debug_helper.rb +0 -40
  230. data/lib/action_view/helpers/form_helper.rb +0 -1486
  231. data/lib/action_view/helpers/form_options_helper.rb +0 -658
  232. data/lib/action_view/helpers/form_tag_helper.rb +0 -685
  233. data/lib/action_view/helpers/javascript_helper.rb +0 -110
  234. data/lib/action_view/helpers/number_helper.rb +0 -622
  235. data/lib/action_view/helpers/output_safety_helper.rb +0 -38
  236. data/lib/action_view/helpers/record_tag_helper.rb +0 -111
  237. data/lib/action_view/helpers/rendering_helper.rb +0 -92
  238. data/lib/action_view/helpers/sanitize_helper.rb +0 -259
  239. data/lib/action_view/helpers/tag_helper.rb +0 -167
  240. data/lib/action_view/helpers/text_helper.rb +0 -426
  241. data/lib/action_view/helpers/translation_helper.rb +0 -91
  242. data/lib/action_view/helpers/url_helper.rb +0 -693
  243. data/lib/action_view/helpers.rb +0 -60
  244. data/lib/action_view/locale/en.yml +0 -160
  245. data/lib/action_view/log_subscriber.rb +0 -28
  246. data/lib/action_view/lookup_context.rb +0 -258
  247. data/lib/action_view/path_set.rb +0 -101
  248. data/lib/action_view/railtie.rb +0 -55
  249. data/lib/action_view/renderer/abstract_renderer.rb +0 -41
  250. data/lib/action_view/renderer/partial_renderer.rb +0 -415
  251. data/lib/action_view/renderer/renderer.rb +0 -61
  252. data/lib/action_view/renderer/streaming_template_renderer.rb +0 -106
  253. data/lib/action_view/renderer/template_renderer.rb +0 -95
  254. data/lib/action_view/template/error.rb +0 -128
  255. data/lib/action_view/template/handlers/builder.rb +0 -26
  256. data/lib/action_view/template/handlers/erb.rb +0 -125
  257. data/lib/action_view/template/handlers.rb +0 -50
  258. data/lib/action_view/template/resolver.rb +0 -298
  259. data/lib/action_view/template/text.rb +0 -30
  260. data/lib/action_view/template.rb +0 -337
  261. data/lib/action_view/test_case.rb +0 -246
  262. data/lib/action_view/testing/resolvers.rb +0 -49
  263. data/lib/action_view.rb +0 -84
  264. data/lib/sprockets/assets.rake +0 -99
  265. data/lib/sprockets/bootstrap.rb +0 -37
  266. data/lib/sprockets/compressors.rb +0 -83
  267. data/lib/sprockets/helpers/isolated_helper.rb +0 -13
  268. data/lib/sprockets/helpers/rails_helper.rb +0 -182
  269. data/lib/sprockets/helpers.rb +0 -6
  270. data/lib/sprockets/railtie.rb +0 -62
  271. data/lib/sprockets/static_compiler.rb +0 -56
@@ -1,337 +0,0 @@
1
- require 'active_support/core_ext/array/wrap'
2
- require 'active_support/core_ext/object/blank'
3
- require 'active_support/core_ext/object/try'
4
- require 'active_support/core_ext/kernel/singleton_class'
5
- require 'thread'
6
-
7
- module ActionView
8
- # = Action View Template
9
- class Template
10
- extend ActiveSupport::Autoload
11
-
12
- # === Encodings in ActionView::Template
13
- #
14
- # ActionView::Template is one of a few sources of potential
15
- # encoding issues in Rails. This is because the source for
16
- # templates are usually read from disk, and Ruby (like most
17
- # encoding-aware programming languages) assumes that the
18
- # String retrieved through File IO is encoded in the
19
- # <tt>default_external</tt> encoding. In Rails, the default
20
- # <tt>default_external</tt> encoding is UTF-8.
21
- #
22
- # As a result, if a user saves their template as ISO-8859-1
23
- # (for instance, using a non-Unicode-aware text editor),
24
- # and uses characters outside of the ASCII range, their
25
- # users will see diamonds with question marks in them in
26
- # the browser.
27
- #
28
- # For the rest of this documentation, when we say "UTF-8",
29
- # we mean "UTF-8 or whatever the default_internal encoding
30
- # is set to". By default, it will be UTF-8.
31
- #
32
- # To mitigate this problem, we use a few strategies:
33
- # 1. If the source is not valid UTF-8, we raise an exception
34
- # when the template is compiled to alert the user
35
- # to the problem.
36
- # 2. The user can specify the encoding using Ruby-style
37
- # encoding comments in any template engine. If such
38
- # a comment is supplied, Rails will apply that encoding
39
- # to the resulting compiled source returned by the
40
- # template handler.
41
- # 3. In all cases, we transcode the resulting String to
42
- # the UTF-8.
43
- #
44
- # This means that other parts of Rails can always assume
45
- # that templates are encoded in UTF-8, even if the original
46
- # source of the template was not UTF-8.
47
- #
48
- # From a user's perspective, the easiest thing to do is
49
- # to save your templates as UTF-8. If you do this, you
50
- # do not need to do anything else for things to "just work".
51
- #
52
- # === Instructions for template handlers
53
- #
54
- # The easiest thing for you to do is to simply ignore
55
- # encodings. Rails will hand you the template source
56
- # as the default_internal (generally UTF-8), raising
57
- # an exception for the user before sending the template
58
- # to you if it could not determine the original encoding.
59
- #
60
- # For the greatest simplicity, you can support only
61
- # UTF-8 as the <tt>default_internal</tt>. This means
62
- # that from the perspective of your handler, the
63
- # entire pipeline is just UTF-8.
64
- #
65
- # === Advanced: Handlers with alternate metadata sources
66
- #
67
- # If you want to provide an alternate mechanism for
68
- # specifying encodings (like ERB does via <%# encoding: ... %>),
69
- # you may indicate that you will handle encodings yourself
70
- # by implementing <tt>self.handles_encoding?</tt>
71
- # on your handler.
72
- #
73
- # If you do, Rails will not try to encode the String
74
- # into the default_internal, passing you the unaltered
75
- # bytes tagged with the assumed encoding (from
76
- # default_external).
77
- #
78
- # In this case, make sure you return a String from
79
- # your handler encoded in the default_internal. Since
80
- # you are handling out-of-band metadata, you are
81
- # also responsible for alerting the user to any
82
- # problems with converting the user's data to
83
- # the <tt>default_internal</tt>.
84
- #
85
- # To do so, simply raise the raise +WrongEncodingError+
86
- # as follows:
87
- #
88
- # raise WrongEncodingError.new(
89
- # problematic_string,
90
- # expected_encoding
91
- # )
92
-
93
- eager_autoload do
94
- autoload :Error
95
- autoload :Handlers
96
- autoload :Text
97
- end
98
-
99
- extend Template::Handlers
100
-
101
- attr_accessor :locals, :formats, :virtual_path
102
-
103
- attr_reader :source, :identifier, :handler, :original_encoding, :updated_at
104
-
105
- # This finalizer is needed (and exactly with a proc inside another proc)
106
- # otherwise templates leak in development.
107
- Finalizer = proc do |method_name, mod|
108
- proc do
109
- mod.module_eval do
110
- remove_possible_method method_name
111
- end
112
- end
113
- end
114
-
115
- def initialize(source, identifier, handler, details)
116
- format = details[:format] || (handler.default_format if handler.respond_to?(:default_format))
117
-
118
- @source = source
119
- @identifier = identifier
120
- @handler = handler
121
- @compiled = false
122
- @original_encoding = nil
123
- @locals = details[:locals] || []
124
- @virtual_path = details[:virtual_path]
125
- @updated_at = details[:updated_at] || Time.now
126
- @formats = Array.wrap(format).map { |f| f.is_a?(Mime::Type) ? f.ref : f }
127
- @compile_mutex = Mutex.new
128
- end
129
-
130
- # Returns if the underlying handler supports streaming. If so,
131
- # a streaming buffer *may* be passed when it start rendering.
132
- def supports_streaming?
133
- handler.respond_to?(:supports_streaming?) && handler.supports_streaming?
134
- end
135
-
136
- # Render a template. If the template was not compiled yet, it is done
137
- # exactly before rendering.
138
- #
139
- # This method is instrumented as "!render_template.action_view". Notice that
140
- # we use a bang in this instrumentation because you don't want to
141
- # consume this in production. This is only slow if it's being listened to.
142
- def render(view, locals, buffer=nil, &block)
143
- ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do
144
- compile!(view)
145
- view.send(method_name, locals, buffer, &block)
146
- end
147
- rescue Exception => e
148
- handle_render_error(view, e)
149
- end
150
-
151
- def mime_type
152
- @mime_type ||= Mime::Type.lookup_by_extension(@formats.first.to_s) if @formats.first
153
- end
154
-
155
- # Receives a view object and return a template similar to self by using @virtual_path.
156
- #
157
- # This method is useful if you have a template object but it does not contain its source
158
- # anymore since it was already compiled. In such cases, all you need to do is to call
159
- # refresh passing in the view object.
160
- #
161
- # Notice this method raises an error if the template to be refreshed does not have a
162
- # virtual path set (true just for inline templates).
163
- def refresh(view)
164
- raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path
165
- lookup = view.lookup_context
166
- pieces = @virtual_path.split("/")
167
- name = pieces.pop
168
- partial = !!name.sub!(/^_/, "")
169
- lookup.disable_cache do
170
- lookup.find_template(name, [ pieces.join('/') ], partial, @locals)
171
- end
172
- end
173
-
174
- def inspect
175
- @inspect ||= defined?(Rails.root) ? identifier.sub("#{Rails.root}/", '') : identifier
176
- end
177
-
178
- # This method is responsible for properly setting the encoding of the
179
- # source. Until this point, we assume that the source is BINARY data.
180
- # If no additional information is supplied, we assume the encoding is
181
- # the same as <tt>Encoding.default_external</tt>.
182
- #
183
- # The user can also specify the encoding via a comment on the first
184
- # line of the template (# encoding: NAME-OF-ENCODING). This will work
185
- # with any template engine, as we process out the encoding comment
186
- # before passing the source on to the template engine, leaving a
187
- # blank line in its stead.
188
- def encode!
189
- return unless source.encoding_aware? && source.encoding == Encoding::BINARY
190
-
191
- # Look for # encoding: *. If we find one, we'll encode the
192
- # String in that encoding, otherwise, we'll use the
193
- # default external encoding.
194
- if source.sub!(/\A#{ENCODING_FLAG}/, '')
195
- encoding = magic_encoding = $1
196
- else
197
- encoding = Encoding.default_external
198
- end
199
-
200
- # Tag the source with the default external encoding
201
- # or the encoding specified in the file
202
- source.force_encoding(encoding)
203
-
204
- # If the user didn't specify an encoding, and the handler
205
- # handles encodings, we simply pass the String as is to
206
- # the handler (with the default_external tag)
207
- if !magic_encoding && @handler.respond_to?(:handles_encoding?) && @handler.handles_encoding?
208
- source
209
- # Otherwise, if the String is valid in the encoding,
210
- # encode immediately to default_internal. This means
211
- # that if a handler doesn't handle encodings, it will
212
- # always get Strings in the default_internal
213
- elsif source.valid_encoding?
214
- source.encode!
215
- # Otherwise, since the String is invalid in the encoding
216
- # specified, raise an exception
217
- else
218
- raise WrongEncodingError.new(source, encoding)
219
- end
220
- end
221
-
222
- protected
223
-
224
- # Compile a template. This method ensures a template is compiled
225
- # just once and removes the source after it is compiled.
226
- def compile!(view) #:nodoc:
227
- return if @compiled
228
-
229
- # Templates can be used concurrently in threaded environments
230
- # so compilation and any instance variable modification must
231
- # be synchronized
232
- @compile_mutex.synchronize do
233
- # Any thread holding this lock will be compiling the template needed
234
- # by the threads waiting. So re-check the @compiled flag to avoid
235
- # re-compilation
236
- return if @compiled
237
-
238
- if view.is_a?(ActionView::CompiledTemplates)
239
- mod = ActionView::CompiledTemplates
240
- else
241
- mod = view.singleton_class
242
- end
243
-
244
- compile(view, mod)
245
-
246
- # Just discard the source if we have a virtual path. This
247
- # means we can get the template back.
248
- @source = nil if @virtual_path
249
- @compiled = true
250
- end
251
- end
252
-
253
- # Among other things, this method is responsible for properly setting
254
- # the encoding of the compiled template.
255
- #
256
- # If the template engine handles encodings, we send the encoded
257
- # String to the engine without further processing. This allows
258
- # the template engine to support additional mechanisms for
259
- # specifying the encoding. For instance, ERB supports <%# encoding: %>
260
- #
261
- # Otherwise, after we figure out the correct encoding, we then
262
- # encode the source into <tt>Encoding.default_internal</tt>.
263
- # In general, this means that templates will be UTF-8 inside of Rails,
264
- # regardless of the original source encoding.
265
- def compile(view, mod) #:nodoc:
266
- encode!
267
- method_name = self.method_name
268
- code = @handler.call(self)
269
-
270
- # Make sure that the resulting String to be evalled is in the
271
- # encoding of the code
272
- source = <<-end_src
273
- def #{method_name}(local_assigns, output_buffer)
274
- _old_virtual_path, @virtual_path = @virtual_path, #{@virtual_path.inspect};_old_output_buffer = @output_buffer;#{locals_code};#{code}
275
- ensure
276
- @virtual_path, @output_buffer = _old_virtual_path, _old_output_buffer
277
- end
278
- end_src
279
-
280
- if source.encoding_aware?
281
- # Make sure the source is in the encoding of the returned code
282
- source.force_encoding(code.encoding)
283
-
284
- # In case we get back a String from a handler that is not in
285
- # BINARY or the default_internal, encode it to the default_internal
286
- source.encode!
287
-
288
- # Now, validate that the source we got back from the template
289
- # handler is valid in the default_internal. This is for handlers
290
- # that handle encoding but screw up
291
- unless source.valid_encoding?
292
- raise WrongEncodingError.new(@source, Encoding.default_internal)
293
- end
294
- end
295
-
296
- begin
297
- mod.module_eval(source, identifier, 0)
298
- ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
299
- rescue Exception => e # errors from template code
300
- if logger = (view && view.logger)
301
- logger.debug "ERROR: compiling #{method_name} RAISED #{e}"
302
- logger.debug "Function body: #{source}"
303
- logger.debug "Backtrace: #{e.backtrace.join("\n")}"
304
- end
305
-
306
- raise ActionView::Template::Error.new(self, {}, e)
307
- end
308
- end
309
-
310
- def handle_render_error(view, e) #:nodoc:
311
- if e.is_a?(Template::Error)
312
- e.sub_template_of(self)
313
- raise e
314
- else
315
- assigns = view.respond_to?(:assigns) ? view.assigns : {}
316
- template = self
317
- unless template.source
318
- template = refresh(view)
319
- template.encode!
320
- end
321
- raise Template::Error.new(template, assigns, e)
322
- end
323
- end
324
-
325
- def locals_code #:nodoc:
326
- @locals.map { |key| "#{key} = local_assigns[:#{key}];" }.join
327
- end
328
-
329
- def method_name #:nodoc:
330
- @method_name ||= "_#{identifier_method_name}__#{@identifier.hash}_#{__id__}".gsub('-', "_")
331
- end
332
-
333
- def identifier_method_name #:nodoc:
334
- inspect.gsub(/[^a-z_]/, '_')
335
- end
336
- end
337
- end
@@ -1,246 +0,0 @@
1
- require 'active_support/core_ext/object/blank'
2
- require 'active_support/core_ext/module/delegation'
3
- require 'active_support/core_ext/module/remove_method'
4
- require 'action_controller'
5
- require 'action_controller/test_case'
6
- require 'action_view'
7
-
8
- module ActionView
9
- # = Action View Test Case
10
- class TestCase < ActiveSupport::TestCase
11
- class TestController < ActionController::Base
12
- include ActionDispatch::TestProcess
13
-
14
- attr_accessor :request, :response, :params
15
-
16
- class << self
17
- attr_writer :controller_path
18
- end
19
-
20
- def controller_path=(path)
21
- self.class.controller_path=(path)
22
- end
23
-
24
- def initialize
25
- super
26
- self.class.controller_path = ""
27
- @request = ActionController::TestRequest.new
28
- @response = ActionController::TestResponse.new
29
-
30
- @request.env.delete('PATH_INFO')
31
- @params = {}
32
- end
33
- end
34
-
35
- module Behavior
36
- extend ActiveSupport::Concern
37
-
38
- include ActionDispatch::Assertions, ActionDispatch::TestProcess
39
- include ActionController::TemplateAssertions
40
- include ActionView::Context
41
-
42
- include ActionDispatch::Routing::PolymorphicRoutes
43
- include ActionController::RecordIdentifier
44
-
45
- include AbstractController::Helpers
46
- include ActionView::Helpers
47
-
48
- delegate :lookup_context, :to => :controller
49
- attr_accessor :controller, :output_buffer, :rendered
50
-
51
- module ClassMethods
52
- def tests(helper_class)
53
- case helper_class
54
- when String, Symbol
55
- self.helper_class = "#{helper_class.to_s.underscore}_helper".camelize.safe_constantize
56
- when Module
57
- self.helper_class = helper_class
58
- end
59
- end
60
-
61
- def determine_default_helper_class(name)
62
- mod = name.sub(/Test$/, '').constantize
63
- mod.is_a?(Class) ? nil : mod
64
- rescue NameError
65
- nil
66
- end
67
-
68
- def helper_method(*methods)
69
- # Almost a duplicate from ActionController::Helpers
70
- methods.flatten.each do |method|
71
- _helpers.module_eval <<-end_eval
72
- def #{method}(*args, &block) # def current_user(*args, &block)
73
- _test_case.send(%(#{method}), *args, &block) # _test_case.send(%(current_user), *args, &block)
74
- end # end
75
- end_eval
76
- end
77
- end
78
-
79
- attr_writer :helper_class
80
-
81
- def helper_class
82
- @helper_class ||= determine_default_helper_class(name)
83
- end
84
-
85
- def new(*)
86
- include_helper_modules!
87
- super
88
- end
89
-
90
- private
91
-
92
- def include_helper_modules!
93
- helper(helper_class) if helper_class
94
- include _helpers
95
- end
96
-
97
- end
98
-
99
- def setup_with_controller
100
- @controller = ActionView::TestCase::TestController.new
101
- @request = @controller.request
102
- @output_buffer = ActiveSupport::SafeBuffer.new
103
- @rendered = ''
104
-
105
- make_test_case_available_to_view!
106
- say_no_to_protect_against_forgery!
107
- end
108
-
109
- def config
110
- @controller.config if @controller.respond_to?(:config)
111
- end
112
-
113
- def render(options = {}, local_assigns = {}, &block)
114
- view.assign(view_assigns)
115
- @rendered << output = view.render(options, local_assigns, &block)
116
- output
117
- end
118
-
119
- def locals
120
- @locals ||= {}
121
- end
122
-
123
- included do
124
- setup :setup_with_controller
125
- end
126
-
127
- private
128
-
129
- # Support the selector assertions
130
- #
131
- # Need to experiment if this priority is the best one: rendered => output_buffer
132
- def response_from_page
133
- HTML::Document.new(@rendered.blank? ? @output_buffer : @rendered).root
134
- end
135
-
136
- def say_no_to_protect_against_forgery!
137
- _helpers.module_eval do
138
- remove_possible_method :protect_against_forgery?
139
- def protect_against_forgery?
140
- false
141
- end
142
- end
143
- end
144
-
145
- def make_test_case_available_to_view!
146
- test_case_instance = self
147
- _helpers.module_eval do
148
- unless private_method_defined?(:_test_case)
149
- define_method(:_test_case) { test_case_instance }
150
- private :_test_case
151
- end
152
- end
153
- end
154
-
155
- module Locals
156
- attr_accessor :locals
157
-
158
- def render(options = {}, local_assigns = {})
159
- case options
160
- when Hash
161
- if block_given?
162
- locals[options[:layout]] = options[:locals]
163
- elsif options.key?(:partial)
164
- locals[options[:partial]] = options[:locals]
165
- end
166
- else
167
- locals[options] = local_assigns
168
- end
169
-
170
- super
171
- end
172
- end
173
-
174
- # The instance of ActionView::Base that is used by +render+.
175
- def view
176
- @view ||= begin
177
- view = @controller.view_context
178
- view.singleton_class.send :include, _helpers
179
- view.extend(Locals)
180
- view.locals = self.locals
181
- view.output_buffer = self.output_buffer
182
- view
183
- end
184
- end
185
-
186
- alias_method :_view, :view
187
-
188
- INTERNAL_IVARS = %w{
189
- @__name__
190
- @__io__
191
- @_assertion_wrapped
192
- @_assertions
193
- @_result
194
- @_routes
195
- @controller
196
- @internal_data
197
- @layouts
198
- @locals
199
- @method_name
200
- @output_buffer
201
- @partials
202
- @passed
203
- @rendered
204
- @request
205
- @routes
206
- @templates
207
- @options
208
- @test_passed
209
- @view
210
- @view_context_class
211
- }
212
-
213
- def _user_defined_ivars
214
- instance_variables.map(&:to_s) - INTERNAL_IVARS
215
- end
216
-
217
- # Returns a Hash of instance variables and their values, as defined by
218
- # the user in the test case, which are then assigned to the view being
219
- # rendered. This is generally intended for internal use and extension
220
- # frameworks.
221
- def view_assigns
222
- Hash[_user_defined_ivars.map do |var|
223
- [var[1, var.length].to_sym, instance_variable_get(var)]
224
- end]
225
- end
226
-
227
- def _routes
228
- @controller._routes if @controller.respond_to?(:_routes)
229
- end
230
-
231
- def method_missing(selector, *args)
232
- if @controller.respond_to?(:_routes) &&
233
- ( @controller._routes.named_routes.helpers.include?(selector) ||
234
- @controller._routes.mounted_helpers.method_defined?(selector) )
235
- @controller.__send__(selector, *args)
236
- else
237
- super
238
- end
239
- end
240
-
241
- end
242
-
243
- include Behavior
244
-
245
- end
246
- end
@@ -1,49 +0,0 @@
1
- require 'action_view/template/resolver'
2
-
3
- module ActionView #:nodoc:
4
- # Use FixtureResolver in your tests to simulate the presence of files on the
5
- # file system. This is used internally by Rails' own test suite, and is
6
- # useful for testing extensions that have no way of knowing what the file
7
- # system will look like at runtime.
8
- class FixtureResolver < PathResolver
9
- attr_reader :hash
10
-
11
- def initialize(hash = {}, pattern=nil)
12
- super(pattern)
13
- @hash = hash
14
- end
15
-
16
- def to_s
17
- @hash.keys.join(', ')
18
- end
19
-
20
- private
21
-
22
- def query(path, exts, formats, outside_app_allowed)
23
- query = ""
24
- EXTENSIONS.each do |ext|
25
- query << '(' << exts[ext].map {|e| e && Regexp.escape(".#{e}") }.join('|') << '|)'
26
- end
27
- query = /^(#{Regexp.escape(path)})#{query}$/
28
-
29
- templates = []
30
- @hash.each do |_path, array|
31
- source, updated_at = array
32
- next unless _path =~ query
33
- handler, format = extract_handler_and_format(_path, formats)
34
- templates << Template.new(source, _path, handler,
35
- :virtual_path => path.virtual, :format => format, :updated_at => updated_at)
36
- end
37
-
38
- templates.sort_by {|t| -t.identifier.match(/^#{query}$/).captures.reject(&:blank?).size }
39
- end
40
- end
41
-
42
- class NullResolver < PathResolver
43
- def query(path, exts, formats, outside_app_allowed)
44
- handler, format = extract_handler_and_format(path, formats)
45
- [ActionView::Template.new("Template generated by Null Resolver", path, handler, :virtual_path => path, :format => format)]
46
- end
47
- end
48
-
49
- end