2013-03-21 16:48:14 -04:00
|
|
|
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)
|
2013-07-16 20:53:18 -04:00
|
|
|
group = keep_versions(group)
|
2013-03-21 16:48:14 -04:00
|
|
|
if group.size > 0
|
|
|
|
group.each do |member|
|
|
|
|
member.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-16 20:53:18 -04:00
|
|
|
def keep_versions(group)
|
|
|
|
@keeping_versions.times do
|
|
|
|
group.pop
|
|
|
|
end
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
2013-03-21 16:48:14 -04:00
|
|
|
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
|
|
|
|
|
2013-07-16 20:53:18 -04:00
|
|
|
def clean_paper_trail_versions(keeping = 1)
|
|
|
|
@keeping_versions = keeping
|
2013-03-21 16:48:14 -04:00
|
|
|
acquire_version_info
|
|
|
|
examine_and_clean_versions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|