byroot/frozen_record — pull request #4 by kmcphillips (2014-07-25 18:26:02+00:00)
Title: Fix object reloading bug

@byroot @camilo

I found two issues causing code reloading to fail in dev mode that were introduced in 0.1.1 in: https://github.com/byroot/frozen_record/commit/b5727d16b855231c7503d76de345be6187597b89

The first was that the instance of `store` being a `ThreadSafeStorage` was not correctly memoized. The side effect of this is primarily that we are building a new instance of this object in production about a dozen times each request in production right now, if not more.

The other is that instances of the class are being stored on `Thread.current`. Which is fine. But in dev mode in Rails when a ruby file is modified rails calls undefs and loads the class and builds a new one. But since we would on next request retrieve the memozied instance of the class from `Thread.current` we would end up with an instance of a class that no longer existed.

The specific manifestation we were seeing of this issue was that when one of our models that inherrits from `FrozenRecord::Base` called `super` inside a method it was looking for a class that no-longer exists.

The fix is straightforward. It keys by the `ThreadSafeStorage#object_id` and the name of the class. This leaks a bit, but only in dev mode. Anything we do in prod will dump the current thread context.

I struggled to write a test for this but ultimately decided it was too internal. All other tests continue to work.