Skip to content

Commit

Permalink
Don't cache serialized attributes
Browse files Browse the repository at this point in the history
Without this fix, when a serialized attribute is accessed after destroy
or revive, the Serialization::Attribute wrapper is returned instead of
the actual value (rails 4.0/4.1).
  • Loading branch information
mherold committed Mar 5, 2015
1 parent 7e6341c commit 0ae3f25
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions lib/permanent_records.rb
Expand Up @@ -60,16 +60,15 @@ def set_deleted_at(value, force = nil)
record.save!
end
if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) < ::Gem::Version.new('4.2.0')
@attributes, @attributes_cache = record.attributes, record.attributes
@attributes = record.attributes
@attributes_cache = record.attributes.except(record.class.serialized_attributes.keys)
# workaround for active_record >= 3.2.0: re-wrap values of serialized attributes
# (record.attributes returns the plain values but in the instance variables they are expected to be wrapped)
if defined?(::ActiveRecord::AttributeMethods::Serialization::Attribute)
serialized_attribute_class = ::ActiveRecord::AttributeMethods::Serialization::Attribute
self.class.serialized_attributes.each do |key, coder|
if @attributes.key?(key)
attr = serialized_attribute_class.new(coder, @attributes[key], :unserialized)
@attributes[key] = attr
@attributes_cache[key] = attr
@attributes[key] = serialized_attribute_class.new(coder, @attributes[key], :unserialized)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/permanent_records_spec.rb
Expand Up @@ -5,7 +5,7 @@
let!(:frozen_moment) { Time.now }
let!(:dirt) { Dirt.create! }
let!(:earthworm) { dirt.create_earthworm }
let!(:hole) { dirt.create_hole }
let!(:hole) { dirt.create_hole(:options => {}) }
let!(:muskrat) { hole.muskrats.create! }
let!(:mole) { hole.moles.create! }
let!(:location) { hole.create_location }
Expand Down Expand Up @@ -38,8 +38,8 @@
end

it 'handles serialized attributes correctly' do
expect { subject.options }.to_not raise_error
expect { subject.size }.to_not raise_error if record.respond_to?(:size)
expect(subject.options).to eq({})
expect(subject.size).to be_nil if record.respond_to?(:size)
end

context 'with force argument set to truthy' do
Expand Down

0 comments on commit 0ae3f25

Please sign in to comment.