actionview 6.0.0.beta1 → 6.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionview might be problematic. Click here for more details.

Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -3
  3. data/lib/action_view.rb +1 -1
  4. data/lib/action_view/base.rb +107 -10
  5. data/lib/action_view/context.rb +0 -5
  6. data/lib/action_view/digestor.rb +4 -8
  7. data/lib/action_view/file_template.rb +33 -0
  8. data/lib/action_view/gem_version.rb +1 -1
  9. data/lib/action_view/helpers/asset_tag_helper.rb +5 -5
  10. data/lib/action_view/helpers/cache_helper.rb +5 -5
  11. data/lib/action_view/helpers/csp_helper.rb +4 -2
  12. data/lib/action_view/helpers/rendering_helper.rb +6 -4
  13. data/lib/action_view/helpers/tags/base.rb +1 -1
  14. data/lib/action_view/helpers/translation_helper.rb +1 -1
  15. data/lib/action_view/layouts.rb +5 -5
  16. data/lib/action_view/lookup_context.rb +59 -24
  17. data/lib/action_view/railtie.rb +8 -3
  18. data/lib/action_view/renderer/abstract_renderer.rb +56 -3
  19. data/lib/action_view/renderer/partial_renderer.rb +66 -52
  20. data/lib/action_view/renderer/partial_renderer/collection_caching.rb +14 -14
  21. data/lib/action_view/renderer/renderer.rb +16 -4
  22. data/lib/action_view/renderer/streaming_template_renderer.rb +3 -3
  23. data/lib/action_view/renderer/template_renderer.rb +18 -18
  24. data/lib/action_view/rendering.rb +44 -26
  25. data/lib/action_view/template.rb +58 -36
  26. data/lib/action_view/template/handlers.rb +27 -1
  27. data/lib/action_view/template/handlers/builder.rb +2 -2
  28. data/lib/action_view/template/handlers/erb.rb +5 -5
  29. data/lib/action_view/template/handlers/erb/erubi.rb +7 -3
  30. data/lib/action_view/template/handlers/html.rb +1 -1
  31. data/lib/action_view/template/handlers/raw.rb +2 -2
  32. data/lib/action_view/template/html.rb +14 -5
  33. data/lib/action_view/template/inline.rb +22 -0
  34. data/lib/action_view/template/resolver.rb +14 -7
  35. data/lib/action_view/template/text.rb +5 -3
  36. data/lib/action_view/testing/resolvers.rb +6 -4
  37. data/lib/action_view/view_paths.rb +25 -1
  38. metadata +12 -10
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/deprecation"
4
+
3
5
  module ActionView #:nodoc:
4
6
  # = Action View Template Handlers
5
7
  class Template #:nodoc:
@@ -14,7 +16,7 @@ module ActionView #:nodoc:
14
16
  base.register_template_handler :erb, ERB.new
15
17
  base.register_template_handler :html, Html.new
16
18
  base.register_template_handler :builder, Builder.new
17
- base.register_template_handler :ruby, :source.to_proc
19
+ base.register_template_handler :ruby, lambda { |_, source| source }
18
20
  end
19
21
 
20
22
  @@template_handlers = {}
@@ -24,11 +26,35 @@ module ActionView #:nodoc:
24
26
  @@template_extensions ||= @@template_handlers.keys
25
27
  end
26
28
 
29
+ class LegacyHandlerWrapper < SimpleDelegator # :nodoc:
30
+ def call(view, source)
31
+ __getobj__.call(ActionView::Template::LegacyTemplate.new(view, source))
32
+ end
33
+ end
34
+
27
35
  # Register an object that knows how to handle template files with the given
28
36
  # extensions. This can be used to implement new template types.
29
37
  # The handler must respond to +:call+, which will be passed the template
30
38
  # and should return the rendered template as a String.
31
39
  def register_template_handler(*extensions, handler)
40
+ params = if handler.is_a?(Proc)
41
+ handler.parameters
42
+ else
43
+ handler.method(:call).parameters
44
+ end
45
+
46
+ unless params.find_all { |type, _| type == :req || type == :opt }.length >= 2
47
+ ActiveSupport::Deprecation.warn <<~eowarn
48
+ Single arity template handlers are deprecated. Template handlers must
49
+ now accept two parameters, the view object and the source for the view object.
50
+ Change:
51
+ >> #{handler.class}#call(#{params.map(&:last).join(", ")})
52
+ To:
53
+ >> #{handler.class}#call(#{params.map(&:last).join(", ")}, source)
54
+ eowarn
55
+ handler = LegacyHandlerWrapper.new(handler)
56
+ end
57
+
32
58
  raise(ArgumentError, "Extension is required") if extensions.empty?
