Skip to content

Commit

Permalink
Merge pull request rails#35429 from jhawthorn/template_format_nil
Browse files Browse the repository at this point in the history
Allow nil format on templates
  • Loading branch information
tenderlove authored Mar 1, 2019
2 parents 2939c2c + bcd42ae commit ed6364f
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 19 deletions.
5 changes: 3 additions & 2 deletions actionmailer/lib/action_mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,11 @@ def collect_responses_from_templates(headers)
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
Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def _render_template(options)
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
Expand Down
5 changes: 0 additions & 5 deletions actionview/lib/action_view/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,6 @@ def self.finalize_compiled_template_methods=(_)
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 = []
Expand Down
8 changes: 4 additions & 4 deletions actionview/lib/action_view/template/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def query(path, details, formats, outside_app_allowed, locals)
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,
Expand Down Expand Up @@ -280,7 +280,7 @@ def mtime(p)
# 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

Expand All @@ -294,12 +294,12 @@ def extract_handler_and_format_and_variant(path, query_format)
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

Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/testing/resolvers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def query(path, exts, _, _, locals)
@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,
Expand All @@ -50,7 +50,7 @@ def query(path, exts, _, _, locals)

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
Expand Down
4 changes: 2 additions & 2 deletions actionview/test/template/lookup_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ def teardown
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

Expand Down
2 changes: 1 addition & 1 deletion actionview/test/template/resolver_patterns_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_should_return_template_for_declared_path
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
Expand Down
2 changes: 1 addition & 1 deletion actionview/test/template/testing/fixture_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def test_should_return_template_for_declared_path
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
2 changes: 1 addition & 1 deletion actionview/test/template/testing/null_resolver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ def test_should_return_template_for_any_path
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

0 comments on commit ed6364f

Please sign in to comment.