From a7ed92bd1d92127423ad6dce608d68d451a26821 Mon Sep 17 00:00:00 2001 From: Petrik Date: Fri, 10 Jul 2020 10:20:22 +0200 Subject: [PATCH] Use superclass name in inspect of ActionView::Base.with_empty_template_cache When rendering views an anonymous subclass is created by calling ActionView::Base.with_empty_template_cache. This causes inspect to return an unhelpful description when calling inspect: `#<#:<0x012345012345>`. This can be confusing when exceptions are raised because it's hard to figure out where to look. For example calling an undefined method in a template would raise the following exception: undefined method `undefined' for #<#:<0x012345012345> Instead we can return the non-anonymous superclass name. undefined method `undefined' for # The anonymous class is created in ActionView::Base.with_empty_template_cache. See f9bea6304dfba902b1937b3bc29b1ebc2f67e55b This seems to be done for performance reasons only, without expecting a change to calling `inspect`. --- actionview/lib/action_view/base.rb | 8 ++++++++ actionview/test/template/render_test.rb | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/actionview/lib/action_view/base.rb b/actionview/lib/action_view/base.rb index 3840ac0caf..ab9e217648 100644 --- a/actionview/lib/action_view/base.rb +++ b/actionview/lib/action_view/base.rb @@ -190,6 +190,14 @@ def with_empty_template_cache # :nodoc: # correctly. define_method(:compiled_method_container) { subclass } define_singleton_method(:compiled_method_container) { subclass } + + def self.name + superclass.name + end + + def inspect + "#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>" + end } end diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index 82dd56b99f..541b0b13b7 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -327,6 +327,11 @@ def test_render_file_with_errors assert_equal File.expand_path("#{FIXTURE_LOAD_PATH}/test/_raise.html.erb"), e.file_name end + def test_undefined_method_error_references_named_class + e = assert_raises(ActionView::Template::Error) { @view.render(inline: "<%= undefined %>") } + assert_match(/`undefined' for #/, e.message) + end + def test_render_object assert_equal "Hello: david", @view.render(partial: "test/customer", object: Customer.new("david")) assert_equal "FalseClass", @view.render(partial: "test/klass", object: false) -- GitLab