33
59
  extensions.each do |extension|
34
60
  @@template_handlers[extension.to_sym] = handler
@@ -5,11 +5,11 @@ module ActionView
5
5
  class Builder
6
6
  class_attribute :default_format, default: :xml
7
7
 
8
- def call(template)
8
+ def call(template, source)
9
9
  require_engine
10
10
  "xml = ::Builder::XmlMarkup.new(:indent => 2);" \
11
11
  "self.output_buffer = xml.target!;" +
12
- template.source +
12
+ source +
13
13
  ";xml.target!;"
14
14
  end
15
15
 
@@ -28,8 +28,8 @@ module ActionView
28
28
 
29
29
  ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")
30
30
 
31
- def self.call(template)
32
- new.call(template)
31
+ def self.call(template, source)
32
+ new.call(template, source)
33
33
  end
34
34
 
35
35
  def supports_streaming?
@@ -40,17 +40,17 @@ module ActionView
40
40
  true
41
41
  end
42
42
 
43
- def call(template)
43
+ def call(template, source)
44
44
  # First, convert to BINARY, so in case the encoding is
45
45
  # wrong, we can still find an encoding tag
46
46
  # (<%# encoding %>) inside the String using a regular
47
47
  # expression
48
- template_source = template.source.dup.force_encoding(Encoding::ASCII_8BIT)
48
+ template_source = source.dup.force_encoding(Encoding::ASCII_8BIT)
49
49
 
50
50
  erb = template_source.gsub(ENCODING_TAG, "")
51
51
  encoding = $2
52
52
 
53
- erb.force_encoding valid_encoding(template.source.dup, encoding)
53
+ erb.force_encoding valid_encoding(source.dup, encoding)
54
54
 
55
55
  # Always make sure we return a String in the default_internal
56
56
  erb.encode!
@@ -13,7 +13,7 @@ module ActionView
13
13
 
14
14
  # Dup properties so that we don't modify argument
15
15
  properties = Hash[properties]
16
- properties[:preamble] = "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
16
+ properties[:preamble] = ""
17
17
  properties[:postamble] = "@output_buffer.to_s"
18
18
  properties[:bufvar] = "@output_buffer"
19
19
  properties[:escapefunc] = ""
@@ -22,8 +22,12 @@ module ActionView
22
22
  end
23
23
 
24
24
  def evaluate(action_view_erb_handler_context)
25
- pr = eval("proc { #{@src} }", binding, @filename || "(erubi)")
26
- action_view_erb_handler_context.instance_eval(&pr)
25
+ src = @src
26
+ view = Class.new(ActionView::Base) {
27
+ include action_view_erb_handler_context._routes.url_helpers
28
+ class_eval("define_method(:_template) { |local_assigns, output_buffer| #{src} }", @filename || "(erubi)", 0)
29
+ }.empty
30
+ view.run(:_template, nil, {}, ActionView::OutputBuffer.new)
27
31
  end
28
32
 
29
33
  private
@@ -3,7 +3,7 @@
3
3
  module ActionView
4
4
  module Template::Handlers
5
5
  class Html < Raw
6
- def call(template)
6
+ def call(template, source)
7
7
  "ActionView::OutputBuffer.new #{super}"
8
8
  end
9
9
  end
@@ -3,8 +3,8 @@
3
3
  module ActionView
4
4
  module Template::Handlers
5
5
  class Raw
6
- def call(template)
7
- "#{template.source.inspect}.html_safe;"
6
+ def call(template, source)
7
+ "#{source.inspect}.html_safe;"
8
8
  end
9
9
  end
10
10
  end
@@ -1,15 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/deprecation"
4
+
3
5
  module ActionView #:nodoc:
4
6
  # = Action View HTML Template
5
7
  class Template #:nodoc:
6
8
  class HTML #:nodoc:
7
- attr_accessor :type
9
+ attr_reader :type
8
10
 
9
11
  def initialize(string, type = nil)
12
+ unless type
13
+ ActiveSupport::Deprecation.warn "ActionView::Template::HTML#initialize requires a type parameter"
14
+ type = :html
15
+ end
16
+
10
17
  @string = string.to_s
11
- @type = Types[type] || type if type
12
- @type ||= Types[:html]
18
+ @type = type
13
19
  end
14
20
 
15
21
  def identifier
@@ -26,9 +32,12 @@ module ActionView #:nodoc:
26
32
  to_str
27
33
  end
28
34
 
29
- def formats
30
- [@type.respond_to?(:ref) ? @type.ref : @type.to_s]
35
+ def format
36
+ @type
31
37
  end
