Skip to content

Commit

Permalink
Copy daniel's tests from his PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Kmita committed Sep 23, 2014
1 parent ed66567 commit 5f4800f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/zombie_record/restorable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def associate_method(association, klass = nil)
when :has_one, :belongs_to
(klass || association.klass).unscoped { super() }
when :has_many
super().deleted
super().with_deleted
end
else
super()
Expand Down
44 changes: 37 additions & 7 deletions spec/restorable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,46 @@
end

describe "#associate_with_deleted" do
it "removes default scope from associations when deleted" do
cover = Cover.create!
book = Book.create!(cover: cover)
it "allows accessing a deleted has_one association" do
book = Book.create!
cover = Cover.create!(book: book)

book.destroy
Book.find_by_id(book.id).should be_nil
Cover.find_by_id(cover.id).should be_nil
book = Book.deleted.find(book.id)

book = Book.deleted.first
book.cover.should == cover
book.cover.should == cover.reload
end

it "allows accessing deleted belongs_to associations" do
book = Book.create!
chapter = book.chapters.create!

book.destroy
chapter = Chapter.deleted.find(chapter.id)

chapter.book.should == book
end

it "ensures deleted associations themselves allow access to deleted records" do
book = Book.create!
chapter = book.chapters.create!
bookmark = book.bookmarks.create!

book.destroy
bookmark = Bookmark.deleted.find(bookmark.id)

bookmark.book.chapters.should == [chapter]
chapter = bookmark.book.chapters.first

chapter.book.should == book
end

it "forwards normal method calls" do
book = Book.create!(title: "The Odyssey")
book.destroy
book = Book.deleted.find(book.id)

book.title.should == "The Odyssey"
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Library < ActiveRecord::Base
t.integer :author_id
t.integer :library_id
t.timestamps
t.string :title
t.timestamp :deleted_at
end

Expand Down

0 comments on commit 5f4800f

Please sign in to comment.