Sha256: b2270ff1d0dfa7a8d2fe9b4bc8ecde8659f140d92054311d63d8fbcc17d65754

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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 *args
    end

    def flash!(**flashes)
      flashes.symbolize_keys.each do |type,messages|
        stored_flashes[type] ||= []
        stored_flashes[type] += Array.wrap(messages)
      end
    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
        if !request.xhr?
          stored_flashes.each { |type,messages| flash.now[type] = messages }
        end
      end

      # If request is an ajax request, or we are redirecting, include flash messages
      # in the appropriate outlet, either response headers or `flash` itself
      def include_flash_later
        if request.xhr?
          response.headers[Voltron.config.flash.header] = stored_flashes.to_json
        elsif is_redirecting?
          stored_flashes.each { |type,messages| flash[type] = messages }
        end
      end

      def is_redirecting?
        self.status == 302 || self.status == 301
      end

  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
voltron-flash-0.1.1 lib/voltron/flash.rb