38
+
39
+ def formats; Array(format); end
40
+ deprecate :formats
32
41
  end
33
42
  end
34
43
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionView #:nodoc:
4
+ class Template #:nodoc:
5
+ class Inline < Template #:nodoc:
6
+ # This finalizer is needed (and exactly with a proc inside another proc)
7
+ # otherwise templates leak in development.
8
+ Finalizer = proc do |method_name, mod| # :nodoc:
9
+ proc do
10
+ mod.module_eval do
11
+ remove_possible_method method_name
12
+ end
13
+ end
14
+ end
15
+
16
+ def compile(mod)
17
+ super
18
+ ObjectSpace.define_finalizer(self, Finalizer[method_name, mod])
19
+ end
20
+ end
21
+ end
22
+ end
@@ -196,7 +196,6 @@ module ActionView
196
196
  cached = nil
197
197
  templates.each do |t|
198
198
  t.locals = locals
199
- t.formats = details[:formats] || [:html] if t.formats.empty?
200
199
  t.variants = details[:variants] || [] if t.variants.empty?
201
200
  t.virtual_path ||= (cached ||= build_path(*path_info))
202
201
  end
@@ -225,10 +224,9 @@ module ActionView
225
224
  template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed
226
225
 
227
226
  template_paths.map do |template|
228
- handler, format, variant = extract_handler_and_format_and_variant(template)
229
- contents = File.binread(template)
227
+ handler, format, variant = extract_handler_and_format_and_variant(template, formats.first)
230
228
 
