Simplifying the :reify method a bit on the VersionConcern by removing one level of nested blocks

This commit is contained in:
Ben Atkins 2013-11-01 18:15:46 -04:00
parent 6f23beee9f
commit 7c9f9b8953
1 changed files with 43 additions and 43 deletions

View File

@ -71,56 +71,56 @@ module PaperTrail
# set to a float to change the lookback time (check whether your db supports # set to a float to change the lookback time (check whether your db supports
# sub-second datetimes if you want them). # sub-second datetimes if you want them).
def reify(options = {}) def reify(options = {})
return nil if object.nil?
without_identity_map do without_identity_map do
options[:has_one] = 3 if options[:has_one] == true options[:has_one] = 3 if options[:has_one] == true
options.reverse_merge! :has_one => false options.reverse_merge! :has_one => false
unless object.nil? attrs = self.class.object_col_is_json? ? object : PaperTrail.serializer.load(object)
attrs = self.class.object_col_is_json? ? object : PaperTrail.serializer.load(object)
# Normally a polymorphic belongs_to relationship allows us # Normally a polymorphic belongs_to relationship allows us
# to get the object we belong to by calling, in this case, # to get the object we belong to by calling, in this case,
# `item`. However this returns nil if `item` has been # `item`. However this returns nil if `item` has been
# destroyed, and we need to be able to retrieve destroyed # destroyed, and we need to be able to retrieve destroyed
# objects. # objects.
# #
# In this situation we constantize the `item_type` to get hold of # In this situation we constantize the `item_type` to get hold of
# the class...except when the stored object's attributes # the class...except when the stored object's attributes
# include a `type` key. If this is the case, the object # include a `type` key. If this is the case, the object
# we belong to is using single table inheritance and the # we belong to is using single table inheritance and the
# `item_type` will be the base class, not the actual subclass. # `item_type` will be the base class, not the actual subclass.
# If `type` is present but empty, the class is the base class. # If `type` is present but empty, the class is the base class.
if item if item
model = item model = item
# Look for attributes that exist in the model and not in this version. These attributes should be set to nil. # Look for attributes that exist in the model and not in this version. These attributes should be set to nil.
(model.attribute_names - attrs.keys).each { |k| attrs[k] = nil } (model.attribute_names - attrs.keys).each { |k| attrs[k] = nil }
else else
inheritance_column_name = item_type.constantize.inheritance_column inheritance_column_name = item_type.constantize.inheritance_column
class_name = attrs[inheritance_column_name].blank? ? item_type : attrs[inheritance_column_name] class_name = attrs[inheritance_column_name].blank? ? item_type : attrs[inheritance_column_name]
klass = class_name.constantize klass = class_name.constantize
model = klass.new model = klass.new
end
model.class.unserialize_attributes_for_paper_trail attrs
# Set all the attributes in this version on the model
attrs.each do |k, v|
if model.respond_to?("#{k}=")
model[k.to_sym] = v
else
logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
end
end
model.send "#{model.class.version_association_name}=", self
unless options[:has_one] == false
reify_has_ones model, options[:has_one]
end
model
end end
model.class.unserialize_attributes_for_paper_trail attrs
# Set all the attributes in this version on the model
attrs.each do |k, v|
if model.respond_to?("#{k}=")
model[k.to_sym] = v
else
logger.warn "Attribute #{k} does not exist on #{item_type} (Version id: #{id})."
end
end
model.send "#{model.class.version_association_name}=", self
unless options[:has_one] == false
reify_has_ones model, options[:has_one]
end
model
end end
end end