diff --git a/lib/paper_trail/cleaner.rb b/lib/paper_trail/cleaner.rb index 34442e3f..5b9f7e86 100644 --- a/lib/paper_trail/cleaner.rb +++ b/lib/paper_trail/cleaner.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 39a5afe6..609b9b22 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 diff --git a/test/unit/cleaner_test.rb b/test/unit/cleaner_test.rb index 8f07b26f..cfc84d5c 100644 --- a/test/unit/cleaner_test.rb +++ b/test/unit/cleaner_test.rb @@ -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 diff --git a/test/unit/timestamp_test.rb b/test/unit/timestamp_test.rb index b94822d9..01638e6c 100644 --- a/test/unit/timestamp_test.rb +++ b/test/unit/timestamp_test.rb @@ -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