Sha256: 460bdfcf466cfcc79e0249bef513dc86749d29f5c19183d033dcdb47da9256c3

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

class BootstrapBuilders::Flash
  def initialize(args)
    @class = args[:class]
    @alert_types = [:success, :info, :warning, :danger]
    @context = args.fetch(:context)
  end

  def html
    flash_messages = []
    @context.flash.each do |type, message|
      # Skip empty messages, e.g. for devise messages set to nothing in a locale file.
      next if message.blank?

      type = type.to_sym
      type = :success if type == :notice
      type = :danger  if type == :alert
      type = :danger  if type == :error
      next unless @alert_types.include?(type)

      close_button = @context.content_tag(:button, @context.raw("×"), type: "button", class: "close", "data-dismiss" => "alert")

      Array(message).each do |msg|
        text = @context.content_tag(:div, close_button + msg, class: classes(type))
        flash_messages << text if msg
      end
    end
    @context.flash.clear
    flash_messages.join("\n").html_safe # rubocop:disable Rails/OutputSafety
  end

private

  def classes(type)
    classes = []

    if @class.is_a?(String)
      classes += @class.split(/\s+/)
    elsif @class.is_a?(Array)
      classes += @class
    end

    classes += ["bb-flash", "alert", "alert-#{type}"]
    classes
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bootstrap_builders-1.0.4 lib/bootstrap_builders/flash.rb
bootstrap_builders-0.0.63 lib/bootstrap_builders/flash.rb
bootstrap_builders-1.0.3 lib/bootstrap_builders/flash.rb
bootstrap_builders-1.0.2 lib/bootstrap_builders/flash.rb
bootstrap_builders-1.0.1 lib/bootstrap_builders/flash.rb
bootstrap_builders-1.0.0 lib/bootstrap_builders/flash.rb
bootstrap_builders-0.0.62 lib/bootstrap_builders/flash.rb