Class: MaquinaComponents::DropdownMenuHelper::DropdownMenuContentBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/maquina_components/dropdown_menu_helper.rb

Overview

Builder for dropdown menu content

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ DropdownMenuContentBuilder

Returns a new instance of DropdownMenuContentBuilder.



144
145
146
147
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 144

def initialize(view_context)
  @view = view_context
  @parts = []
end

Instance Method Details

#group(**options) { ... } ⇒ Object

Adds a group

Parameters:

  • options (Hash)

    Additional options

Yields:

  • Block containing group items



208
209
210
211
212
213
214
215
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 208

def group(**options, &block)
  group_builder = DropdownMenuContentBuilder.new(@view)
  @view.capture(group_builder, &block)

  @parts << @view.render("components/dropdown_menu/group", **options) do
    group_builder.to_html
  end
end

#item(label = nil, href: nil, method: nil, icon: nil, variant: :default, disabled: false, **options) { ... } ⇒ Object

Adds a menu item

Parameters:

  • label (String, nil) (defaults to: nil)

    Item label (alternative to block)

  • href (String, nil) (defaults to: nil)

    URL for the item

  • method (Symbol, nil) (defaults to: nil)

    HTTP method

  • icon (Symbol, nil) (defaults to: nil)

    Icon name

  • variant (Symbol) (defaults to: :default)

    Visual variant

  • disabled (Boolean) (defaults to: false)

    Whether disabled

  • options (Hash)

    Additional options

Yields:

  • Optional block for custom content or item builder



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 159

def item(label = nil, href: nil, method: nil, icon: nil, variant: :default, disabled: false, **options, &block)
  item_builder = DropdownMenuItemBuilder.new(@view)

  content = if block
    @view.capture(item_builder, &block)
  elsif label
    build_item_content(label, icon)
  end

  # Append shortcut if defined via builder
  content = @view.safe_join([content, item_builder.shortcut_html].compact) if item_builder.shortcut_html

  @parts << @view.render(
    "components/dropdown_menu/item",
    href: href,
    method: method,
    variant: variant,
    disabled: disabled,
    **options
  ) { content }
end

#label(text = nil, inset: false, **options) { ... } ⇒ Object

Adds a label/heading

Parameters:

  • text (String, nil) (defaults to: nil)

    Label text

  • inset (Boolean) (defaults to: false)

    Whether to indent

  • options (Hash)

    Additional options

Yields:

  • Optional block for custom content



187
188
189
190
191
192
193
194
195
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 187

def label(text = nil, inset: false, **options, &block)
  @parts << @view.render(
    "components/dropdown_menu/label",
    text: text,
    inset: inset,
    **options,
    &block
  )
end

#separator(**options) ⇒ Object

Adds a separator

Parameters:

  • options (Hash)

    Additional options



200
201
202
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 200

def separator(**options)
  @parts << @view.render("components/dropdown_menu/separator", **options)
end

#to_htmlObject

Generates the final HTML



218
219
220
# File 'app/helpers/maquina_components/dropdown_menu_helper.rb', line 218

def to_html
  @view.safe_join(@parts)
end