18F/identity-idp

View on GitHub
app/components/flash_component.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class FlashComponent < BaseComponent
  VALID_FLASH_TYPES = %w[error info success warning other notice alert].freeze

  attr_reader :flash

  def initialize(flash:)
    @flash = flash
  end

  def alerts
    flash.
      to_hash.
      slice(*VALID_FLASH_TYPES).
      select { |_flash_type, message| message.present? }.
      map { |flash_type, message| [alert_type(flash_type), message] }
  end

  def alert_type(flash_type)
    case flash_type
    when 'notice'
      :info
    when 'alert'
      :error
    else
      flash_type.to_sym
    end
  end
end