Sha256: e9257627c0b2023dc34d182d2c1386d22a29b48325bc592bb17a74baacc2cf75

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

module FlashRailsMessages
  class Base
    include ActionView::Context
    include ActionView::Helpers::TagHelper

    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    def render(flash)
      flash = Hash[flash].symbolize_keys
      flash.map { |message| alert_element(*message) }.join.html_safe
    end

    private

    def alert_element(type, message)
      content_tag :div, alert_options(type) do
        content = ActiveSupport::SafeBuffer.new
        content += close_element if options.fetch(:dismissible, false)
        content += message.html_safe
        content
      end
    end

    def close_element
      content_tag :a, class: 'close', href: '#' do
        '×'.html_safe
      end
    end

    def alert_options(type)
      default_alert_options
        .merge(options.except(:dismissible))
        .merge(class: alert_classes(type))
    end

    def alert_classes(type)
      [
        default_alert_classes,
        alert_type_classes[type],
        custom_alert_classes,
        options[:class]
      ].compact.join(' ').strip
    end

    def default_alert_options
      {}
    end

    def default_alert_classes
      'alert'
    end

    def alert_type_classes
      {}
    end

    def custom_alert_classes
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_rails_messages-2.2.0 lib/flash_rails_messages/base.rb
flash_rails_messages-2.1.0 lib/flash_rails_messages/base.rb