Merge remote-tracking branch 'SeanMarcia/master' into cleaner
Conflicts: lib/paper_trail/version.rb
This commit is contained in:
commit
b5fa5fee9a
|
@ -1,3 +1,4 @@
|
|||
require 'paper_trail/cleaner'
|
||||
require 'paper_trail/config'
|
||||
require 'paper_trail/controller'
|
||||
require 'paper_trail/has_paper_trail'
|
||||
|
@ -8,6 +9,7 @@ require 'paper_trail/serializers/json'
|
|||
|
||||
# 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)
|
||||
|
|
|
@ -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
|
|
@ -150,6 +150,10 @@ module PaperTrail
|
|||
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
|
||||
|
|
|
@ -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, PaperTrail::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, PaperTrail::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, PaperTrail::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, PaperTrail::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, PaperTrail::Version.all.count
|
||||
assert_equal "Animal Muppet", @animal.name
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue