-
Notifications
You must be signed in to change notification settings - Fork 38
Description
I ran into a problem while trying out this branch of paper_trail (and a couple related ones): paper-trail-gem/paper_trail#1143. (It's not in master yet, but it could end up there soon)
When using store_base_sti_class, you can't find PaperTrail "destroy" versions through the versions association because store_base_sti_class causes the has_many :versions association to look for version records where(item_type: subclass) instead of the Rails default of where(item_type: base_class).
Non-destroy versions still work as expected with store_base_sti_class, since those get created through a standard has_many, which means item_type gets set to the subclass due to store_base_sti_class:
lib/paper_trail/record_trail.rb
=> 129: versions_assoc = @record.send(@record.class.versions_association_name)
130: version = versions_assoc.create(data)
The problem is that when paper_trail creates "destroy" versions,,it doesn't go through the versions association — it hard-codes it to always store the base_class in item_type — such that the ActiveRecord hacks that store_base_sti_class provides have no effect.
So if you do this:
record.destroy
record.versions.last.event
you'll find that the destroy event is not there!
Here's the code used for creating destroy versions:
lib/paper_trail/events/destroy.rb
12: # Return attributes of nascent `Version` record.
13: #
14: # @api private
15: def data
=> 16: data = {
17: item_id: @record.id,
18: item_type: @record.class.base_class.name,
lib/paper_trail/record_trail.rb
99: data = event.data.merge(data_for_destroy)
100:
=> 101: version = @record.class.paper_trail.version_class.create(data)
You can see it doesn't go through versions_assoc like it does for update events. It just calls Version.create directly.