Module: CacheableFlash

Defined in:
lib/cacheable_flash/railtie.rb,
lib/cacheable_flash.rb,
lib/cacheable_flash/engine.rb,
lib/cacheable_flash/version.rb,
lib/generators/cacheable_flash/install/install_generator.rb

Overview

Add cacheable flash JS to defaults for Rails < 3.1 (not needed with asset pipeline)

Defined Under Namespace

Classes: Engine, InstallGenerator, Railtie

Constant Summary collapse

VERSION =
"0.2.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
# File 'lib/cacheable_flash.rb', line 15

def self.included(base)
  #base must define around_filter, as in Rails
  base.around_filter :write_flash_to_cookie
end

Instance Method Details



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cacheable_flash.rb', line 20

def write_flash_to_cookie
  yield if block_given?
  cookie_flash = if cookies['flash']
    begin
      JSON(cookies['flash'])
    rescue
      {}
    end
  else
    {}
  end

  flash.each do |key, value|
    if cookie_flash[key.to_s].blank?
      cookie_flash[key.to_s] = value.kind_of?(Numeric) ? value.to_s : value
    else
      cookie_flash[key.to_s] << "<br/>#{value}"
    end
  end
  # Base must define cookies, as in Rails
  cookies['flash'] = cookie_flash.to_json.gsub("+", "%2B")
  # Base must define flash, as in Rails
  flash.clear
end