Class: ViewComponentContrib::WrapperComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/view_component_contrib/wrapper_component.rb

Overview

WrapperComponent allwows to wrap any component with a custom HTML code. The whole wrapper is only rendered when the child component.render? returns true. Thus, wrapper could be used to conditionally render the outer html for components without conditionals in templates.

Defined Under Namespace

Classes: DoubleRenderError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ WrapperComponent

Returns a new instance of WrapperComponent.



19
20
21
# File 'lib/view_component_contrib/wrapper_component.rb', line 19

def initialize(component)
  @component_instance = component
end

Instance Attribute Details

#component_instanceObject (readonly)

Returns the value of attribute component_instance.



15
16
17
# File 'lib/view_component_contrib/wrapper_component.rb', line 15

def component_instance
  @component_instance
end

Instance Method Details

#callObject

Simply return the contents of the block passed to #render_component. (Alias couldn’t be used here ‘cause ViewComponent check for the method presence when choosing between #call and a template.)



26
27
28
# File 'lib/view_component_contrib/wrapper_component.rb', line 26

def call
  content
end

#componentObject

Returns rendered child component The name component is chosen for convienent usage in templates, so we can simply call ‘= wrapper.component` in the place where we’re going to put the component

Raises:



34
35
36
37
38
# File 'lib/view_component_contrib/wrapper_component.rb', line 34

def component
  raise DoubleRenderError, component_instance if @rendered

  @rendered = component_instance.render_in(view_context).html_safe
end