From bcd44217aa3f0b503ea9b0e0b9e1568d307dae8d Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 12:13:36 -0600 Subject: [PATCH 1/6] CI should fail --- CHANGELOG.md | 4 ++++ test/view_component/integration_test.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index afe7b1d83..37e15f9ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # master +* Add support for `config.action_view.annotate_template_file_names` (coming in Rails 6.1). + + *Joel Hawksley* + # v2.1.0 * Support rendering collections (e.g., `render(MyComponent.with_collection(@items))`). diff --git a/test/view_component/integration_test.rb b/test/view_component/integration_test.rb index 6921c93da..98964cdc9 100644 --- a/test/view_component/integration_test.rb +++ b/test/view_component/integration_test.rb @@ -10,6 +10,21 @@ class IntegrationTest < ActionDispatch::IntegrationTest assert_select("div", "Foo\n bar") end + if Rails.version.to_f >= 6.1 + test "rendering component with template annotations enabled" do + begin + ActionView::Base.annotate_template_file_names = true + + get "/" + assert_response :success + + assert_select("div", "Foo\n bar") + ensure + ActionView::Base.annotate_template_file_names = false + end + end + end + test "rendering component in a controller" do get "/controller_inline_baseline" From 20df295b9b1195407f3f248d3103cd336d2cfe10 Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 14:10:20 -0600 Subject: [PATCH 2/6] remove comment about upstreaming --- lib/view_component/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index a92dffe06..64854c4d8 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -283,7 +283,7 @@ def compiled_template(file_path) if handler.method(:call).parameters.length > 1 handler.call(self, template) - else # remove before upstreaming into Rails + else handler.call(OpenStruct.new(source: template, identifier: identifier, type: type)) end end From 468a435675613f60c7575b7003b407e31645e1e2 Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 14:10:39 -0600 Subject: [PATCH 3/6] do not include better_html in demo app --- test/config/application.rb | 1 - test/view_component/better_html_test.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/test/config/application.rb b/test/config/application.rb index 52b0d0a7a..b9658c960 100644 --- a/test/config/application.rb +++ b/test/config/application.rb @@ -7,7 +7,6 @@ require "action_view/railtie" require "view_component/engine" require "sprockets/railtie" -require "better_html" require "haml" require "slim" diff --git a/test/view_component/better_html_test.rb b/test/view_component/better_html_test.rb index e6a67f3ad..f1e1aabad 100644 --- a/test/view_component/better_html_test.rb +++ b/test/view_component/better_html_test.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "test_helper" +require "better_html" require "better_html/test_helper/safe_erb_tester" # This test exists to ensure basic non-breaking compatiblity with Shopify/better_html From 5049b2ab5f760f9dd442a59b8668e2cb76c0931f Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 14:11:15 -0600 Subject: [PATCH 4/6] view_component is compatible with annotate_template_file_names --- lib/view_component/base.rb | 4 ++++ test/config/environments/test.rb | 2 ++ test/view_component/integration_test.rb | 19 +++++++------------ test/view_component/view_component_test.rb | 5 ++--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 64854c4d8..da08affbf 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -77,6 +77,10 @@ def render? true end + def self.short_identifier + @short_identifier ||= defined?(Rails.root) ? source_location.sub("#{Rails.root}/", "") : source_location + end + def initialize(*); end def render(options = {}, args = {}, &block) diff --git a/test/config/environments/test.rb b/test/config/environments/test.rb index e14d982c8..5a7e8d4ea 100644 --- a/test/config/environments/test.rb +++ b/test/config/environments/test.rb @@ -36,5 +36,7 @@ # Print deprecation notices to the stderr config.active_support.deprecation = :stderr + config.action_view.annotate_template_file_names = true if Rails.version.to_f >= 6.1 + config.eager_load = true end diff --git a/test/view_component/integration_test.rb b/test/view_component/integration_test.rb index 98964cdc9..2b49da03a 100644 --- a/test/view_component/integration_test.rb +++ b/test/view_component/integration_test.rb @@ -12,16 +12,12 @@ class IntegrationTest < ActionDispatch::IntegrationTest if Rails.version.to_f >= 6.1 test "rendering component with template annotations enabled" do - begin - ActionView::Base.annotate_template_file_names = true + get "/" + assert_response :success - get "/" - assert_response :success + assert_includes response.body, "BEGIN app/components/erb_component.rb" - assert_select("div", "Foo\n bar") - ensure - ActionView::Base.annotate_template_file_names = false - end + assert_select("div", "Foo\n bar") end end @@ -39,7 +35,7 @@ class IntegrationTest < ActionDispatch::IntegrationTest inline_response = response.body - assert_equal baseline_response, inline_response + assert_includes inline_response, baseline_response end test "rendering component with content" do @@ -63,8 +59,7 @@ class IntegrationTest < ActionDispatch::IntegrationTest get "/partial" assert_response :success - assert_includes response.body, "partial:
hello,partial world!" - assert_includes response.body, "component:
hello,partial world!" + assert_select("div", "hello,partial world!", count: 2) end test "rendering component without variant" do @@ -126,7 +121,7 @@ class IntegrationTest < ActionDispatch::IntegrationTest get "/render_check" assert_response :success - assert_empty response.body.strip + refute_includes response.body, "Rendered" end test "renders component preview" do diff --git a/test/view_component/view_component_test.rb b/test/view_component/view_component_test.rb index a08ad7d04..078b20f4a 100644 --- a/test/view_component/view_component_test.rb +++ b/test/view_component/view_component_test.rb @@ -89,7 +89,7 @@ def test_renders_erb_template def test_renders_partial_template render_inline(PartialComponent.new) - assert_text("hello,partial world!\n\nhello,partial world!") + assert_text("hello,partial world!", count: 2) end def test_renders_content_for_template @@ -378,10 +378,9 @@ def test_backtrace_returns_correct_file_and_line_number render_inline(ExceptionInTemplateComponent.new) end - assert_match %r[app/components/exception_in_template_component\.html\.erb:2], error.backtrace[0] + assert_match %r[app/components/exception_in_template_component\.html\.erb:3], error.backtrace[0] end - def test_render_collection products = [OpenStruct.new(name: "Radio clock"), OpenStruct.new(name: "Mints")] render_inline(ProductComponent.with_collection(products, notice: "On sale")) From 760b6bcaea6c0c9e123ca711ee548063bd8359f2 Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 14:13:53 -0600 Subject: [PATCH 5/6] fix matcher --- test/view_component/view_component_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/view_component/view_component_test.rb b/test/view_component/view_component_test.rb index 078b20f4a..2802436e0 100644 --- a/test/view_component/view_component_test.rb +++ b/test/view_component/view_component_test.rb @@ -378,7 +378,7 @@ def test_backtrace_returns_correct_file_and_line_number render_inline(ExceptionInTemplateComponent.new) end - assert_match %r[app/components/exception_in_template_component\.html\.erb:3], error.backtrace[0] + assert_match %r[app/components/exception_in_template_component\.html\.erb], error.backtrace[0] end def test_render_collection From fb3cf49821e838fa1fda73866c1a9b2a1bcce28f Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Tue, 31 Mar 2020 14:22:46 -0600 Subject: [PATCH 6/6] return correct line number regardless of whether annotations are on --- lib/view_component/base.rb | 14 +++++++++++++- test/view_component/view_component_test.rb | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb index 27e99b1b1..ddcbc51d1 100644 --- a/lib/view_component/base.rb +++ b/lib/view_component/base.rb @@ -184,8 +184,20 @@ def compile(raise_template_errors: false) return false end + # If template name annotations are turned on, a line is dynamically + # added with a comment. In this case, we want to return a different + # starting line number so errors that are raised will point to the + # correct line in the component template. + line_number = + if ActionView::Base.respond_to?(:annotate_template_file_names) && + ActionView::Base.annotate_template_file_names + -2 + else + -1 + end + templates.each do |template| - class_eval <<-RUBY, template[:path], -1 + class_eval <<-RUBY, template[:path], line_number def #{call_method_name(template[:variant])} @output_buffer = ActionView::OutputBuffer.new #{compiled_template(template[:path])} diff --git a/test/view_component/view_component_test.rb b/test/view_component/view_component_test.rb index 812bdb1a6..0f9d09286 100644 --- a/test/view_component/view_component_test.rb +++ b/test/view_component/view_component_test.rb @@ -376,7 +376,7 @@ def test_backtrace_returns_correct_file_and_line_number render_inline(ExceptionInTemplateComponent.new) end - assert_match %r[app/components/exception_in_template_component\.html\.erb], error.backtrace[0] + assert_match %r[app/components/exception_in_template_component\.html\.erb:2], error.backtrace[0] end def test_render_collection