1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Tests to cover changes from cb46e87; Cleaner module should group versions by PaperTrail.timestamp_field

This commit is contained in:
Ben Atkins 2014-03-18 15:56:53 -04:00
parent 4fe79ff674
commit 1c00c0e7d2
4 changed files with 55 additions and 11 deletions

View file

@ -12,8 +12,9 @@ module PaperTrail
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.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.group_by { |v| v.send(PaperTrail.timestamp_field).to_date }.each do |date, versions|
# remove the number of versions we wish to keep from the collection of versions prior to destruction
versions.pop(options[:keeping])
versions.map(&:destroy)
end
end
@ -27,7 +28,6 @@ module PaperTrail
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

View file

@ -32,6 +32,7 @@ def change_schema
add_column :versions, :custom_created_at, :datetime
end
ActiveRecord::Migration.verbose = true
reset_version_class_column_info!
end
def restore_schema
@ -41,4 +42,10 @@ def restore_schema
remove_column :versions, :custom_created_at
end
ActiveRecord::Migration.verbose = true
reset_version_class_column_info!
end
def reset_version_class_column_info!
PaperTrail::Version.connection.schema_cache.clear!
PaperTrail::Version.reset_column_information
end

View file

@ -2,19 +2,21 @@ require 'test_helper'
class PaperTrailCleanerTest < ActiveSupport::TestCase
setup do
def populate_db!
@animals = [@animal = Animal.new, @dog = Dog.new, @cat = Cat.new]
@animals.each do |animal|
3.times { animal.update_attribute(:name, Faker::Name.name) }
end
end
test 'Baseline' do
assert_equal 9, PaperTrail::Version.count
@animals.each { |animal| assert_equal 3, animal.versions.size }
end
context '`clean_versions!` method' do
setup { self.populate_db! }
should 'Baseline' do
assert_equal 9, PaperTrail::Version.count
@animals.each { |animal| assert_equal 3, animal.versions.size }
end
should 'be extended by `PaperTrail` module' do
assert_respond_to PaperTrail, :clean_versions!
end
@ -140,4 +142,41 @@ class PaperTrailCleanerTest < ActiveSupport::TestCase
end
end # clean_versions! method
context "Custom timestamp field" do
setup do
change_schema
self.populate_db!
# now mess with the timestamps
@animals.each do |animal|
animal.versions.reverse.each_with_index do |version, index|
version.update_attribute(:custom_created_at, Time.now.utc + index.days)
end
end
PaperTrail.timestamp_field = :custom_created_at
@animals.map { |a| a.versions(true) } # reload the `versions` association for each animal
end
teardown do
PaperTrail.timestamp_field = :created_at
restore_schema
end
should 'Baseline' do
assert_equal 9, PaperTrail::Version.count
@animals.each do |animal|
assert_equal 3, animal.versions.size
animal.versions.each_cons(2) do |a,b|
a.created_at.to_date == b.created_at.to_date
a.custom_created_at.to_date != b.custom_created_at.to_date
end
end
end
should 'group by `PaperTrail.timestamp_field` when seperating the versions by date to clean' do
assert_equal 9, PaperTrail::Version.count
PaperTrail.clean_versions!
assert_equal 9, PaperTrail::Version.count
end
end
end

View file

@ -5,8 +5,6 @@ class TimestampTest < ActiveSupport::TestCase
setup do
PaperTrail.timestamp_field = :custom_created_at
change_schema
PaperTrail::Version.connection.schema_cache.clear!
PaperTrail::Version.reset_column_information
Fluxor.instance_eval <<-END
has_paper_trail