Skip to content

Commit

Permalink
Fixed :next_version method to return the live model when appropriate. C…
Browse files Browse the repository at this point in the history
…lose #200
  • Loading branch information
Ben Atkins committed Feb 5, 2013
1 parent fed9b6d commit 143d1ac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 2.7.1 (Unreleased)

- [#200](https://github.com/airblade/paper_trail/issues/200) - Fixed `next_version` method so that it returns the live model
when called on latest reified version of a model prior to the live model.
- [#197](https://github.com/airblade/paper_trail/issues/197) - PaperTrail now falls back on using YAML for serialization of
serialized model attributes for storage in the `object` and `object_changes` columns in the `Version` table. This fixes
compatibility for `Rails 3.0.x` for projects that employ the `serialize` declaration on a model.
Expand Down
12 changes: 7 additions & 5 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,17 @@ def versions_between(start_time, end_time, reify_options={})
# Returns the object (not a Version) as it was most recently.
def previous_version
preceding_version = source_version ? source_version.previous : send(self.class.versions_association_name).last
preceding_version.try :reify
preceding_version.reify if preceding_version
end

# Returns the object (not a Version) as it became next.
# NOTE: if self (the item) was not reified from a version, i.e. it is the
# "live" item, we return nil. Perhaps we should return self instead?
def next_version
# NOTE: if self (the item) was not reified from a version, i.e. it is the
# "live" item, we return nil. Perhaps we should return self instead?
subsequent_version = source_version ? source_version.next : nil
subsequent_version.reify if subsequent_version
subsequent_version = source_version.next
subsequent_version ? subsequent_version.reify : self.class.find(self.id)
rescue
nil
end

# Executes the given method or block without creating a new version.
Expand Down
27 changes: 15 additions & 12 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -795,32 +795,31 @@ def without(&block)
end

should 'have a previous version' do
assert_equal @widget.versions.last.reify, @widget.previous_version
assert_equal @widget.versions.last.reify.name, @widget.previous_version.name
end

should 'have a next version' do
should 'not have a next version' do
assert_nil @widget.next_version
end
end
end

context 'A reified item' do
setup do
widget = Widget.create :name => 'Bob'
%w( Tom Dick Jane ).each { |name| widget.update_attributes :name => name }
@versions = widget.versions
@second_widget = @versions[1].reify # first widget is null
@last_widget = @versions.last.reify
@widget = Widget.create :name => 'Bob'
%w(Tom Dick Jane).each { |name| @widget.update_attributes :name => name }
@second_widget = @widget.versions[1].reify # first widget is `nil`
@last_widget = @widget.versions.last.reify
end

should 'have a previous version' do
assert_nil @second_widget.previous_version
assert_equal @versions[-2].reify, @last_widget.previous_version
assert_nil @second_widget.previous_version # `create` events return `nil` for `reify`
assert_equal @widget.versions[-2].reify.name, @last_widget.previous_version.name
end

should 'have a next version' do
assert_equal @versions[2].reify, @second_widget.next_version
assert_nil @last_widget.next_version
assert_equal @widget.versions[2].reify.name, @second_widget.next_version.name
assert_equal @last_widget.next_version.name, @widget.name
end
end

Expand Down Expand Up @@ -1106,7 +1105,11 @@ def without(&block)
assert_equal 2, @doc.paper_trail_versions.length
end

should 'respond to previous_version as normal' do
should 'respond to `next_version` as normal' do
assert_equal @doc.paper_trail_versions.last.reify.next_version.name, @doc.name
end

should 'respond to `previous_version` as normal' do
@doc.update_attributes :name => 'Doc 2'
assert_equal 3, @doc.paper_trail_versions.length
assert_equal 'Doc 1', @doc.previous_version.name
Expand Down

0 comments on commit 143d1ac

Please sign in to comment.