Skip to content

Commit

Permalink
Include InlineTemplate by default in Base (#1779)
Browse files Browse the repository at this point in the history
* Include InlineTemplate by default in Base

* handle uninitialized ivar

* do not worry about coverage of codestats rake task

* Update docs/guide/templates.md
  • Loading branch information
joelhawksley authored Jun 23, 2023
1 parent ca8e25f commit 364dcbe
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 15 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Include InlineTemplate by default in Base. **Note:** It's no longer necessary to include `ViewComponent::InlineTemplate` to use inline templates.

*Joel Hawksley*

* Allow Setting host when using the `with_request_url` test helper.

*Daniel Alfaro*
Expand Down
4 changes: 1 addition & 3 deletions docs/guide/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ _**Note**: `call_*` methods must be public._
Since 3.0.0
{: .label }

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

```ruby
class InlineErbComponent < ViewComponent::Base
include ViewComponent::InlineTemplate

attr_reader :name

erb_template <<~ERB
Expand Down
2 changes: 2 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "view_component/compiler"
require "view_component/config"
require "view_component/errors"
require "view_component/inline_template"
require "view_component/preview"
require "view_component/slotable"
require "view_component/translatable"
Expand All @@ -29,6 +30,7 @@ def config
attr_writer :config
end

include ViewComponent::InlineTemplate
include ViewComponent::Slotable
include ViewComponent::Translatable
include ViewComponent::WithContentHelper
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/inline_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def respond_to_missing?(method, include_all = false)
end

def inline_template
@__vc_inline_template
@__vc_inline_template if defined?(@__vc_inline_template)
end

def inline_template_language
Expand Down
2 changes: 2 additions & 0 deletions lib/view_component/rails/tasks/view_component.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ task stats: "view_component:statsetup"

namespace :view_component do
task :statsetup do
# :nocov:
require "rails/code_statistics"

::STATS_DIRECTORIES << ["ViewComponents", ViewComponent::Base.view_component_path]
# :nocov:
end
end
12 changes: 1 addition & 11 deletions test/sandbox/test/inline_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

class InlineErbTest < ViewComponent::TestCase
class InlineErbComponent < ViewComponent::Base
include ViewComponent::InlineTemplate

attr_reader :name

erb_template <<~ERB
Expand All @@ -18,8 +16,6 @@ def initialize(name)
end

class InlineRaiseErbComponent < ViewComponent::Base
include ViewComponent::InlineTemplate

attr_reader :name

erb_template <<~ERB
Expand All @@ -38,8 +34,6 @@ class InlineErbSubclassComponent < InlineErbComponent
end

class InlineSlimComponent < ViewComponent::Base
include ViewComponent::InlineTemplate

attr_reader :name

slim_template <<~SLIM
Expand All @@ -58,8 +52,6 @@ class InheritedInlineSlimComponent < InlineSlimComponent
end

class SlotsInlineComponent < ViewComponent::Base
include ViewComponent::InlineTemplate

renders_one :greeting, InlineErbComponent

erb_template <<~ERB
Expand All @@ -73,8 +65,6 @@ class ParentBaseComponent < ViewComponent::Base
end

class InlineErbChildComponent < ParentBaseComponent
include ViewComponent::InlineTemplate

attr_reader :name

erb_template <<~ERB
Expand Down Expand Up @@ -103,7 +93,7 @@ def initialize(name)
render_inline(InlineRaiseErbComponent.new("Fox Mulder"))
end

assert_match %r{test/sandbox/test/inline_template_test.rb:26}, error.backtrace[0]
assert_match %r{test/sandbox/test/inline_template_test.rb:22}, error.backtrace[0]
end

test "renders inline slim templates" do
Expand Down

0 comments on commit 364dcbe

Please sign in to comment.