Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Loading a new Rails 3.0 session under Rails 2.3 #18

Open
mrship opened this issue Jun 3, 2015 · 0 comments
Open

Loading a new Rails 3.0 session under Rails 2.3 #18

mrship opened this issue Jun 3, 2015 · 0 comments

Comments

@mrship
Copy link

mrship commented Jun 3, 2015

I'm upgrading a Rails 2.3 app to Rails 3.0 and intend to run both frameworks in production for a time to manage the transition. As such, I can have requests served by either 2.3 or 3.0. If I create a new session under Rails 3.0 and then try to read values in the flash under Rails 2.3 I get an error:

undefined method `each' for #<ActionDispatch::Flash::FlashHash:0x007fc58a9113e0>
/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rails_4_session_flash_backport-0.2.0/lib/rails_4_session_flash_backport/rails2/flash_hash.rb:50:in `block in new_from_session'
/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rails_4_session_flash_backport-0.2.0/lib/rails_4_session_flash_backport/rails2/flash_hash.rb:45:in `new_from_session'
/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rails_4_session_flash_backport-0.2.0/lib/rails_4_session_flash_backport/rails2/flash_hash.rb:26:in `from_session_value'
/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/rails_4_session_flash_backport-0.2.0/lib/rails_4_session_flash_backport/rails2/flash_hash.rb:62:in `flash'

To fix that, I modified rails2/flash_hash.rb to test whether what is in the Hash is a Rails 3.0 FlashHash

      def self.new_from_session(flashes)
        new.tap do |flash|
          # Rails 3.0
          if flashes.is_a?(ActionDispatch::Flash::FlashHash)
            flashes = flashes.instance_variable_get(:@self)
          end
          flashes.each do |key, value|
            flash[key] = value
            flash.discard key
          end
        end
      end

This works for my specific use case, but I was unsure as to how to add a test for that scenario? Also, whether there are ramifications elsewhere that I've overlooked.

Happy to PR with a bit of help. :)

Thoughts?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant