From 6fd232df609fca5e7cba8e749e81dda450bd4c91 Mon Sep 17 00:00:00 2001 From: Jared Beck Date: Sun, 6 Mar 2016 14:05:21 -0500 Subject: [PATCH] Optimize post-rollback association reset [Fixes #715] --- CHANGELOG.md | 2 ++ lib/paper_trail/has_paper_trail.rb | 6 ++++-- spec/models/widget_spec.rb | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac7497b7..791e75f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ ### Fixed +- [#715](https://github.com/airblade/paper_trail/issues/715) - + Optimize post-rollback association reset. - [#701](https://github.com/airblade/paper_trail/pull/701) / [#699](https://github.com/airblade/paper_trail/issues/699) - Cleaning old versions explicitly preserves the most recent diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 7e24a9c8..b9ac8b8d 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -195,9 +195,11 @@ module PaperTrail end # Invoked after rollbacks to ensure versions records are not created - # for changes that never actually took place + # for changes that never actually took place. + # Optimization: Use lazy `reset` instead of eager `reload` because, in + # many use cases, the association will not be used. def clear_rolled_back_versions - send(self.class.versions_association_name).reload + send(self.class.versions_association_name).reset end # Returns the object (not a Version) as it was at the given timestamp. diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index 88e90042..c5d64da4 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -110,6 +110,10 @@ describe Widget, type: :model do expect(changeset.fetch("name", [])).to_not include(rolled_back_name) end end + + it "has not yet loaded the assocation" do + expect(widget.versions).to_not be_loaded + end end end