Created
December 12, 2013 01:16
-
-
Save adammiller/7921675 to your computer and use it in GitHub Desktop.
If you're sharing sessions between a rails 3.x and 4.x application, this code can be dropped into the rails 4 app to work around the flash message incompatibility.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# We override some fo the Flash code here to ensure our flash messages from this app are written to a different | |
# session key than the default. This prevents conflicts with our older rails applications | |
require "rails" | |
ActionDispatch::Request.class_eval do | |
def flash | |
@env["#{Flash::KEY}_four"] ||= Flash::FlashHash.from_session_value(session['flash_4']) | |
end | |
end | |
ActionDispatch::Flash.class_eval do | |
def call(env) | |
@app.call(env) | |
ensure | |
@key = self.class.const_get(:KEY) | |
session = ActionDispatch::Request::Session.find(env) || {} | |
flash_hash = env[@key] | |
if flash_hash && (flash_hash.present? || session.key?('flash_4')) | |
session['flash_4'] = flash_hash.to_session_value | |
env[@key] = flash_hash.dup | |
end | |
if (!session.respond_to?(:loaded?) || session.loaded?) && # (reset_session uses {}, which doesn't implement #loaded?) | |
session.key?('flash_4') && session['flash_4'].nil? | |
session.delete('flash_4') | |
end | |
end | |
end |
It looks like line 17 should be "#{env[@key]}_four" since that's what the key is on line 7
--- a/config/initializers/session_store.rb
+++ b/config/initializers/session_store.rb
@@ -18,7 +18,7 @@ ActionDispatch::Flash.class_eval do
def call(env)
@app.call(env)
ensure
- @key = self.class.const_get(:KEY)
+ @key = "#{self.class.const_get(:KEY)}_four"
session = ActionDispatch::Request::Session.find(env) || {}
flash_hash = env[@key]
(just FYI)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does the KEY stand for in line 7 and 15 ?