Module: CacheableFlash

Included in:
Admin::BaseController, BaseController
Defined in:
lib/rails_ext/action_controller/cacheable_flash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/rails_ext/action_controller/cacheable_flash.rb', line 6

def self.included(base)
  base.prepend_around_action :write_flash_to_cookie
end

Instance Method Details

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_ext/action_controller/cacheable_flash.rb', line 10

def write_flash_to_cookie
  yield self

  cookie_flash = begin
    JSON.parse(cookies["flash"] || "{}")
  rescue JSON::ParserError
    {}
  end

  flash.each do |key, value|
    if cookie_flash[key.to_s].blank?
      cookie_flash[key.to_s] = value
    else
      cookie_flash[key.to_s] << "<br/>#{value}" # TODO should be an array
    end
  end

  cookies['flash'] = cookie_flash.to_json
  flash.clear
end