Skip to content

Commit

Permalink
Merge remote-tracking branch 'straydogstudio/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
batter committed Jul 29, 2014
2 parents 6421f1f + 5ee4582 commit 6de04f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
12 changes: 8 additions & 4 deletions lib/paper_trail/has_paper_trail.rb
Expand Up @@ -74,7 +74,7 @@ def has_paper_trail(options = {})
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)
Expand Down Expand Up @@ -259,7 +259,9 @@ def record_create
: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)
Expand All @@ -276,12 +278,14 @@ def record_update
: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

Expand Down
3 changes: 1 addition & 2 deletions lib/paper_trail/version_concern.rb
Expand Up @@ -7,8 +7,7 @@ module VersionConcern
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

Expand Down
6 changes: 5 additions & 1 deletion spec/models/widget_spec.rb
Expand Up @@ -49,7 +49,7 @@
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 }

Expand All @@ -59,6 +59,10 @@
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
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/initializers/paper_trail.rb
@@ -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
9 changes: 6 additions & 3 deletions test/unit/model_test.rb
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -731,7 +734,7 @@ def without(&block)
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
Expand Down

0 comments on commit 6de04f1

Please sign in to comment.