1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
This commit is contained in:
lyfeyaj 2014-02-26 19:44:43 +08:00
parent e69d68cab9
commit 546c58b975
2 changed files with 20 additions and 14 deletions

View file

@ -223,11 +223,11 @@ module PaperTrail
# Utility method for reifying. Anything executed inside the block will appear like a new record
def appear_as_new_record
instance_eval {
alias_method :old_new_record?, :new_record?
alias_method :new_record?, :present?
alias :old_new_record? :new_record?
alias :new_record? :present?
}
yield
instance_eval { alias_method :new_record?, :old_new_record? }
instance_eval { alias :new_record? :old_new_record? }
end
# Mimicks behavior of `touch` method from `ActiveRecord::Persistence`, but generates a version
@ -283,7 +283,7 @@ module PaperTrail
data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail :
PaperTrail.serializer.dump(changes_for_paper_trail)
end
version = send(self.class.versions_association_name).build merge_metadata(data)
version = send(self.class.versions_association_name).create merge_metadata(data)
set_transaction_id(version)
save_associations(version)
end
@ -315,7 +315,11 @@ module PaperTrail
def save_associations(version)
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
PaperTrail::VersionAssociation.create(:version_id => version.id, :foreign_key_name => assoc.foreign_key, :foreign_key_id => self.send(assoc.foreign_key))
PaperTrail::VersionAssociation.create(
:version_id => version.id,
:foreign_key_name => assoc.foreign_key,
:foreign_key_id => self.send(assoc.foreign_key)
)
end
end

View file

@ -245,8 +245,8 @@ module PaperTrail
next if assoc.name == model.class.versions_association_name
version_id_subquery = PaperTrail::VersionAssociation.joins(model.class.version_association_name).
select("MIN(version_id)").
where(:foreign_key_name => assoc.foreign_key).
where(:foreign_key_id => model.id).
where("foreign_key_name = ?", assoc.foreign_key).
where("foreign_key_id = ?", model.id).
where("#{version_table_name}.item_type = ?", assoc.class_name).
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
group("item_id").to_sql
@ -257,17 +257,19 @@ module PaperTrail
# Iterate all the child records to replace them with the previous values
versions.each do |version|
if version.event == 'create'
if child = version.item
collection.delete child
collection << version.reify(options) if version.event == 'destroy'
collection.map! do |c|
if version.event == 'create'
c.mark_for_destruction if version.item && version.item.id == c.id
c
else
child = version.reify(options)
c.id == child.id ? child : c
end
else
child = version.reify(options)
collection.map!{ |c| c.id == child.id ? child : c }
end
end
model.send "#{assoc.name}=", collection
model.send(assoc.name).proxy_association.target = collection
end
end