Skip to content

Commit 31d9570

Browse files
authored
Add #content? (#1649)
* #content is part of the public API * #content? indicates of content has been passed to component * add changelog, fix test names * check for content being passed, not the content itself
1 parent bca3742 commit 31d9570

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
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+
* Add `#content?` method that indicates if content has been passed to component.
14+
15+
*Joel Hawksley*
16+
1317
* Added example of a custom preview controller.
1418

1519
*Graham Rogers*

lib/view_component/base.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,40 @@ def request
245245
@request ||= controller.request if controller.respond_to?(:request)
246246
end
247247

248-
private
249-
250-
attr_reader :view_context
251-
248+
# The content passed to the component instance as a block.
249+
#
250+
# @return [String]
252251
def content
253252
@__vc_content_evaluated = true
254253
return @__vc_content if defined?(@__vc_content)
255254

256255
@__vc_content =
257-
if @view_context && @__vc_render_in_block
256+
if __vc_render_in_block_provided?
258257
view_context.capture(self, &@__vc_render_in_block)
259-
elsif defined?(@__vc_content_set_by_with_content)
258+
elsif __vc_content_set_by_with_content_defined?
260259
@__vc_content_set_by_with_content
261260
end
262261
end
263262

263+
# Whether `content` has been passed to the component.
264+
#
265+
# @return [Boolean]
266+
def content?
267+
__vc_render_in_block_provided? || __vc_content_set_by_with_content_defined?
268+
end
269+
270+
private
271+
272+
attr_reader :view_context
273+
274+
def __vc_render_in_block_provided?
275+
@view_context && @__vc_render_in_block
276+
end
277+
278+
def __vc_content_set_by_with_content_defined?
279+
defined?(@__vc_content_set_by_with_content)
280+
end
281+
264282
def content_evaluated?
265283
@__vc_content_evaluated
266284
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class ContentPredicateComponent < ViewComponent::Base
4+
def call
5+
if content?
6+
content
7+
else
8+
"Default"
9+
end
10+
end
11+
end

test/sandbox/test/rendering_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,4 +1038,16 @@ def test_concurrency_deadlock
10381038
end
10391039
end
10401040
end
1041+
1042+
def test_content_predicate_false
1043+
render_inline(ContentPredicateComponent.new)
1044+
1045+
assert_text("Default")
1046+
end
1047+
1048+
def test_content_predicate_true
1049+
render_inline(ContentPredicateComponent.new.with_content("foo"))
1050+
1051+
assert_text("foo")
1052+
end
10411053
end

0 commit comments

Comments
 (0)