Sha256: 7a676b038d93790f5baa60badaaf75e2cdb1daea3b8b789ea9d242c82ad1869f

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require 'json'
require 'stackable_flash'

module CacheableFlash
  if defined?(Rails) && (::Rails::VERSION::MAJOR >= 3)
    require 'cacheable_flash/middleware'
    require 'cacheable_flash/engine' if (::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR >= 1) || ::Rails::VERSION::MAJOR > 3
    require 'cacheable_flash/railtie'
  else
    # For older rails use generator
  end

  # By default stacking is false, but it can be turned on with:
  # CacheableFlash.configure do |config|
  #   config[:stacking] = true
  #   config[:append_as] = :br # Pick a value, set your own proc, or don't: passes the JSON'd array to the cookie
  # end
  StackableFlash.stacking = false

  # The configure will override the above defaults
  require 'cacheable_flash/config'
  require 'cacheable_flash/cookie_flash'
  include CacheableFlash::CookieFlash

  def self.included(base)
    #base must define around_action or around_filter, as in Rails

    around_method = if base.respond_to?(:around_action)
      :around_action
    else
      :around_filter
    end

    base.send around_method, :write_flash_to_cookie
  end

  def write_flash_to_cookie
    yield if block_given?


    # Base must define cookies, as in Rails
    domain = CacheableFlash::Config.config[:domain]
    cookies['flash'] = { :value => cookie_flash(flash, cookies), :domain => domain }
    # Base must define flash, as in Rails
    # TODO: Does not support flash.now feature of the FlashHash in Rails,
    #       because flashes are only removed from cookies when they are used.
    flash.clear
  end

  # simply abstracts the StackableFlash.stacking method
  def self.stacking
    StackableFlash.stacking
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cacheable_flash-1.0.0 lib/cacheable_flash.rb