Skip to content

Commit 5ee4582

Browse files
Have Version created_at match model
1 parent c3f6072 commit 5ee4582

5 files changed

Lines changed: 21 additions & 11 deletions

File tree

lib/paper_trail/has_paper_trail.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def has_paper_trail(options = {})
7474
after_create :record_create, :if => :save_version? if options_on.empty? || options_on.include?(:create)
7575
if options_on.empty? || options_on.include?(:update)
7676
before_save :reset_timestamp_attrs_for_update_if_needed!, :on => :update
77-
before_update :record_update, :if => :save_version?
77+
after_update :record_update, :if => :save_version?
7878
after_update :clear_version_instance!
7979
end
8080
after_destroy :record_destroy, :if => :save_version? if options_on.empty? || options_on.include?(:destroy)
@@ -258,7 +258,9 @@ def record_create
258258
:event => paper_trail_event || 'create',
259259
:whodunnit => PaperTrail.whodunnit
260260
}
261-
261+
if respond_to?(:created_at)
262+
data[PaperTrail.timestamp_field] = created_at
263+
end
262264
if changed_notably? and self.class.paper_trail_version_class.column_names.include?('object_changes')
263265
data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail :
264266
PaperTrail.serializer.dump(changes_for_paper_trail)
@@ -275,12 +277,14 @@ def record_update
275277
:object => self.class.paper_trail_version_class.object_col_is_json? ? object_attrs : PaperTrail.serializer.dump(object_attrs),
276278
:whodunnit => PaperTrail.whodunnit
277279
}
278-
280+
if respond_to?(:updated_at)
281+
data[PaperTrail.timestamp_field] = updated_at
282+
end
279283
if self.class.paper_trail_version_class.column_names.include?('object_changes')
280284
data[:object_changes] = self.class.paper_trail_version_class.object_changes_col_is_json? ? changes_for_paper_trail :
281285
PaperTrail.serializer.dump(changes_for_paper_trail)
282286
end
283-
send(self.class.versions_association_name).build merge_metadata(data)
287+
send(self.class.versions_association_name).create merge_metadata(data)
284288
end
285289
end
286290

lib/paper_trail/version_concern.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ module VersionConcern
77
included do
88
belongs_to :item, :polymorphic => true
99
validates_presence_of :event
10-
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes if PaperTrail.active_record_protected_attributes?
11-
10+
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes, :created_at if PaperTrail.active_record_protected_attributes?
1211
after_create :enforce_version_limit!
1312
end
1413

spec/models/widget_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
end
3636

3737
describe :after_update do
38-
before { widget.update_attributes!(:name => 'Foobar') }
38+
before { widget.update_attributes!(:name => 'Foobar', :updated_at => Time.now + 1.week) }
3939

4040
subject { widget.versions.last.reify }
4141

@@ -45,6 +45,10 @@
4545
subject.save!
4646
subject.should be_live
4747
end
48+
49+
it "should use the widget updated_at" do
50+
widget.versions.last.created_at.to_i.should == widget.updated_at.to_i
51+
end
4852
end
4953

5054
describe :after_destroy do
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module PaperTrail
22
class Version < ActiveRecord::Base
3-
attr_accessible :created_at, :updated_at, :answer, :action, :question, :article_id, :ip, :user_agent, :title if ::PaperTrail.active_record_protected_attributes?
3+
attr_accessible :answer, :action, :question, :article_id, :ip, :user_agent, :title if ::PaperTrail.active_record_protected_attributes?
44
end
55
end

test/unit/model_test.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
191191
assert @widget.live?
192192
end
193193

194-
195194
context 'which is then created' do
196-
setup { @widget.update_attributes :name => 'Henry' }
195+
setup { @widget.update_attributes :name => 'Henry', :created_at => Time.now - 1.day }
197196

198197
should 'have one previous version' do
199198
assert_equal 1, @widget.versions.length
@@ -212,6 +211,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
212211
assert @widget.live?
213212
end
214213

214+
should 'use the widget created_at' do
215+
assert_equal @widget.created_at.to_i, @widget.versions.first.created_at.to_i
216+
end
217+
215218
should 'have changes' do
216219

217220
#TODO Postgres does not appear to pass back ActiveSupport::TimeWithZone,
@@ -731,7 +734,7 @@ def without(&block)
731734
should 'return versions in the time period' do
732735
assert_equal ['Fidget'], @widget.versions_between(20.days.ago, 10.days.ago).map(&:name)
733736
assert_equal ['Widget', 'Fidget'], @widget.versions_between(45.days.ago, 10.days.ago).map(&:name)
734-
assert_equal ['Fidget', 'Digit'], @widget.versions_between(16.days.ago, 1.minute.ago).map(&:name)
737+
assert_equal ['Fidget', 'Digit', 'Digit'], @widget.versions_between(16.days.ago, 1.minute.ago).map(&:name)
735738
assert_equal [], @widget.versions_between(60.days.ago, 45.days.ago).map(&:name)
736739
end
737740
end

0 commit comments

Comments
 (0)