Skip to content

Commit

Permalink
When Cleaning Versions ensure they are in the correct order.
Browse files Browse the repository at this point in the history
  • Loading branch information
rposborne committed Mar 16, 2014
1 parent 463c214 commit cb46e87
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/paper_trail/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Cleaner
def clean_versions!(options = {})
options = {:keeping => 1, :date => :all}.merge(options)
gather_versions(options[:item_id], options[:date]).each do |item_id, versions|
versions.group_by { |v| v.created_at.to_date }.each do |date, versions| # now group the versions by date and iterate through those
versions.group_by { |v| v.send(PaperTrail.timestamp_field).to_date }.each do |date, versions| # now group the versions by date and iterate through those
versions.pop(options[:keeping]) # remove the number of versions we wish to keep from the collection of versions prior to destruction
versions.map(&:destroy)
end
Expand All @@ -27,6 +27,7 @@ def gather_versions(item_id = nil, date = :all)
raise "`date` argument must receive a Timestamp or `:all`" unless date == :all || date.respond_to?(:to_date)
versions = item_id ? PaperTrail::Version.where(:item_id => item_id) : PaperTrail::Version
versions = versions.between(date.to_date, date.to_date + 1.day) unless date == :all
versions = versions.order("#{PaperTrail.timestamp_field} ASC")
versions = PaperTrail::Version.all if versions == PaperTrail::Version # if versions has not been converted to an ActiveRecord::Relation yet, do so now
versions.group_by(&:item_id)
end
Expand Down

0 comments on commit cb46e87

Please sign in to comment.