Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default order by timestamp to ensure early versions are cleaned #701

Merged
merged 1 commit into from
Jan 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ None

### Fixed

None
- [#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
versions instead of relying on database result ordering.

## 4.1.0 (Unreleased)

Expand Down
7 changes: 5 additions & 2 deletions lib/paper_trail/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Cleaner
# Options:
#
# - :keeping - An `integer` indicating the number of versions to be kept for
# each item per date. Defaults to `1`.
# each item per date. Defaults to `1`. The most recent matching versions
# are kept.
# - :date - Should either be a `Date` object specifying which date to
# destroy versions for or `:all`, which will specify that all dates
# should be cleaned. Defaults to `:all`.
Expand All @@ -30,12 +31,14 @@ def clean_versions!(options = {})
# Returns a hash of versions grouped by the `item_id` attribute formatted
# like this: {:item_id => PaperTrail::Version}. If `item_id` or `date` is
# set, versions will be narrowed to those pointing at items with those ids
# that were created on specified date.
# that were created on specified date. Versions are returned in
# chronological order.
def gather_versions(item_id = nil, date = :all)
unless date == :all || date.respond_to?(:to_date)
raise ArgumentError.new("`date` argument must receive a Timestamp or `:all`")
end
versions = item_id ? PaperTrail::Version.where(:item_id => item_id) : PaperTrail::Version
versions = versions.order(PaperTrail.timestamp_field, :id)
versions = versions.between(date.to_date, date.to_date + 1.day) unless date == :all

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