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

Clean Paper Trail Versions #212

Merged
merged 6 commits into from
Jul 31, 2013
2 changes: 2 additions & 0 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'paper_trail/cleaner'
require 'paper_trail/config'
require 'paper_trail/controller'
require 'paper_trail/has_paper_trail'
Expand All @@ -9,6 +10,7 @@
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail

extend PaperTrail::Cleaner
# Switches PaperTrail on or off.
def self.enabled=(value)
PaperTrail.config.enabled = value
Expand Down
55 changes: 55 additions & 0 deletions lib/paper_trail/cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module PaperTrail
module Cleaner
def gather_all_versions
Version.all.group_by(&:item_id)
end

def get_all_keys
@versions.keys
end

def grouping_for_key(key)
@versions[key].group_by(&:grouping_by_date)
end

def sanitize(group)
group = keep_versions(group)
if group.size > 0
group.each do |member|
member.destroy
end
end
end

def keep_versions(group)
@keeping_versions.times do
group.pop
end
group
end

def analyze_grouping(grouping)
grouping.each_value do |group|
sanitize(group)
end
end

def acquire_version_info
@versions = gather_all_versions
@keys = get_all_keys
end

def examine_and_clean_versions
@keys.each do |key|
grouping = grouping_for_key(key)
analyze_grouping(grouping)
end
end

def clean_paper_trail_versions(keeping = 1)
@keeping_versions = keeping
acquire_version_info
examine_and_clean_versions
end
end
end
4 changes: 4 additions & 0 deletions lib/paper_trail/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def index
sibling_versions.select(id_column).order("#{id_column} ASC").map(&id_column).index(self.send(id_column))
end

def grouping_by_date
created_at.to_date.to_s(:db)
end

private

# In Rails 3.1+, calling reify on a previous version confuses the
Expand Down
61 changes: 61 additions & 0 deletions test/unit/cleaner_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'test_helper'

class PaperTrailCleanerTest < ActiveSupport::TestCase

test 'Baseline' do
@animal = Animal.create :name => 'Animal'
@animal.update_attributes :name => 'Animal from the Muppets'
@animal.update_attributes :name => 'Animal Muppet'

@dog = Dog.create :name => 'Snoopy'
@dog.update_attributes :name => 'Scooby'
@dog.update_attributes :name => 'Scooby Doo'

@cat = Cat.create :name => 'Garfield'
@cat.update_attributes :name => 'Garfield (I hate Mondays)'
@cat.update_attributes :name => 'Garfield The Cat'
assert_equal 9, Version.count
end

test 'cleaner removes extra versions' do
@animal = Animal.create :name => 'Animal'
@animal.update_attributes :name => 'Animal from the Muppets'
@animal.update_attributes :name => 'Animal Muppet'
PaperTrail.clean_paper_trail_versions
assert_equal 1, Version.all.count
end

test 'cleaner removes versions' do
@animal = Animal.create :name => 'Animal'
@animal.update_attributes :name => 'Animal from the Muppets'
@animal.update_attributes :name => 'Animal Muppet'

@dog = Dog.create :name => 'Snoopy'
@dog.update_attributes :name => 'Scooby'
@dog.update_attributes :name => 'Scooby Doo'

@cat = Cat.create :name => 'Garfield'
@cat.update_attributes :name => 'Garfield (I hate Mondays)'
@cat.update_attributes :name => 'Garfield The Cat'
PaperTrail.clean_paper_trail_versions
assert_equal 3, Version.all.count
end

test 'cleaner keeps the correct (last) version' do
@animal = Animal.create :name => 'Animal'
@animal.update_attributes :name => 'Animal from the Muppets'
@animal.update_attributes :name => 'Animal Muppet'
PaperTrail.clean_paper_trail_versions
assert_equal 1, Version.all.count
assert_equal "Animal Muppet", @animal.name
end

test 'cleaner accepts variable arguments' do
@animal = Animal.create :name => 'Animal'
@animal.update_attributes :name => 'Animal from the Muppets'
@animal.update_attributes :name => 'Animal Muppet'
PaperTrail.clean_paper_trail_versions(2)
assert_equal 2, Version.all.count
assert_equal "Animal Muppet", @animal.name
end
end