Module: MaquinaComponents::ToastHelper

Defined in:
app/helpers/maquina_components/toast_helper.rb

Overview

Toast Helper

Provides helpers for rendering toast notifications.

Examples:

Render flash messages as toasts

<%= toast_flash_messages %>

Render a single toast

<%= toast :success, "Profile updated!" %>

Toast with description

<%= toast :error, "Save failed", description: "Please check your connection." %>

Toast with action

<%= toast :info, "New version available", content: capture { %>
  <%= render "components/toast/action", label: "Refresh", href: root_path %>
<% } %>

Constant Summary collapse

FLASH_VARIANTS =

Flash type to toast variant mapping

{
  notice: :success,
  success: :success,
  alert: :error,
  error: :error,
  warning: :warning,
  warn: :warning,
  info: :info
}.freeze

Instance Method Summary collapse

Instance Method Details

#toast(variant, title, description: nil, **options) ⇒ String

Render a single toast

Parameters:

  • variant (Symbol)

    Toast variant (:default, :success, :info, :warning, :error)

  • title (String)

    Toast title

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

    Optional description

  • options (Hash)

    Additional options passed to the toast partial

  • content (String, nil)

    HTML content via capture (e.g., action button)

Returns:

  • (String)

    HTML-safe toast element



60
61
62
63
64
65
66
# File 'app/helpers/maquina_components/toast_helper.rb', line 60

def toast(variant, title, description: nil, **options)
  render "components/toast",
    variant: variant,
    title: title,
    description: description,
    **options
end

#toast_error(title, **options) ⇒ String

Render an error toast

Parameters:

  • title (String)

    Toast title

  • options (Hash)

    Additional options

Returns:

  • (String)

    HTML-safe toast element



82
83
84
# File 'app/helpers/maquina_components/toast_helper.rb', line 82

def toast_error(title, **options)
  toast(:error, title, **options)
end

#toast_flash_messages(exclude: []) ⇒ String

Render all flash messages as toasts

Parameters:

  • exclude (Array<Symbol>) (defaults to: [])

    Flash types to exclude

Returns:

  • (String)

    HTML-safe string of toast elements



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/helpers/maquina_components/toast_helper.rb', line 38

def toast_flash_messages(exclude: [])
  return "" if flash.empty?

  toasts = flash.map do |type, message|
    next if exclude.include?(type.to_sym)
    next if message.blank?

    variant = flash_to_variant(type)
    render "components/toast", variant: variant, title: message
  end

  safe_join(toasts.compact)
end

#toast_info(title, **options) ⇒ String

Render an info toast

Parameters:

  • title (String)

    Toast title

  • options (Hash)

    Additional options

Returns:

  • (String)

    HTML-safe toast element



100
101
102
# File 'app/helpers/maquina_components/toast_helper.rb', line 100

def toast_info(title, **options)
  toast(:info, title, **options)
end

#toast_success(title, **options) ⇒ String

Render a success toast

Parameters:

  • title (String)

    Toast title

  • options (Hash)

    Additional options

Returns:

  • (String)

    HTML-safe toast element



73
74
75
# File 'app/helpers/maquina_components/toast_helper.rb', line 73

def toast_success(title, **options)
  toast(:success, title, **options)
end

#toast_warning(title, **options) ⇒ String

Render a warning toast

Parameters:

  • title (String)

    Toast title

  • options (Hash)

    Additional options

Returns:

  • (String)

    HTML-safe toast element



91
92
93
# File 'app/helpers/maquina_components/toast_helper.rb', line 91

def toast_warning(title, **options)
  toast(:warning, title, **options)
end