Skip to content

Commit c4e7b3c

Browse files
committed
Default order by timestamp to ensure early versions are cleaned
1 parent a80c23f commit c4e7b3c

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ None
1919

2020
### Fixed
2121

22-
None
22+
- [#701](https://github.com/airblade/paper_trail/pull/701) /
23+
[#699](https://github.com/airblade/paper_trail/issues/699) -
24+
Cleaning old versions explicitly preserves the most recent
25+
versions instead of relying on database result ordering.
2326

2427
## 4.1.0 (Unreleased)
2528

lib/paper_trail/cleaner.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module Cleaner
66
# Options:
77
#
88
# - :keeping - An `integer` indicating the number of versions to be kept for
9-
# each item per date. Defaults to `1`.
9+
# each item per date. Defaults to `1`. The most recent matching versions
10+
# are kept.
1011
# - :date - Should either be a `Date` object specifying which date to
1112
# destroy versions for or `:all`, which will specify that all dates
1213
# should be cleaned. Defaults to `:all`.
@@ -30,12 +31,14 @@ def clean_versions!(options = {})
3031
# Returns a hash of versions grouped by the `item_id` attribute formatted
3132
# like this: {:item_id => PaperTrail::Version}. If `item_id` or `date` is
3233
# set, versions will be narrowed to those pointing at items with those ids
33-
# that were created on specified date.
34+
# that were created on specified date. Versions are returned in
35+
# chronological order.
3436
def gather_versions(item_id = nil, date = :all)
3537
unless date == :all || date.respond_to?(:to_date)
3638
raise ArgumentError.new("`date` argument must receive a Timestamp or `:all`")
3739
end
3840
versions = item_id ? PaperTrail::Version.where(:item_id => item_id) : PaperTrail::Version
41+
versions = versions.order(PaperTrail.timestamp_field, :id)
3942
versions = versions.between(date.to_date, date.to_date + 1.day) unless date == :all
4043

4144
# If `versions` has not been converted to an ActiveRecord::Relation yet,

0 commit comments

Comments
 (0)