Versioned models should have their version association getter updated upon destruction when applicable

This commit is contained in:
Ben Atkins 2014-04-16 12:17:33 -04:00
parent 5d9b6b7d3c
commit c8c37561be
3 changed files with 18 additions and 1 deletions

View File

@ -6,6 +6,8 @@
for return values from `current_user` method.
- `PaperTrail::Cleaner.clean_versions!` should group versions by `PaperTrail.timestamp_field` when deciding which ones to
keep / destroy, instead of always grouping by the `created_at` field.
- If `destroy` actions are tracked for a versioned model, invoking `destroy` on the model will cause the corresponding version that
gets generated to be assigned into the `model_instance#version_association_name` accessor (usally `model_instance#version`).
## 3.0.1

View File

@ -295,7 +295,7 @@ module PaperTrail
:object => self.class.paper_trail_version_class.object_col_is_json? ? object_attrs : PaperTrail.serializer.dump(object_attrs),
:whodunnit => PaperTrail.whodunnit
}
self.class.paper_trail_version_class.create merge_metadata(data)
send("#{self.class.version_association_name}=", self.class.paper_trail_version_class.create(merge_metadata(data)))
send(self.class.versions_association_name).send :load_target
end
end

View File

@ -21,6 +21,21 @@ describe Widget do
end
end
describe "Callbacks", :versioning => true do
describe :after_destroy do
it "should create a version for that event" do
expect { widget.destroy }.to change(widget.versions, :count).by(1)
end
it "should assign the version into the `versions_association_name`" do
widget.version.should be_nil
widget.destroy
widget.version.should_not be_nil
widget.version.should == widget.versions.last
end
end
end
describe "Methods" do
describe "Instance", :versioning => true do
describe :whodunnit do