test/unit/model_test.rb in paper_trail-4.0.0.beta2 vs test/unit/model_test.rb in paper_trail-4.0.0.rc1
- old
+ new
@@ -216,12 +216,12 @@
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,
- # so chosing the lowest common denominator to test.
+ #TODO Postgres does not appear to pass back ActiveSupport::TimeWithZone,
+ # so chosing the lowest common denominator to test.
changes = {
'name' => [nil, 'Henry'],
'created_at' => [nil, @widget.created_at.to_time.utc],
'updated_at' => [nil, @widget.updated_at.to_time.utc],
@@ -330,11 +330,36 @@
assert_equal @widget.versions.length, @reified_widget.versions.length
assert_same_elements @widget.versions, @reified_widget.versions
end
end
+ context 'and has many associated polymorphic objects' do
+ setup do
+ @f0 = @widget.whatchamajiggers.create :name => 'f-zero'
+ @f1 = @widget.whatchamajiggers.create :name => 'f-zero'
+ @reified_widget = @widget.versions.last.reify
+ end
+ should 'copy the has_many associations when reifying' do
+ assert_equal @widget.whatchamajiggers.length, @reified_widget.whatchamajiggers.length
+ assert_same_elements @widget.whatchamajiggers, @reified_widget.whatchamajiggers
+
+ assert_equal @widget.versions.length, @reified_widget.versions.length
+ assert_same_elements @widget.versions, @reified_widget.versions
+ end
+ end
+
+ context 'polymorphic objects by themselves' do
+ setup do
+ @widget = Whatchamajigger.new :name => 'f-zero'
+ end
+
+ should 'not fail with a nil pointer on the polymorphic association' do
+ @widget.save!
+ end
+ end
+
context 'and then destroyed' do
setup do
@fluxor = @widget.fluxors.create :name => 'flux'
@widget.destroy
@reified_widget = PaperTrail::Version.last.reify
@@ -444,11 +469,11 @@
Widget.reset_column_information
assert_raise(NoMethodError) { Widget.new.sacrificial_column }
@last = @widget.versions.last
end
- teardown do
+ teardown do
restore_schema
end
should 'reify previous version' do
assert_kind_of Widget, @last.reify
@@ -569,13 +594,13 @@
@version = @widget.versions.last # only 1 version
end
should 'track who made the change' do
assert_equal 'Alice', @version.whodunnit
- assert_nil @version.originator
+ assert_nil @version.paper_trail_originator
assert_equal 'Alice', @version.terminator
- assert_equal 'Alice', @widget.originator
+ assert_equal 'Alice', @widget.paper_trail_originator
end
context 'when a record is updated' do
setup do
PaperTrail.whodunnit = 'Bob'
@@ -583,13 +608,13 @@
@version = @widget.versions.last
end
should 'track who made the change' do
assert_equal 'Bob', @version.whodunnit
- assert_equal 'Alice', @version.originator
+ assert_equal 'Alice', @version.paper_trail_originator
assert_equal 'Bob', @version.terminator
- assert_equal 'Bob', @widget.originator
+ assert_equal 'Bob', @widget.paper_trail_originator
end
context 'when a record is destroyed' do
setup do
PaperTrail.whodunnit = 'Charlie'
@@ -597,13 +622,13 @@
@version = PaperTrail::Version.last
end
should 'track who made the change' do
assert_equal 'Charlie', @version.whodunnit
- assert_equal 'Bob', @version.originator
+ assert_equal 'Bob', @version.paper_trail_originator
assert_equal 'Charlie', @version.terminator
- assert_equal 'Charlie', @widget.originator
+ assert_equal 'Charlie', @widget.paper_trail_originator
end
end
end
end
end
@@ -618,11 +643,10 @@
@wotsit.update_attributes! :name => 'changed'
assert_not_nil @wotsit.versions.last.reify.created_at
assert_not_nil @wotsit.versions.last.reify.updated_at
end
- # Currently the gem generates a bunch of deprecation warnings about serialized attributes on AR 4.2
should 'not generate warning' do
# Tests that it doesn't try to write created_on as an attribute just because a created_on
# method exists.
warnings = capture(:stderr) { # Deprecation warning in Rails 3.2
assert_nothing_raised { # ActiveModel::MissingAttributeError in Rails 4
@@ -649,11 +673,11 @@
end
should 'should return the correct originator' do
PaperTrail.whodunnit = 'Ben'
@foo.update_attribute(:name, 'Geoffrey')
- assert_equal PaperTrail.whodunnit, @foo.originator
+ assert_equal PaperTrail.whodunnit, @foo.paper_trail_originator
end
context 'when destroyed' do
setup { @foo.destroy }
@@ -1100,10 +1124,25 @@
assert_equal 5, @song.length
end
should 'return "overwritten" value on reified instance' do
assert_equal 4, @song.versions.last.reify.length
end
+
+ context 'Has a virtual attribute injected into the ActiveModel::Dirty changes' do
+ setup do
+ @song.name = 'Good Vibrations'
+ @song.save
+ @song.name = 'Yellow Submarine'
+ end
+
+ should 'return persist the changes on the live instance properly' do
+ assert_equal 'Yellow Submarine', @song.name
+ end
+ should 'return "overwritten" virtual attribute on the reified instance' do
+ assert_equal 'Good Vibrations', @song.versions.last.reify.name
+ end
+ end
end
context 'An unsaved record' do
setup do
@@ -1187,10 +1226,35 @@
should 'only have a version for the destroy event' do
assert_equal 1, @fluxor.versions.length
assert_equal 'destroy', @fluxor.versions.last.event
end
end
+ context 'on []' do
+ setup do
+ Fluxor.reset_callbacks :create
+ Fluxor.reset_callbacks :update
+ Fluxor.reset_callbacks :destroy
+ Fluxor.instance_eval <<-END
+ has_paper_trail :on => []
+ END
+ @fluxor = Fluxor.create
+ @fluxor.update_attributes :name => 'blah'
+ end
+
+ teardown do
+ @fluxor.destroy
+ end
+
+ should 'not have any versions' do
+ assert_equal 0, @fluxor.versions.length
+ end
+
+ should 'still respond to touch_with_version' do
+ @fluxor.touch_with_version
+ assert_equal 1, @fluxor.versions.length
+ end
+ end
context 'allows a symbol to be passed' do
setup do
Fluxor.reset_callbacks :create
Fluxor.reset_callbacks :update
Fluxor.reset_callbacks :destroy
@@ -1836,6 +1900,6 @@
assert_equal ['editor_0'], @book_0.editors.map(&:name)
end
end
end
end
-end
\ No newline at end of file
+end