Add convenience method to find out if instance is live.

This commit is contained in:
Andy Stewart 2010-10-21 12:30:50 +01:00
parent 15d557d4f9
commit aa00ccae02
3 changed files with 26 additions and 2 deletions

View File

@ -171,6 +171,13 @@ Finally, if you got an item by reifying one of its versions, you can navigate ba
>> widget = latest_version.reify
>> widget.version == latest_version # true
You can find out whether a model instance is the current, live one -- or whether it came instead from a previous version -- with `live?`:
>> widget = Widget.find 42
>> widget.live? # true
>> widget = widget.versions.last.reify
>> widget.live? # false
## Finding Out Who Was Responsible For A Change

View File

@ -78,6 +78,12 @@ module PaperTrail
end
end
# Returns true if this instance is the current, live one;
# returns false if this instance came from a previous version.
def live?
version.nil?
end
# Returns who put the object into its current state.
def originator
versions.last.try :whodunnit

View File

@ -46,8 +46,7 @@ end
class HasPaperTrailModelTest < Test::Unit::TestCase
load_schema
=begin
=end
context 'A record' do
setup { @article = Article.create }
@ -70,6 +69,10 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
assert_equal [], @widget.versions
end
should 'be live' do
assert @widget.live?
end
context 'which is then created' do
setup { @widget.update_attributes :name => 'Henry' }
@ -87,6 +90,10 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
assert_match /create/i, @widget.versions.first.event
end
should 'be live' do
assert @widget.live?
end
context 'and then updated without any changes' do
setup { @widget.save }
@ -120,6 +127,10 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
assert_match /update/i, @widget.versions.last.event
end
should 'have versions that are not live' do
assert @widget.versions.map(&:reify).compact.all? { |w| !w.live? }
end
context 'and has one associated object' do
setup do