File tree Expand file tree Collapse file tree 4 files changed +51
-6
lines changed Expand file tree Collapse file tree 4 files changed +51
-6
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ nav_order: 5
10
10
11
11
## main
12
12
13
+ * Add ` #content? ` method that indicates if content has been passed to component.
14
+
15
+ * Joel Hawksley*
16
+
13
17
* Added example of a custom preview controller.
14
18
15
19
* Graham Rogers*
Original file line number Diff line number Diff line change @@ -245,22 +245,40 @@ def request
245
245
@request ||= controller . request if controller . respond_to? ( :request )
246
246
end
247
247
248
- private
249
-
250
- attr_reader :view_context
251
-
248
+ # The content passed to the component instance as a block.
249
+ #
250
+ # @return [String]
252
251
def content
253
252
@__vc_content_evaluated = true
254
253
return @__vc_content if defined? ( @__vc_content )
255
254
256
255
@__vc_content =
257
- if @view_context && @__vc_render_in_block
256
+ if __vc_render_in_block_provided?
258
257
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?
260
259
@__vc_content_set_by_with_content
261
260
end
262
261
end
263
262
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
+
264
282
def content_evaluated?
265
283
@__vc_content_evaluated
266
284
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -1038,4 +1038,16 @@ def test_concurrency_deadlock
1038
1038
end
1039
1039
end
1040
1040
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
1041
1053
end
You can’t perform that action at this time.
0 commit comments