Last active
September 1, 2016 20:28
-
-
Save raj454raj/26da384669fa4bd472e86702bc2f185d to your computer and use it in GitHub Desktop.
FlashHash override to handle both kinds of flashes i.e created in Rails 3.0.20 as well as in Rails 3.2.22. Put this config/initializers/. Kept with Rails 3.2.22 code.
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
# Supports both kind of flash messages (Rails 3.0.20 and 3.2.22) | |
# It converts Hash type of flash messages to Object type of flashes | |
ActiveRecord::SessionStore::Session.class_eval do | |
def self.convert_to_3_2(flash_data) | |
return unless flash_data | |
flash_new = ActionDispatch::Flash::FlashHash.new | |
used = flash_data.instance_variable_get('@used') | |
flash_new.instance_variable_set('@used', used) if used | |
flash_new.instance_variable_set('@flashes', Hash.new.merge(flash_data)) | |
flash_new | |
end | |
def self.unmarshal(data) | |
return unless data | |
marshalled = nil | |
begin | |
decoded = ::Base64.decode64 data | |
marshalled = Marshal.load decoded | |
rescue ArgumentError => ae | |
if ae.message =~ /dump format error/ | |
with_alternate_flash_klass do | |
marshalled = Marshal.load decoded | |
end | |
marshalled['flash'] = convert_to_3_2(marshalled['flash']) if marshalled.key?('flash') | |
else | |
raise ae | |
end | |
end | |
marshalled | |
end | |
def self.alternate_flash_klass | |
if ActionDispatch::Flash::FlashHash.ancestors.include? Hash | |
Class.new() do # Rails 3.2.22 FlashHash | |
def initialize | |
@used = Set.new | |
@closed = false | |
@flashes = {} | |
@now = nil | |
end | |
end | |
else | |
Class.new(Hash) do # Rails 3.0.20 FlashHash | |
def initialize | |
super | |
@used = Set.new | |
end | |
end | |
end | |
end | |
def self.with_alternate_flash_klass | |
temporary_flash_hash_klass = alternate_flash_klass | |
original_flash_hash_klass = ActionDispatch::Flash::FlashHash | |
ActionDispatch::Flash.send :remove_const, :FlashHash | |
ActionDispatch::Flash.const_set 'FlashHash', temporary_flash_hash_klass | |
yield | |
ActionDispatch::Flash.send :remove_const, :FlashHash | |
ActionDispatch::Flash.const_set 'FlashHash', original_flash_hash_klass | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment