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

Have Version created_at match model

This commit is contained in:
Noel Peden 2014-05-30 14:38:23 -07:00
parent c3f6072e11
commit 5ee45827c2
5 changed files with 21 additions and 11 deletions

View file

@ -74,7 +74,7 @@ module PaperTrail
after_create :record_create, :if => :save_version? if options_on.empty? || options_on.include?(:create)
if options_on.empty? || options_on.include?(:update)
before_save :reset_timestamp_attrs_for_update_if_needed!, :on => :update
before_update :record_update, :if => :save_version?
after_update :record_update, :if => :save_version?
after_update :clear_version_instance!
end
after_destroy :record_destroy, :if => :save_version? if options_on.empty? || options_on.include?(:destroy)
@ -258,7 +258,9 @@ module PaperTrail
:event => paper_trail_event || 'create',
:whodunnit => PaperTrail.whodunnit
}
if respond_to?(:created_at)
data[PaperTrail.timestamp_field] = created_at
end
if changed_notably? and self.class.paper_trail_version_class.column_names.include?('object_changes')
data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail :
PaperTrail.serializer.dump(changes_for_paper_trail)
@ -275,12 +277,14 @@ module PaperTrail
:object => self.class.paper_trail_version_class.object_col_is_json? ? object_attrs : PaperTrail.serializer.dump(object_attrs),
:whodunnit => PaperTrail.whodunnit
}
if respond_to?(:updated_at)
data[PaperTrail.timestamp_field] = updated_at
end
if self.class.paper_trail_version_class.column_names.include?('object_changes')
data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail :
PaperTrail.serializer.dump(changes_for_paper_trail)
end
send(self.class.versions_association_name).build merge_metadata(data)
send(self.class.versions_association_name).create merge_metadata(data)
end
end

View file

@ -7,8 +7,7 @@ module PaperTrail
included do
belongs_to :item, :polymorphic => true
validates_presence_of :event
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes if PaperTrail.active_record_protected_attributes?
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes, :created_at if PaperTrail.active_record_protected_attributes?
after_create :enforce_version_limit!
end

View file

@ -35,7 +35,7 @@ describe Widget do
end
describe :after_update do
before { widget.update_attributes!(:name => 'Foobar') }
before { widget.update_attributes!(:name => 'Foobar', :updated_at => Time.now + 1.week) }
subject { widget.versions.last.reify }
@ -45,6 +45,10 @@ describe Widget do
subject.save!
subject.should be_live
end
it "should use the widget updated_at" do
widget.versions.last.created_at.to_i.should == widget.updated_at.to_i
end
end
describe :after_destroy do

View file

@ -1,5 +1,5 @@
module PaperTrail
class Version < ActiveRecord::Base
attr_accessible :created_at, :updated_at, :answer, :action, :question, :article_id, :ip, :user_agent, :title if ::PaperTrail.active_record_protected_attributes?
attr_accessible :answer, :action, :question, :article_id, :ip, :user_agent, :title if ::PaperTrail.active_record_protected_attributes?
end
end

View file

@ -191,9 +191,8 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
assert @widget.live?
end
context 'which is then created' do
setup { @widget.update_attributes :name => 'Henry' }
setup { @widget.update_attributes :name => 'Henry', :created_at => Time.now - 1.day }
should 'have one previous version' do
assert_equal 1, @widget.versions.length
@ -212,6 +211,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
assert @widget.live?
end
should 'use the widget created_at' do
assert_equal @widget.created_at.to_i, @widget.versions.first.created_at.to_i
end
should 'have changes' do
#TODO Postgres does not appear to pass back ActiveSupport::TimeWithZone,
@ -731,7 +734,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
should 'return versions in the time period' do
assert_equal ['Fidget'], @widget.versions_between(20.days.ago, 10.days.ago).map(&:name)
assert_equal ['Widget', 'Fidget'], @widget.versions_between(45.days.ago, 10.days.ago).map(&:name)
assert_equal ['Fidget', 'Digit'], @widget.versions_between(16.days.ago, 1.minute.ago).map(&:name)
assert_equal ['Fidget', 'Digit', 'Digit'], @widget.versions_between(16.days.ago, 1.minute.ago).map(&:name)
assert_equal [], @widget.versions_between(60.days.ago, 45.days.ago).map(&:name)
end
end