From ff4b2cf2410286e75b2ad41c9733ef41f65fba15 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 25 Feb 2019 22:55:23 -0800 Subject: [PATCH 1/3] Allow format to be nil --- actionview/lib/action_view/template.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 7f29dedd7c..2c27e11b9e 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -126,11 +126,6 @@ module ActionView attr_reader :variable, :format, :variant, :locals, :virtual_path def initialize(source, identifier, handler, format: nil, variant: nil, locals: nil, virtual_path: nil, updated_at: Time.now) - unless format - ActiveSupport::Deprecation.warn "ActionView::Template#initialize requires a format parameter" - format = :html - end - unless locals ActiveSupport::Deprecation.warn "ActionView::Template#initialize requires a locals parameter" locals = [] From c2145462178d2b28135fac38ebbceeaadfa151df Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 25 Feb 2019 19:33:50 -0800 Subject: [PATCH 2/3] Create templates with format=nil --- actionmailer/lib/action_mailer/base.rb | 5 +++-- actionview/lib/action_view/rendering.rb | 3 ++- actionview/lib/action_view/template/resolver.rb | 4 ++-- actionview/test/template/lookup_context_test.rb | 4 ++-- actionview/test/template/resolver_patterns_test.rb | 2 +- actionview/test/template/testing/fixture_resolver_test.rb | 2 +- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 52360d40fe..8595b1e063 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -973,10 +973,11 @@ module ActionMailer templates_name = headers[:template_name] || action_name each_template(Array(templates_path), templates_name).map do |template| - self.formats = [template.format] + format = template.format || self.formats.first + self.formats = [format] { body: render(template: template), - content_type: template.type.to_s + content_type: Mime[format].to_s } end end diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index e5e2771323..ac861c44d4 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -118,7 +118,8 @@ module ActionView renderer.render_to_object(context, options) end - @rendered_format = Template::Types[rendered_template.format] + rendered_format = rendered_template.format || lookup_context.formats.first + @rendered_format = Template::Types[rendered_format] rendered_template.body end diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index 1c463efbb2..f2b8b1166d 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -294,12 +294,12 @@ module ActionView if handler.respond_to?(:default_format) # default_format can return nil handler.default_format else - query_format + nil end end # Template::Types[format] and handler.default_format can return nil - [handler, format || query_format, variant] + [handler, format, variant] end end diff --git a/actionview/test/template/lookup_context_test.rb b/actionview/test/template/lookup_context_test.rb index a763e24226..62935620e9 100644 --- a/actionview/test/template/lookup_context_test.rb +++ b/actionview/test/template/lookup_context_test.rb @@ -121,11 +121,11 @@ class LookupContextTest < ActiveSupport::TestCase assert_equal "Hello texty phone!", template.source end - test "found templates respects given formats if one cannot be found from template or handler" do + test "found templates have nil format if one cannot be found from template or handler" do assert_called(ActionView::Template::Handlers::Builder, :default_format, returns: nil) do @lookup_context.formats = [:text] template = @lookup_context.find("hello", %w(test)) - assert_equal :text, template.format + assert_nil template.format end end diff --git a/actionview/test/template/resolver_patterns_test.rb b/actionview/test/template/resolver_patterns_test.rb index 38357aa10b..8122de779f 100644 --- a/actionview/test/template/resolver_patterns_test.rb +++ b/actionview/test/template/resolver_patterns_test.rb @@ -19,7 +19,7 @@ class ResolverPatternsTest < ActiveSupport::TestCase assert_equal 1, templates.size, "expected one template" assert_equal "Hello custom patterns!", templates.first.source assert_equal "custom_pattern/path", templates.first.virtual_path - assert_equal :html, templates.first.format + assert_nil templates.first.format end def test_should_return_all_templates_when_ambiguous_pattern diff --git a/actionview/test/template/testing/fixture_resolver_test.rb b/actionview/test/template/testing/fixture_resolver_test.rb index 4361824a71..afb6686dac 100644 --- a/actionview/test/template/testing/fixture_resolver_test.rb +++ b/actionview/test/template/testing/fixture_resolver_test.rb @@ -15,6 +15,6 @@ class FixtureResolverTest < ActiveSupport::TestCase assert_equal 1, templates.size, "expected one template" assert_equal "this text", templates.first.source assert_equal "arbitrary/path", templates.first.virtual_path - assert_equal :html, templates.first.format + assert_nil templates.first.format end end From bcd42ae974eb04cb5def7a01d194e70b87bc4152 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 25 Feb 2019 22:56:13 -0800 Subject: [PATCH 3/3] Remove query_format argument from resolver --- actionview/lib/action_view/template/resolver.rb | 4 ++-- actionview/lib/action_view/testing/resolvers.rb | 4 ++-- actionview/test/template/testing/null_resolver_test.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/actionview/lib/action_view/template/resolver.rb b/actionview/lib/action_view/template/resolver.rb index f2b8b1166d..1dc9c9919a 100644 --- a/actionview/lib/action_view/template/resolver.rb +++ b/actionview/lib/action_view/template/resolver.rb @@ -212,7 +212,7 @@ module ActionView template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed template_paths.map do |template| - handler, format, variant = extract_handler_and_format_and_variant(template, formats.first) + handler, format, variant = extract_handler_and_format_and_variant(template) FileTemplate.new(File.expand_path(template), handler, virtual_path: path.virtual, @@ -280,7 +280,7 @@ module ActionView # Extract handler, formats and variant from path. If a format cannot be found neither # from the path, or the handler, we should return the array of formats given # to the resolver. - def extract_handler_and_format_and_variant(path, query_format) + def extract_handler_and_format_and_variant(path) pieces = File.basename(path).split(".") pieces.shift diff --git a/actionview/lib/action_view/testing/resolvers.rb b/actionview/lib/action_view/testing/resolvers.rb index 2305fc9b81..3ca8420c6c 100644 --- a/actionview/lib/action_view/testing/resolvers.rb +++ b/actionview/lib/action_view/testing/resolvers.rb @@ -34,7 +34,7 @@ module ActionView #:nodoc: @hash.each do |_path, array| source, updated_at = array next unless query.match?(_path) - handler, format, variant = extract_handler_and_format_and_variant(_path, :html) + handler, format, variant = extract_handler_and_format_and_variant(_path) templates << Template.new(source, _path, handler, virtual_path: path.virtual, format: format, @@ -50,7 +50,7 @@ module ActionView #:nodoc: class NullResolver < PathResolver def query(path, exts, _, _, locals) - handler, format, variant = extract_handler_and_format_and_variant(path, :html) + handler, format, variant = extract_handler_and_format_and_variant(path) [ActionView::Template.new("Template generated by Null Resolver", path.virtual, handler, virtual_path: path.virtual, format: format, variant: variant, locals: locals)] end end diff --git a/actionview/test/template/testing/null_resolver_test.rb b/actionview/test/template/testing/null_resolver_test.rb index dad8d0966d..c7c78804c0 100644 --- a/actionview/test/template/testing/null_resolver_test.rb +++ b/actionview/test/template/testing/null_resolver_test.rb @@ -9,6 +9,6 @@ class NullResolverTest < ActiveSupport::TestCase assert_equal 1, templates.size, "expected one template" assert_equal "Template generated by Null Resolver", templates.first.source assert_equal "arbitrary/path.erb", templates.first.virtual_path.to_s - assert_equal :html, templates.first.format + assert_nil templates.first.format end end