231
- Template.new(contents, File.expand_path(template), handler,
229
+ FileTemplate.new(File.expand_path(template), handler,
232
230
  virtual_path: path.virtual,
233
231
  format: format,
234
232
  variant: variant,
@@ -293,7 +291,7 @@ module ActionView
293
291
  # Extract handler, formats and variant from path. If a format cannot be found neither
294
292
  # from the path, or the handler, we should return the array of formats given
295
293
  # to the resolver.
296
- def extract_handler_and_format_and_variant(path)
294
+ def extract_handler_and_format_and_variant(path, query_format)
297
295
  pieces = File.basename(path).split(".")
298
296
  pieces.shift
299
297
 
@@ -301,9 +299,18 @@ module ActionView
301
299
 
302
300
  handler = Template.handler_for_extension(extension)
303
301
  format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last
304
- format &&= Template::Types[format]
302
+ format = if format
303
+ Template::Types[format]&.ref
304
+ else
305
+ if handler.respond_to?(:default_format) # default_format can return nil
306
+ handler.default_format
307
+ else
308
+ query_format
309
+ end
310
+ end
305
311
 
306
- [handler, format, variant]
312
+ # Template::Types[format] and handler.default_format can return nil
313
+ [handler, format || query_format, variant]
307
314
  end
308
315
  end
309
316
 
@@ -8,7 +8,6 @@ module ActionView #:nodoc:
8
8
 
9
9
  def initialize(string)
10
10
  @string = string.to_s
11
- @type = Types[:text]
12
11
  end
13
12
 
14
13
  def identifier
@@ -25,9 +24,12 @@ module ActionView #:nodoc:
25
24
  to_str
26
25
  end
27
26
 
28
- def formats
29
- [@type.ref]
27
+ def format
28
+ :text
30
29
  end
30
+
31
+ def formats; Array(format); end
32
+ deprecate :formats
31
33
  end
32
34
  end
33
35
  end
@@ -8,13 +8,15 @@ module ActionView #:nodoc:
8
8
  # useful for testing extensions that have no way of knowing what the file
9
9
  # system will look like at runtime.
10
10
  class FixtureResolver < PathResolver
11
- attr_reader :hash
12
-
13
11
  def initialize(hash = {}, pattern = nil)
14
12
  super(pattern)
15
13
  @hash = hash
16
14
  end
17
15
 
16
+ def data
17
+ @hash
18
+ end
19
+
18
20
  def to_s
19
21
  @hash.keys.join(", ")
20
22
  end
@@ -32,7 +34,7 @@ module ActionView #:nodoc:
32
34
  @hash.each do |_path, array|
33
35
  source, updated_at = array
34
36
  next unless query.match?(_path)
35
- handler, format, variant = extract_handler_and_format_and_variant(_path)
37
+ handler, format, variant = extract_handler_and_format_and_variant(_path, :html)
36
38
  templates << Template.new(source, _path, handler,
37
39
  virtual_path: path.virtual,
38
40
  format: format,
@@ -47,7 +49,7 @@ module ActionView #:nodoc:
47
49
 
48
50
  class NullResolver < PathResolver
49
51
  def query(path, exts, _, _)
50
- handler, format, variant = extract_handler_and_format_and_variant(path)
52
+ handler, format, variant = extract_handler_and_format_and_variant(path, :html)
51
53
  [ActionView::Template.new("Template generated by Null Resolver", path.virtual, handler, virtual_path: path.virtual, format: format, variant: variant)]
52
54
  end
53
55
  end
@@ -5,13 +5,21 @@ module ActionView
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- class_attribute :_view_paths, default: ActionView::PathSet.new.freeze
8
+ ViewPaths.set_view_paths(self, ActionView::PathSet.new.freeze)
9
9
  end
10
10
 
11
11
  delegate :template_exists?, :any_templates?, :view_paths, :formats, :formats=,
12
12
  :locale, :locale=, to: :lookup_context
13
13
 
14
14
  module ClassMethods
15
+ def _view_paths
16
+ ViewPaths.get_view_paths(self)
17
+ end
18
+
19
+ def _view_paths=(paths)
20
+ ViewPaths.set_view_paths(self, paths)
21
+ end
22
+
15
23
  def _prefixes # :nodoc:
16
24
  @_prefixes ||= begin
17
25
  return local_prefixes if superclass.abstract?
@@ -29,6 +37,22 @@ module ActionView
29
37
  end
30
38
  end
31
39
 
40
+ # :stopdoc:
41
+ @all_view_paths = {}
42
+
43
+ def self.get_view_paths(klass)
44
+ @all_view_paths[klass] || get_view_paths(klass.superclass)
45
+ end
46
+
47
+ def self.set_view_paths(klass, paths)
48
+ @all_view_paths[klass] = paths
49
+ end
50
+
51
+ def self.all_view_paths
52
+ @all_view_paths.values.uniq
53
+ end
54
+ # :startdoc:
55
+
32
56
  # The prefixes used in render "foo" shortcuts.
33
57
  def _prefixes # :nodoc:
34
58
  self.class._prefixes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actionview
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0.beta1
4
+ version: 6.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2019-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.0.beta1
19
+ version: 6.0.0.beta2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 6.0.0.beta1
26
+ version: 6.0.0.beta2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: builder
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -92,28 +92,28 @@ dependencies:
92
92
  requirements:
93
93
  - - '='
94
94
  - !ruby/object:Gem::Version
95
- version: 6.0.0.beta1
95
+ version: 6.0.0.beta2
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - '='
101
101
  - !ruby/object:Gem::Version
102
- version: 6.0.0.beta1
102
+ version: 6.0.0.beta2
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: activemodel
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - '='
108
108
  - !ruby/object:Gem::Version
109
- version: 6.0.0.beta1
109
+ version: 6.0.0.beta2
110
110
  type: :development
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - '='
115
115
  - !ruby/object:Gem::Version
116
- version: 6.0.0.beta1
116
+ version: 6.0.0.beta2
117
117
  description: Simple, battle-tested conventions and helpers for building web pages.
118
118
  email: david@loudthinking.com
119
119
  executables: []
@@ -129,6 +129,7 @@ files:
129
129
  - lib/action_view/context.rb
130
130
  - lib/action_view/dependency_tracker.rb
131
131
  - lib/action_view/digestor.rb
132
+ - lib/action_view/file_template.rb
132
133
  - lib/action_view/flows.rb
133
134
  - lib/action_view/gem_version.rb
134
135
  - lib/action_view/helpers.rb
@@ -217,6 +218,7 @@ files:
217
218
  - lib/action_view/template/handlers/html.rb
218
219
  - lib/action_view/template/handlers/raw.rb
219
220
  - lib/action_view/template/html.rb
221
+ - lib/action_view/template/inline.rb
220
222
  - lib/action_view/template/resolver.rb
221
223
  - lib/action_view/template/text.rb
222
224
  - lib/action_view/template/types.rb
@@ -229,8 +231,8 @@ homepage: http://rubyonrails.org
229
231
  licenses:
230
232
  - MIT
231
233
  metadata:
232
- source_code_uri: https://github.com/rails/rails/tree/v6.0.0.beta1/actionview
233
- changelog_uri: https://github.com/rails/rails/blob/v6.0.0.beta1/actionview/CHANGELOG.md
234
+ source_code_uri: https://github.com/rails/rails/tree/v6.0.0.beta2/actionview
235
+ changelog_uri: https://github.com/rails/rails/blob/v6.0.0.beta2/actionview/CHANGELOG.md
234
236
  post_install_message:
235
237
  rdoc_options: []
236
238
  require_paths: