Permalink
Browse files

Add convenience method to find out if instance is live.

  • Loading branch information...
airblade committed Oct 21, 2010
1 parent 15d557d commit aa00ccae0229ed9f4e81ec0fec053a560dc435f4
Showing with 26 additions and 2 deletions.
  1. +7 −0 README.md
  2. +6 −0 lib/paper_trail/has_paper_trail.rb
  3. +13 −2 test/paper_trail_model_test.rb
View
@@ -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
@@ -78,6 +78,12 @@ def record_destroy
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
@@ -46,8 +46,7 @@ class Person < ActiveRecord::Base
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

0 comments on commit aa00cca

Please sign in to comment.