Merge pull request #850 from airblade/clarify_docs_undelete_example

Docs: clarify the un-deletion example
This commit is contained in:
Jared Beck 2016-08-23 03:14:30 -04:00 committed by GitHub
commit 5a88c96141
2 changed files with 22 additions and 3 deletions

View File

@ -516,11 +516,12 @@ Note `version_at` gives you the object, not a version, so you don't need to call
Undeleting is just as simple:
```ruby
widget = Widget.find 42
widget = Widget.find(42)
widget.destroy
# Time passes....
widget = PaperTrail::Version.find(153).reify # the widget as it was before destruction
widget.save # the widget lives!
versions = widget.versions # versions ordered by versions.created_at, ascending
widget = versions.last.reify # the widget as it was before destruction
widget.save # the widget lives!
```
You could even use PaperTrail to implement an undo system, [Ryan Bates has!][3]

View File

@ -155,6 +155,24 @@ describe Widget, type: :model do
versions_for_widget = PaperTrail::Version.with_item_keys("Widget", widget.id)
assert_equal 2, versions_for_widget.length
end
it "can have multiple destruction records" do
versions = lambda { |widget|
# Workaround for AR 3. When we drop AR 3 support, we can simply use
# the `widget.versions` association, instead of `with_item_keys`.
PaperTrail::Version.with_item_keys("Widget", widget.id)
}
widget = Widget.create
assert_equal 1, widget.versions.length
widget.destroy
assert_equal 2, versions.call(widget).length
widget = widget.version.reify
widget.save
assert_equal 3, versions.call(widget).length
widget.destroy
assert_equal 4, versions.call(widget).length
assert_equal 2, versions.call(widget).where(event: "destroy").length
end
end
describe "#paper_trail.originator" do