Skip to content

Commit

Permalink
Merge pull request #253 from seanpdoyle/tag-helper-in-previews
Browse files Browse the repository at this point in the history
Make `TagHelper` available in Previews
  • Loading branch information
joelhawksley authored Mar 7, 2020
2 parents 2361506 + 027a2da commit 5970b11
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# master

* Make `ActionView::Helpers::TagHelper` available in Previews

def with_html_content
render(MyComponent.new) do
tag.div do
content_tag(:span, "Hello")
end
end
end

*Sean Doyle*

# v1.14.1

* Fix bug where generator created invalid test code.
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,28 @@ class TestComponentPreview < ViewComponent::Preview
def with_long_title
render(TestComponent.new(title: "This is a really long title to see how the component renders this"))
end

def with_content_block
render(TestComponent.new(title: "This component accepts a block of content") do
tag.div do
content_tag(:span, "Hello")
end
end
end
end
```

Which generates <http://localhost:3000/rails/components/test_component/with_default_title>
and <http://localhost:3000/rails/components/test_component/with_long_title>.
Which generates <http://localhost:3000/rails/components/test_component/with_default_title>,
<http://localhost:3000/rails/components/test_component/with_long_title>,
and <http://localhost:3000/rails/components/test_component/with_content_block>.

The `ViewComponent::Preview` base class includes
[`ActionView::Helpers::TagHelper`][tag-helper], which provides the [`tag`][tag]
and [`content_tag`][content_tag] view helper methods.

[tag-helper]: https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html
[tag]: https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag
[content_tag]: https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag

Previews default to the application layout, but can be overridden:

Expand Down Expand Up @@ -413,6 +430,11 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/github
|@blakewilliams|
|Boston, MA|

|<img src="https://avatars.githubusercontent.com/seanpdoyle?s=256" alt="seanpdoyle" width="128" />|
|:---:|
|@seanpdoyle|
|New York, NY|

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
1 change: 1 addition & 0 deletions lib/view_component/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module ViewComponent # :nodoc:
class Preview
include ActionView::Helpers::TagHelper
extend ActiveSupport::DescendantsTracker

def render(component, **args, &block)
Expand Down
4 changes: 4 additions & 0 deletions test/test/components/previews/preview_component_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def without_cta
def with_content
render(PreviewComponent.new(title: "title")) { "some content" }
end

def with_tag_helper_in_content
render(PreviewComponent.new(title: "title")) { content_tag(:span, "some content") }
end
end
6 changes: 6 additions & 0 deletions test/view_component/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ class IntegrationTest < ActionDispatch::IntegrationTest
assert_includes response.body, "some content"
end

test "renders preview component with tag helper-generated content preview" do
get "/rails/components/preview_component/with_tag_helper_in_content"

assert_includes response.body, "<span>some content</span>"
end

test "renders badge component open preview" do
get "/rails/components/issues/badge_component/open"

Expand Down

0 comments on commit 5970b11

Please sign in to comment.