actionpack 4.0.1 → 4.2.11.1

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