Class: CacheableFlash::Middleware

Inherits:
Object
  • Object
show all
Includes:
CookieFlash
Defined in:
lib/cacheable_flash/middleware.rb

Constant Summary collapse

FLASH_HASH_KEY =
"action_dispatch.request.flash_hash".freeze

Instance Method Summary collapse

Methods included from CookieFlash

#cookie_flash

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
# File 'lib/cacheable_flash/middleware.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Cookies stick in the flash until rendered (cleared out of the cookie by javascript),

to ensure they are seen and not lost, so we grab them from the rails flash hash, or the request cookies


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cacheable_flash/middleware.rb', line 13

def call(env)
  status, headers, body = @app.call(env)
  env_flash = env[FLASH_HASH_KEY]

  if env_flash
    # There is a flash set from this request, merge it into the cookie flash (which may already be populated)
    response = Rack::Response.new(body, status, headers)
    cookies = env["rack.cookies"] || {}
    response.set_cookie("flash", { :value => cookie_flash(env_flash, cookies), :path => "/" })
    response.finish
  else
    # There is no flash set in this request but there are leftovers from previous requests still in the cookie
    request = ActionDispatch::Request.new(env)
    cflash = request.respond_to?(:cookie_jar) ?
      request.cookie_jar['flash'] :
      nil
    if cflash
      response = Rack::Response.new(body, status, headers)
      response.set_cookie("flash", { :value => cflash, :path => "/" })
      response.finish
    else
      response = body
    end
  end
  [status, headers, response]
end