1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Rename method for consistency. Add compatibility check. Whitespace.

This commit is contained in:
Andy Stewart 2011-05-16 11:15:28 +01:00
parent 5df9949ff4
commit 450dfc1c2b
2 changed files with 34 additions and 31 deletions

View file

@ -19,16 +19,6 @@ class Version < ActiveRecord::Base
where(['created_at > ?', timestamp]).order("created_at ASC, #{self.primary_key} ASC")
}
# In Rails 3.1+, calling reify on a previous version confuses the
# IdentityMap, if enabled. This prevents insertion into the map.
def with_identity_map_disabled(&block)
if defined?(ActiveRecord::IdentityMap)
ActiveRecord::IdentityMap.without(&block)
else
block.call
end
end
# Restore the item from this version.
#
# This will automatically restore all :has_one associations as they were "at the time",
@ -41,12 +31,12 @@ class Version < ActiveRecord::Base
# set to a float to change the lookback time (check whether your db supports
# sub-second datetimes if you want them).
def reify(options = {})
with_identity_map_disabled do
without_identity_map do
options.reverse_merge! :has_one => 3
unless object.nil?
attrs = YAML::load object
# Normally a polymorphic belongs_to relationship allows us
# to get the object we belong to by calling, in this case,
# +item+. However this returns nil if +item+ has been
@ -59,7 +49,7 @@ class Version < ActiveRecord::Base
# we belong to is using single table inheritance and the
# +item_type+ will be the base class, not the actual subclass.
# If +type+ is present but empty, the class is the base class.
if item
model = item
else
@ -68,7 +58,7 @@ class Version < ActiveRecord::Base
klass = class_name.constantize
model = klass.new
end
attrs.each do |k, v|
begin
model.send :write_attribute, k.to_sym , v
@ -76,16 +66,16 @@ class Version < ActiveRecord::Base
logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
end
end
model.version = self
unless options[:has_one] == false
reify_has_ones model, options[:has_one]
end
model
end
end
end
end
# Returns who put the item into the state stored in this version.
@ -117,6 +107,16 @@ class Version < ActiveRecord::Base
private
# In Rails 3.1+, calling reify on a previous version confuses the
# IdentityMap, if enabled. This prevents insertion into the map.
def without_identity_map(&block)
if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
ActiveRecord::IdentityMap.without(&block)
else
block.call
end
end
# Restore the `model`'s has_one associations as they were when this version was
# superseded by the next (because that's what the user was looking at when they
# made the change).

View file

@ -96,19 +96,22 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
should 'have versions that are not live' do
assert @widget.versions.map(&:reify).compact.all? { |w| !w.live? }
end
should 'not clobber the IdentityMap when reifying' do
module ::ActiveRecord
class IdentityMap
def self.without(&block)
@unclobbered = true
block.call
end
end
end
@widget.versions.last.reify
assert ActiveRecord::IdentityMap.instance_variable_get("@unclobbered")
if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without)
should 'not clobber the IdentityMap when reifying' do
module ActiveRecord::IdentityMap
class << self
alias :__without :without
def without(&block)
@unclobbered = true
__without(&block)
end
end
end
@widget.versions.last.reify
assert ActiveRecord::IdentityMap.instance_variable_get("@unclobbered")
end
end
context 'and has one associated object' do