Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.annotate_template_file_names annotates HTML output with template names #38848

Merged
merged 1 commit into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions actionmailbox/test/dummy/config/environments/development.rb
Expand Up @@ -57,6 +57,9 @@
# 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
Expand Down
3 changes: 3 additions & 0 deletions actionmailbox/test/dummy/config/environments/test.rb
Expand Up @@ -43,4 +43,7 @@

# 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
3 changes: 3 additions & 0 deletions actiontext/test/dummy/config/environments/development.rb
Expand Up @@ -57,6 +57,9 @@
# 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
Expand Down
3 changes: 3 additions & 0 deletions actiontext/test/dummy/config/environments/test.rb
Expand Up @@ -43,4 +43,7 @@

# 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
4 changes: 4 additions & 0 deletions 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`.

Expand Down
3 changes: 3 additions & 0 deletions actionview/lib/action_view/base.rb
Expand Up @@ -162,6 +162,9 @@ class Base
# 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

Expand Down
3 changes: 2 additions & 1 deletion actionview/lib/action_view/template/handlers/erb.rb
Expand Up @@ -58,7 +58,8 @@ def call(template, source)
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

Expand Down
11 changes: 9 additions & 2 deletions actionview/lib/action_view/template/handlers/erb/erubi.rb
Expand Up @@ -13,8 +13,15 @@ def initialize(input, properties = {})

# 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='<!-- BEGIN #{properties[:short_identifier]} -->\n';"
properties[:postamble] = "@output_buffer.safe_append='<!-- END #{properties[:short_identifier]} -->\n';@output_buffer.to_s"
else
properties[:preamble] = ""
properties[:postamble] = "@output_buffer.to_s"
end

properties[:bufvar] = "@output_buffer"
properties[:escapefunc] = ""

Expand Down
18 changes: 18 additions & 0 deletions actionview/test/actionpack/controller/render_test.rb
Expand Up @@ -1453,4 +1453,22 @@ def test_render_call_to_partial_with_layout_in_main_layout_and_within_content_fo
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, "<!-- BEGIN"
assert_includes lines.first, "test/fixtures/actionpack/test/render_file_with_locals.erb -->"

assert_includes lines[1], "The secret is area51"

assert_includes lines.last, "<!-- END"
assert_includes lines.last, "test/fixtures/actionpack/test/render_file_with_locals.erb -->"
ensure
ActionView::Base.annotate_template_file_names = false
end
end
3 changes: 3 additions & 0 deletions activestorage/test/dummy/config/environments/development.rb
Expand Up @@ -46,6 +46,9 @@
# 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
Expand Down
3 changes: 3 additions & 0 deletions activestorage/test/dummy/config/environments/test.rb
Expand Up @@ -35,4 +35,7 @@

# 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