Sha256: 7ef39600c84f4239581929db3ff4ca729004bf47cfde6416df145466d125d52a

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

require 'voltron'
require 'voltron/flash/version'
require 'voltron/config/flash'

module Voltron
  module Flash

    def self.prepended(base)
      base.send :after_action, :include_flash_later
    end

    def render(*args)
      include_flash_now
      super
    end

    def redirect_to(options={}, response_status={})
      include_flash_later
      super
    end

    def flash!(**flashes)
      flashes.symbolize_keys.each do |type,messages|
        stored_flashes[type] ||= []
        stored_flashes[type] += Array.wrap(messages)
      end

      # Set the headers initially. If redirecting, they will be removed as the flash will instead be a part of `flash`
      response.headers[Voltron.config.flash.header] = stored_flashes.to_json
    end

    private

      def stored_flashes
        @stored_flashes ||= {}
      end

      # Before rendering, include any flash messages in flash.now,
      # so they will be available when the page is rendered
      def include_flash_now
        stored_flashes.each { |type,messages| flash.now[type] = messages }
      end

      # When redirecting, remove the flash from the headers (unless ajax request), append it all to the `flash` object
      def include_flash_later
        unless request.xhr?
          response.headers.except! Voltron.config.flash.header
          stored_flashes.each { |type,messages| flash[type] = messages }
        end
      end

  end
end

require 'voltron/flash/engine' if defined?(Rails)

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
voltron-flash-0.1.6 lib/voltron/flash.rb
voltron-flash-0.1.8 lib/voltron/flash.rb
voltron-flash-0.1.5 lib/voltron/flash.rb
voltron-flash-0.1.7 lib/voltron/flash.rb