Skip to content

Commit

Permalink
Merge pull request #278 from github/short_identifier
Browse files Browse the repository at this point in the history
Add support for config.action_view.annotate_template_file_names
  • Loading branch information
joelhawksley authored Mar 31, 2020
2 parents bc44eed + fb3cf49 commit 05c4fe4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# master

* Add support for `config.action_view.annotate_template_file_names` (coming in Rails 6.1).

*Joel Hawksley*

* Remove initializer requirement from the component.

*Vasiliy Ermolovich*
Expand Down
20 changes: 18 additions & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -180,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])}
Expand Down Expand Up @@ -271,7 +287,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
Expand Down
1 change: 0 additions & 1 deletion test/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
require "action_view/railtie"
require "view_component/engine"
require "sprockets/railtie"
require "better_html"

require "haml"
require "slim"
Expand Down
2 changes: 2 additions & 0 deletions test/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/view_component/better_html_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 14 additions & 4 deletions test/view_component/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ 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
get "/"
assert_response :success

assert_includes response.body, "BEGIN app/components/erb_component.rb"

assert_select("div", "Foo\n bar")
end
end

test "rendering component in a controller" do
get "/controller_inline_baseline"

Expand All @@ -24,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
Expand All @@ -48,8 +59,7 @@ class IntegrationTest < ActionDispatch::IntegrationTest
get "/partial"
assert_response :success

assert_includes response.body, "partial:<div>hello,partial world!"
assert_includes response.body, "component:<div>hello,partial world!"
assert_select("div", "hello,partial world!", count: 2)
end

test "rendering component without variant" do
Expand Down Expand Up @@ -111,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
Expand Down
3 changes: 1 addition & 2 deletions test/view_component/view_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -379,7 +379,6 @@ def test_backtrace_returns_correct_file_and_line_number
assert_match %r[app/components/exception_in_template_component\.html\.erb:2], 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"))
Expand Down

0 comments on commit 05c4fe4

Please sign in to comment.