From a59e1de26a4b0065d4309851901493032369963c Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Mon, 30 Mar 2020 14:16:31 -0600 Subject: [PATCH] .annotate_template_file_names annotates HTML output with template file names As a developer, when looking at a page in my web browser, it's sometimes difficult to figure out which template(s) are being used to render the page. config.action_view.annotate_template_file_names adds HTML comments to the rendered output indicating where each template begins and ends. Co-authored-by: Aaron Patterson --- .../dummy/config/environments/development.rb | 3 +++ .../test/dummy/config/environments/test.rb | 3 +++ .../dummy/config/environments/development.rb | 3 +++ .../test/dummy/config/environments/test.rb | 3 +++ actionview/CHANGELOG.md | 4 ++++ actionview/lib/action_view/base.rb | 3 +++ .../lib/action_view/template/handlers/erb.rb | 3 ++- .../action_view/template/handlers/erb/erubi.rb | 11 +++++++++-- .../test/actionpack/controller/render_test.rb | 18 ++++++++++++++++++ .../dummy/config/environments/development.rb | 3 +++ .../test/dummy/config/environments/test.rb | 3 +++ 11 files changed, 54 insertions(+), 3 deletions(-) diff --git a/actionmailbox/test/dummy/config/environments/development.rb b/actionmailbox/test/dummy/config/environments/development.rb index a8bba7811ff..7a9d9419d79 100644 --- a/actionmailbox/test/dummy/config/environments/development.rb +++ b/actionmailbox/test/dummy/config/environments/development.rb @@ -57,6 +57,9 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true + # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. # config.file_watcher = ActiveSupport::EventedFileUpdateChecker diff --git a/actionmailbox/test/dummy/config/environments/test.rb b/actionmailbox/test/dummy/config/environments/test.rb index 0a38fd3ce92..6c2cd6f8ed5 100644 --- a/actionmailbox/test/dummy/config/environments/test.rb +++ b/actionmailbox/test/dummy/config/environments/test.rb @@ -43,4 +43,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true end diff --git a/actiontext/test/dummy/config/environments/development.rb b/actiontext/test/dummy/config/environments/development.rb index a8bba7811ff..7a9d9419d79 100644 --- a/actiontext/test/dummy/config/environments/development.rb +++ b/actiontext/test/dummy/config/environments/development.rb @@ -57,6 +57,9 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true + # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. # config.file_watcher = ActiveSupport::EventedFileUpdateChecker diff --git a/actiontext/test/dummy/config/environments/test.rb b/actiontext/test/dummy/config/environments/test.rb index 0a38fd3ce92..6c2cd6f8ed5 100644 --- a/actiontext/test/dummy/config/environments/test.rb +++ b/actiontext/test/dummy/config/environments/test.rb @@ -43,4 +43,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true end diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 190c99d6287..398a7f25534 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,7 @@ +* `ActionView::Base.annotate_template_file_names` annotates HTML output with template file names. + + *Joel Hawksley*, *Aaron Patterson* + * `ActionView::Helpers::TranslationHelper#translate` returns nil when passed `default: nil` without a translation matching `I18n#translate`. diff --git a/actionview/lib/action_view/base.rb b/actionview/lib/action_view/base.rb index e13988b9295..ca1ce14b0bb 100644 --- a/actionview/lib/action_view/base.rb +++ b/actionview/lib/action_view/base.rb @@ -162,6 +162,9 @@ module ActionView #:nodoc: # Specify whether submit_tag should automatically disable on click cattr_accessor :automatically_disable_submit_tag, default: true + # Render template filenames as comments in HTML + cattr_accessor :annotate_template_file_names, default: false + class_attribute :_routes class_attribute :logger diff --git a/actionview/lib/action_view/template/handlers/erb.rb b/actionview/lib/action_view/template/handlers/erb.rb index 69a5a0b1b9c..c98685843f9 100644 --- a/actionview/lib/action_view/template/handlers/erb.rb +++ b/actionview/lib/action_view/template/handlers/erb.rb @@ -58,7 +58,8 @@ module ActionView self.class.erb_implementation.new( erb, escape: (self.class.escape_ignore_list.include? template.type), - trim: (self.class.erb_trim_mode == "-") + trim: (self.class.erb_trim_mode == "-"), + short_identifier: template.short_identifier ).src end diff --git a/actionview/lib/action_view/template/handlers/erb/erubi.rb b/actionview/lib/action_view/template/handlers/erb/erubi.rb index e602aa117af..b58560063d1 100644 --- a/actionview/lib/action_view/template/handlers/erb/erubi.rb +++ b/actionview/lib/action_view/template/handlers/erb/erubi.rb @@ -13,8 +13,15 @@ module ActionView # Dup properties so that we don't modify argument properties = Hash[properties] - properties[:preamble] = "" - properties[:postamble] = "@output_buffer.to_s" + + if ActionView::Base.annotate_template_file_names + properties[:preamble] = "@output_buffer.safe_append='\n';" + properties[:postamble] = "@output_buffer.safe_append='\n';@output_buffer.to_s" + else + properties[:preamble] = "" + properties[:postamble] = "@output_buffer.to_s" + end + properties[:bufvar] = "@output_buffer" properties[:escapefunc] = "" diff --git a/actionview/test/actionpack/controller/render_test.rb b/actionview/test/actionpack/controller/render_test.rb index 64070a95429..707e4b98f4e 100644 --- a/actionview/test/actionpack/controller/render_test.rb +++ b/actionview/test/actionpack/controller/render_test.rb @@ -1453,4 +1453,22 @@ class RenderTest < ActionController::TestCase get :render_call_to_partial_with_layout_in_main_layout_and_within_content_for_layout assert_equal "Before (Anthony)\nInside from partial (Anthony)\nAfter\nBefore (David)\nInside from partial (David)\nAfter\nBefore (Ramm)\nInside from partial (Ramm)\nAfter", @response.body end + + def test_template_annotations + ActionView::Base.annotate_template_file_names = true + + get :render_with_explicit_template_with_locals + + lines = @response.body.split("\n") + + assert_includes lines.first, "" + + assert_includes lines[1], "The secret is area51" + + assert_includes lines.last, "" + ensure + ActionView::Base.annotate_template_file_names = false + end end diff --git a/activestorage/test/dummy/config/environments/development.rb b/activestorage/test/dummy/config/environments/development.rb index b38e7f614c9..e04ffa0dd40 100644 --- a/activestorage/test/dummy/config/environments/development.rb +++ b/activestorage/test/dummy/config/environments/development.rb @@ -46,6 +46,9 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true + # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. # config.file_watcher = ActiveSupport::EventedFileUpdateChecker diff --git a/activestorage/test/dummy/config/environments/test.rb b/activestorage/test/dummy/config/environments/test.rb index e89182bb5c3..b9c2799da00 100644 --- a/activestorage/test/dummy/config/environments/test.rb +++ b/activestorage/test/dummy/config/environments/test.rb @@ -35,4 +35,7 @@ Rails.application.configure do # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + # Render template filenames as comments in HTML + # config.action_view.annotate_template_file_names = true end