Class: OkonomiUiKit::Components::SectionBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::CaptureHelper, ActionView::Helpers::TagHelper
Defined in:
app/helpers/okonomi_ui_kit/components/segment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, component, variant = :default) ⇒ SectionBuilder

Returns a new instance of SectionBuilder.



41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 41

def initialize(template, component, variant = :default)
  @template = template
  @component = component
  @title_content = nil
  @subtitle_content = nil
  @actions_content = nil
  @body_content = nil
  @attributes = []
  @variant = variant
end

Instance Attribute Details

#component ⇒ Object (readonly)

Returns the value of attribute component.



39
40
41
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 39

def component
  @component
end

#variant ⇒ Object (readonly)

Returns the value of attribute variant.



39
40
41
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 39

def variant
  @variant
end

#view ⇒ Object (readonly)

Returns the value of attribute view.



39
40
41
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 39

def view
  @view
end

Instance Method Details

#actions(&block) ⇒ Object



60
61
62
63
64
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 60

def actions(&block)
  @actions_content = tag.div(class: @component.variant_style(variant, :actions)) do
    capture(&block) if block_given?
  end
end

#body(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 66

def body(&block)
  if block_given?
    # Capture the content first to see if attributes were used
    content = capture { yield(self) }

    @body_content = tag.div class: @component.variant_style(variant, :body) do
      content
    end
  end
end

#render_content ⇒ Object



101
102
103
104
105
106
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 101

def render_content
  content_parts = []
  content_parts << render_header
  content_parts << @body_content if @body_content
  @template.safe_join(content_parts.compact)
end

#render_header ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 77

def render_header
  return nil unless @title_content || @subtitle_content || @actions_content

  tag.div(class: @component.variant_style(variant, :header)) do
    if @actions_content
      tag.div(class: @component.variant_style(variant, :header_with_actions)) do
        title_section = tag.div do
          content_parts = []
          content_parts << @title_content if @title_content
          content_parts << @subtitle_content if @subtitle_content
          @template.safe_join(content_parts.compact)
        end

        title_section + @actions_content
      end
    else
      content_parts = []
      content_parts << @title_content if @title_content
      content_parts << @subtitle_content if @subtitle_content
      @template.safe_join(content_parts.compact)
    end
  end
end

#subtitle(text, **options) ⇒ Object



56
57
58
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 56

def subtitle(text, **options)
  @subtitle_content = tag.p(text, class: @component.variant_style(variant, :subtitle))
end

#title(text, **options) ⇒ Object



52
53
54
# File 'app/helpers/okonomi_ui_kit/components/segment.rb', line 52

def title(text, **options)
  @title_content = tag.h3(text, class: @component.variant_style(variant, :title))
end