Class: NitroKit::Toast::FlashMessages

Inherits:
Toast
  • Object
show all
Defined in:
app/components/nitro_kit/toast.rb

Constant Summary collapse

VARIANT_BY_SEVERITY =
{
  "alert" => :error,
  "error" => :error,
  "warning" => :warning,
  "success" => :success,
  "info" => :info,
  "notice" => :default
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flash:, duration: 5_000, label: I18n.t("nitro_kit.toast.label"), id: DEFAULT_ID, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ FlashMessages

Returns a new instance of FlashMessages.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/components/nitro_kit/toast.rb', line 109

def initialize(
  flash:,
  duration: 5_000,
  label: I18n.t("nitro_kit.toast.label"),
  id: DEFAULT_ID,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  unless flash.respond_to?(:each)
    raise ArgumentError, "Toast::FlashMessages flash must be enumerable"
  end

  @flash = flash

  super(
    duration:,
    label:,
    id:,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#flashObject (readonly)

Returns the value of attribute flash.



136
137
138
# File 'app/components/nitro_kit/toast.rb', line 136

def flash
  @flash
end

Instance Method Details

#view_templateObject



138
139
140
141
142
143
144
145
146
147
# File 'app/components/nitro_kit/toast.rb', line 138

def view_template
  super do |toast|
    flash.each do |severity, message|
      toast.item(
        description: message.to_s,
        variant: VARIANT_BY_SEVERITY.fetch(severity.to_s, :default)
      )
    end
  end
end