Class: Yattho::ConditionalWrapper

Inherits:
Component
  • Object
show all
Defined in:
app/components/yattho/conditional_wrapper.rb

Overview

Conditionally renders a ‘Yattho::BaseComponent` around the given content. If the given condition is true, a `Yattho::BaseComponent` will render around the content. If the condition is false, only the content is rendered.

Examples:

True conditional

<%# condition is true, so content will be wrapped in a <span> tag
<%= render Yattho::ConditionalWrapper.new(condition: true, tag: :span, class: "foobar")) do %>
  <%# also rendered %>
  <p class="bazboo">Some text</p>
<% end %>

False conditional

<%# condition is false so no <span> tag will render around the content (i.e. the <p> tag)
<%= render(Yattho::ConditionalWrapper.new(condition: false, tag: :span, class: "foobar")) do %>
  <%# this content will be rendered %>
  <p class="bazboo">Some text</p>
<% end %>

Constant Summary

Constants inherited from Component

Yattho::Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Constants included from FetchOrFallbackHelper

FetchOrFallbackHelper::InvalidValueError

Instance Method Summary collapse

Methods inherited from Component

deprecated?, generate_id

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from FetchOrFallbackHelper

#fetch_or_fallback, #fetch_or_fallback_boolean, #silence_deprecations?

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(condition:, **base_component_arguments) ⇒ ConditionalWrapper

Returns a new instance of ConditionalWrapper.



25
26
27
28
# File 'app/components/yattho/conditional_wrapper.rb', line 25

def initialize(condition:, **base_component_arguments)
  @condition = condition
  @base_component_arguments = base_component_arguments
end

Instance Method Details

#callObject



30
31
32
33
34
# File 'app/components/yattho/conditional_wrapper.rb', line 30

def call
  return content unless @condition

  BaseComponent.new(**@base_component_arguments).render_in(self) { content }
end