Class: ViewComponent::SlotV2
- Inherits:
-
Object
- Object
- ViewComponent::SlotV2
- Defined in:
- lib/view_component/slot_v2.rb
Instance Attribute Summary collapse
-
#_component_instance ⇒ Object
writeonly
Sets the attribute _component_instance.
-
#_content ⇒ Object
writeonly
Sets the attribute _content.
-
#_content_block ⇒ Object
writeonly
Sets the attribute _content_block.
Instance Method Summary collapse
- #html_safe? ⇒ Boolean
-
#initialize(parent) ⇒ SlotV2
constructor
A new instance of SlotV2.
-
#method_missing(symbol, *args, &block) ⇒ Object
Allow access to public component methods via the wrapper.
- #respond_to_missing?(symbol, include_all = false) ⇒ Boolean
-
#to_s ⇒ Object
Used to render the slot content in the template.
Constructor Details
#initialize(parent) ⇒ SlotV2
Returns a new instance of SlotV2.
7 8 9 |
# File 'lib/view_component/slot_v2.rb', line 7 def initialize(parent) @parent = parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
Allow access to public component methods via the wrapper
e.g.
calling ‘header.name` (where `header` is a slot) will call `name` on the `HeaderComponent` instance.
Where the component may look like:
class MyComponent < ViewComponent::Base
has_one :header, HeaderComponent
class HeaderComponent < ViewComponent::Base
def name
@name
end
end
end
68 69 70 |
# File 'lib/view_component/slot_v2.rb', line 68 def method_missing(symbol, *args, &block) @_component_instance.public_send(symbol, *args, &block) end |
Instance Attribute Details
#_component_instance=(value) ⇒ Object (writeonly)
Sets the attribute _component_instance
5 6 7 |
# File 'lib/view_component/slot_v2.rb', line 5 def _component_instance=(value) @_component_instance = value end |
#_content=(value) ⇒ Object (writeonly)
Sets the attribute _content
5 6 7 |
# File 'lib/view_component/slot_v2.rb', line 5 def _content=(value) @_content = value end |
#_content_block=(value) ⇒ Object (writeonly)
Sets the attribute _content_block
5 6 7 |
# File 'lib/view_component/slot_v2.rb', line 5 def _content_block=(value) @_content_block = value end |
Instance Method Details
#html_safe? ⇒ Boolean
72 73 74 |
# File 'lib/view_component/slot_v2.rb', line 72 def html_safe? to_s.html_safe? end |
#respond_to_missing?(symbol, include_all = false) ⇒ Boolean
76 77 78 |
# File 'lib/view_component/slot_v2.rb', line 76 def respond_to_missing?(symbol, include_all = false) defined?(@_component_instance) && @_component_instance.respond_to?(symbol, include_all) end |
#to_s ⇒ Object
Used to render the slot content in the template
There’s currently 3 different values that may be set, that we can render.
If the slot renderable is a component, the string class name of a component, or a function that returns a component, we render that component instance, returning the string.
If the slot renderable is a function and returns a string, it is set as ‘@_content` and is returned directly.
If there is no slot renderable, we evaluate the block passed to the slot and return it.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/view_component/slot_v2.rb', line 24 def to_s return @content if defined?(@content) view_context = @parent.send(:view_context) @content = if defined?(@_component_instance) # render_in is faster than `parent.render` if defined?(@_content_block) view_context.capture do @_component_instance.render_in(view_context, &@_content_block) end else view_context.capture do @_component_instance.render_in(view_context) end end elsif defined?(@_content) @_content elsif defined?(@_content_block) view_context.capture(&@_content_block) end @content end |