added revert methods for post

This commit is contained in:
albert 2011-01-26 18:26:32 -05:00
parent f7e2344b9f
commit 7051b016f5
2 changed files with 62 additions and 0 deletions

View File

@ -788,6 +788,33 @@ class Post < ActiveRecord::Base
)
end
end
def revert_to(target)
base_tags = []
base_rating = "q"
base_source = nil
base_parent_id = nil
versions.each do |version|
if version.id <= target.id
base_tags += version.add_tag_array
base_tags -= version.del_tag_array
base_rating = version.rating if version.rating
base_source = version.source if version.source
base_parent_id = version.parent_id if version.parent_id
end
end
self.tag_string = base_tags.sort.join(" ")
self.rating = base_rating
self.source = base_source
self.parent_id = base_parent_id
end
def revert_to!(target)
revert_to(target)
save!
end
end
include FileMethods

View File

@ -596,4 +596,39 @@ class PostTest < ActiveSupport::TestCase
end
end
end
context "Reverting: " do
context "a post that has been updated" do
setup do
@post = Factory.create(:post, :rating => "q", :tag_string => "aaa")
@post.update_attributes(:tag_string => "aaa bbb ccc ddd")
@post.update_attributes(:tag_string => "bbb xxx yyy", :source => "xyz")
@post.update_attributes(:tag_string => "bbb mmm yyy", :source => "abc")
end
context "and then reverted to an early version" do
setup do
@post.revert_to(@post.versions[1])
end
should "correctly revert all fields" do
assert_equal("aaa bbb ccc ddd", @post.tag_string)
assert_equal(nil, @post.source)
assert_equal("q", @post.rating)
end
end
context "and then reverted to a later version" do
setup do
@post.revert_to(@post.versions[-2])
end
should "correctly revert all fields" do
assert_equal("bbb xxx yyy", @post.tag_string)
assert_equal("xyz", @post.source)
assert_equal("q", @post.rating)
end
end
end
end
end