mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
fix bugs
This commit is contained in:
parent
e69d68cab9
commit
546c58b975
2 changed files with 20 additions and 14 deletions
|
@ -223,11 +223,11 @@ module PaperTrail
|
||||||
# Utility method for reifying. Anything executed inside the block will appear like a new record
|
# Utility method for reifying. Anything executed inside the block will appear like a new record
|
||||||
def appear_as_new_record
|
def appear_as_new_record
|
||||||
instance_eval {
|
instance_eval {
|
||||||
alias_method :old_new_record?, :new_record?
|
alias :old_new_record? :new_record?
|
||||||
alias_method :new_record?, :present?
|
alias :new_record? :present?
|
||||||
}
|
}
|
||||||
yield
|
yield
|
||||||
instance_eval { alias_method :new_record?, :old_new_record? }
|
instance_eval { alias :new_record? :old_new_record? }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Mimicks behavior of `touch` method from `ActiveRecord::Persistence`, but generates a version
|
# 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 :
|
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)
|
PaperTrail.serializer.dump(changes_for_paper_trail)
|
||||||
end
|
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)
|
set_transaction_id(version)
|
||||||
save_associations(version)
|
save_associations(version)
|
||||||
end
|
end
|
||||||
|
@ -315,7 +315,11 @@ module PaperTrail
|
||||||
|
|
||||||
def save_associations(version)
|
def save_associations(version)
|
||||||
self.class.reflect_on_all_associations(:belongs_to).each do |assoc|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -245,8 +245,8 @@ module PaperTrail
|
||||||
next if assoc.name == model.class.versions_association_name
|
next if assoc.name == model.class.versions_association_name
|
||||||
version_id_subquery = PaperTrail::VersionAssociation.joins(model.class.version_association_name).
|
version_id_subquery = PaperTrail::VersionAssociation.joins(model.class.version_association_name).
|
||||||
select("MIN(version_id)").
|
select("MIN(version_id)").
|
||||||
where(:foreign_key_name => assoc.foreign_key).
|
where("foreign_key_name = ?", assoc.foreign_key).
|
||||||
where(:foreign_key_id => model.id).
|
where("foreign_key_id = ?", model.id).
|
||||||
where("#{version_table_name}.item_type = ?", assoc.class_name).
|
where("#{version_table_name}.item_type = ?", assoc.class_name).
|
||||||
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
where("created_at >= ? OR transaction_id = ?", options[:version_at], transaction_id).
|
||||||
group("item_id").to_sql
|
group("item_id").to_sql
|
||||||
|
@ -257,17 +257,19 @@ module PaperTrail
|
||||||
|
|
||||||
# Iterate all the child records to replace them with the previous values
|
# Iterate all the child records to replace them with the previous values
|
||||||
versions.each do |version|
|
versions.each do |version|
|
||||||
if version.event == 'create'
|
collection << version.reify(options) if version.event == 'destroy'
|
||||||
if child = version.item
|
collection.map! do |c|
|
||||||
collection.delete child
|
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
|
end
|
||||||
else
|
|
||||||
child = version.reify(options)
|
|
||||||
collection.map!{ |c| c.id == child.id ? child : c }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
model.send "#{assoc.name}=", collection
|
model.send(assoc.name).proxy_association.target = collection
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue