Skip to content

Commit

Permalink
Fix NoMethodError when trying to access the :previous revision on a m…
Browse files Browse the repository at this point in the history
…odel that doesn't have previous revisions [Alex Soto]
  • Loading branch information
bkeepers committed Apr 18, 2008
1 parent 1c9cb5c commit c10ca9a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
acts_as_audited ChangeLog
-------------------------------------------------------------------------------

* 2008-04-18 - Fix NoMethodError when trying to access the :previous revision
on a model that doesn't have previous revisions [Alex Soto]
* 2008-03-21 - added #changed_attributes to get access to the changes before a
save [Chris Parker]
* 2007-12-16 - Added #revision_at for retrieving a revision from a specific
Expand Down
5 changes: 4 additions & 1 deletion lib/acts_as_audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ def revision_at(date_or_time)
private

def changes(from_version = 1)
from_version = audits.find(:first).version if from_version == :previous
if from_version == :previous
last_audit = audits.find(:first)
from_version = last_audit ? last_audit.version : 1
end
changes = {}
result = audits.find(:all, :conditions => ['version >= ?', from_version]).collect do |audit|
attributes = (audit.changes || {}).inject({}) do |attrs, (name, values)|
Expand Down
7 changes: 7 additions & 0 deletions test/acts_as_audited_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def test_save_revision_records_audit
end
end

def test_without_previous_audits
user = create_user
user.audits.destroy_all
assert_nothing_raised(NoMethodError) { user.revision(:previous) }
end


private

def create_user(attrs = {})
Expand Down

0 comments on commit c10ca9a

Please sign in to comment.