diff options
author | John Hawthorn <john@hawthorn.email> | 2019-02-25 14:43:30 -0800 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2019-02-25 17:43:30 -0500 |
commit | dcb13470991539ab581e02670738900c39976ff4 (patch) | |
tree | 4fdbfa0b79aa04f0bbee7c7ca2e0b81bc86feb02 /actionview | |
parent | 56b030605b4d968077a4ddb96b4ab619e75fb999 (diff) | |
download | rails-dcb13470991539ab581e02670738900c39976ff4.tar.gz rails-dcb13470991539ab581e02670738900c39976ff4.tar.bz2 rails-dcb13470991539ab581e02670738900c39976ff4.zip |
Improve Template#inspect output (#35407)
* Don't call inspect from identifier_method_name
* Add locals Template#inspect
Handler, formats, and variant are usually obvious from looking at the
identifier. However it's not uncommon to have different locals for the
same template so we should make that obvious in inspect.
* Add tests for short_identifier and inspect
[John Hawthorn + Rafael Mendonça França]
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/lib/action_view/template.rb | 8 | ||||
-rw-r--r-- | actionview/test/template/template_test.rb | 10 |
2 files changed, 16 insertions, 2 deletions
diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index cc5e0cbb4f..ca387e9229 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -203,8 +203,12 @@ module ActionView end end + def short_identifier + @short_identifier ||= defined?(Rails.root) ? identifier.sub("#{Rails.root}/", "") : identifier + end + def inspect - @inspect ||= defined?(Rails.root) ? identifier.sub("#{Rails.root}/", "") : identifier + "#<#{self.class.name} #{short_identifier} locals=#{@locals.inspect}>" end # This method is responsible for properly setting the encoding of the @@ -378,7 +382,7 @@ module ActionView end def identifier_method_name - inspect.tr("^a-z_", "_") + short_identifier.tr("^a-z_", "_") end def instrument(action, &block) # :doc: diff --git a/actionview/test/template/template_test.rb b/actionview/test/template/template_test.rb index 36caef28c2..c74305fb42 100644 --- a/actionview/test/template/template_test.rb +++ b/actionview/test/template/template_test.rb @@ -215,4 +215,14 @@ class TestERBTemplate < ActiveSupport::TestCase ensure silence_warnings { Encoding.default_external = old } end + + def test_short_identifier + @template = new_template("hello") + assert_equal "hello template", @template.short_identifier + end + + def test_template_inspect + @template = new_template("hello") + assert_equal "#<ActionView::Template hello template locals=[]>", @template.inspect + end end |