Module: PaperTrail::Cleaner

Included in:
PaperTrail
Defined in:
lib/paper_trail/cleaner.rb

Overview

Utilities for deleting version records.

Instance Method Summary collapse

Instance Method Details

#clean_versions!(options = {}) ⇒ Object

Destroys all but the most recent version(s) for items on a given date (or on all dates). Useful for deleting drafts.

Options:

  • :keeping - An ‘integer` indicating the number of versions to be kept for 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`.

  • :item_id - The ‘id` for the item to be cleaned on, or `nil`, which causes all items to be cleaned. Defaults to `nil`.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/paper_trail/cleaner.rb', line 20

def clean_versions!(options = {})
  options = { keeping: 1, date: :all }.merge(options)
  gather_versions(options[:item_id], options[:date]).each do |_item_id, item_versions|
    group_versions_by_date(item_versions).each do |_date, date_versions|
      # Remove the number of versions we wish to keep from the collection
      # of versions prior to destruction.
      date_versions.pop(options[:keeping])
      date_versions.map(&:destroy)
    end
  end
end