class ViewComponent::SlotV2
def initialize(parent)
def initialize(parent) @parent = parent end
def method_missing(symbol, *args, &block)
end
end
end
@name
def name
class HeaderComponent < ViewComponent::Base
has_one :header, HeaderComponent
class MyComponent < ViewComponent::Base
Where the component may look like:
on the `HeaderComponent` instance.
calling `header.name` (where `header` is a slot) will call `name`
e.g.
Allow access to public component methods via the wrapper
def method_missing(symbol, *args, &block) @_component_instance.public_send(symbol, *args, &block) end
def respond_to_missing?(symbol, include_all = false)
def respond_to_missing?(symbol, include_all = false) defined?(@_component_instance) && @_component_instance.respond_to?(symbol, include_all) end
def to_s
If there is no slot renderable, we evaluate the block passed to
set as `@_content` and is returned directly.
If the slot renderable is a function and returns a string, it is
component instance, returning the string.
component, or a function that returns a component, we render that
If the slot renderable is a component, the string class name of a
There's currently 3 different values that may be set, that we can render.
Used to render the slot content in the template
def to_s view_context = @parent.send(:view_context) view_context.capture do if defined?(@_component_instance) # render_in is faster than `parent.render` @_component_instance.render_in(view_context, &@_content_block) elsif defined?(@_content) @_content elsif defined?(@_content_block) @_content_block.call end end end