Skip to content

Commit 364dcbe

Browse files
authored
Include InlineTemplate by default in Base (#1779)
* Include InlineTemplate by default in Base * handle uninitialized ivar * do not worry about coverage of codestats rake task * Update docs/guide/templates.md
1 parent ca8e25f commit 364dcbe

File tree

6 files changed

+11
-15
lines changed

6 files changed

+11
-15
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Include InlineTemplate by default in Base. **Note:** It's no longer necessary to include `ViewComponent::InlineTemplate` to use inline templates.
14+
15+
*Joel Hawksley*
16+
1317
* Allow Setting host when using the `with_request_url` test helper.
1418

1519
*Daniel Alfaro*

docs/guide/templates.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,10 @@ _**Note**: `call_*` methods must be public._
8787
Since 3.0.0
8888
{: .label }
8989

90-
To define a template inside a component, include the experimental `ViewComponent::InlineTemplate` module and call the `.TEMPLATE_HANDLER_template` macro:
90+
To define a template inside a component, call the `.TEMPLATE_HANDLER_template` macro:
9191

9292
```ruby
9393
class InlineErbComponent < ViewComponent::Base
94-
include ViewComponent::InlineTemplate
95-
9694
attr_reader :name
9795

9896
erb_template <<~ERB

lib/view_component/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require "view_component/compiler"
88
require "view_component/config"
99
require "view_component/errors"
10+
require "view_component/inline_template"
1011
require "view_component/preview"
1112
require "view_component/slotable"
1213
require "view_component/translatable"
@@ -29,6 +30,7 @@ def config
2930
attr_writer :config
3031
end
3132

33+
include ViewComponent::InlineTemplate
3234
include ViewComponent::Slotable
3335
include ViewComponent::Translatable
3436
include ViewComponent::WithContentHelper

lib/view_component/inline_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def respond_to_missing?(method, include_all = false)
3939
end
4040

4141
def inline_template
42-
@__vc_inline_template
42+
@__vc_inline_template if defined?(@__vc_inline_template)
4343
end
4444

4545
def inline_template_language

lib/view_component/rails/tasks/view_component.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ task stats: "view_component:statsetup"
44

55
namespace :view_component do
66
task :statsetup do
7+
# :nocov:
78
require "rails/code_statistics"
89

910
::STATS_DIRECTORIES << ["ViewComponents", ViewComponent::Base.view_component_path]
11+
# :nocov:
1012
end
1113
end

test/sandbox/test/inline_template_test.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
class InlineErbTest < ViewComponent::TestCase
66
class InlineErbComponent < ViewComponent::Base
7-
include ViewComponent::InlineTemplate
8-
97
attr_reader :name
108

119
erb_template <<~ERB
@@ -18,8 +16,6 @@ def initialize(name)
1816
end
1917

2018
class InlineRaiseErbComponent < ViewComponent::Base
21-
include ViewComponent::InlineTemplate
22-
2319
attr_reader :name
2420

2521
erb_template <<~ERB
@@ -38,8 +34,6 @@ class InlineErbSubclassComponent < InlineErbComponent
3834
end
3935

4036
class InlineSlimComponent < ViewComponent::Base
41-
include ViewComponent::InlineTemplate
42-
4337
attr_reader :name
4438

4539
slim_template <<~SLIM
@@ -58,8 +52,6 @@ class InheritedInlineSlimComponent < InlineSlimComponent
5852
end
5953

6054
class SlotsInlineComponent < ViewComponent::Base
61-
include ViewComponent::InlineTemplate
62-
6355
renders_one :greeting, InlineErbComponent
6456

6557
erb_template <<~ERB
@@ -73,8 +65,6 @@ class ParentBaseComponent < ViewComponent::Base
7365
end
7466

7567
class InlineErbChildComponent < ParentBaseComponent
76-
include ViewComponent::InlineTemplate
77-
7868
attr_reader :name
7969

8070
erb_template <<~ERB
@@ -103,7 +93,7 @@ def initialize(name)
10393
render_inline(InlineRaiseErbComponent.new("Fox Mulder"))
10494
end
10595

106-
assert_match %r{test/sandbox/test/inline_template_test.rb:26}, error.backtrace[0]
96+
assert_match %r{test/sandbox/test/inline_template_test.rb:22}, error.backtrace[0]
10797
end
10898

10999
test "renders inline slim templates" do

0 commit comments

Comments
 (0)