Fix #3822: Move favorites (while deleting) fails if user has also favorited destination

This commit is contained in:
evazion
2018-08-19 00:54:17 -05:00
parent eaef616a01
commit c504ad555d
2 changed files with 10 additions and 4 deletions

View File

@@ -966,6 +966,13 @@ class Post < ApplicationRecord
clean_fav_string! if clean_fav_string?
end
def add_favorite(user)
add_favorite!(user)
true
rescue Favorite::Error
false
end
def add_favorite!(user)
Favorite.add(post: self, user: user)
vote!("up", user) if user.is_voter?
@@ -1295,7 +1302,7 @@ class Post < ApplicationRecord
transaction do
favorites.each do |fav|
remove_favorite!(fav.user)
parent.add_favorite!(fav.user)
parent.add_favorite(fav.user)
end
end

View File

@@ -68,7 +68,6 @@ class PostTest < ActiveSupport::TestCase
end
should "decrement the user's favorite count" do
@post.add_favorite!(@post.uploader)
assert_difference(["@post.uploader.reload.favorite_count"], -1) do
@post.expunge!
end
@@ -1720,13 +1719,13 @@ class PostTest < ActiveSupport::TestCase
assert_equal(0, @post.score)
end
should "update the fav strings ont he post" do
should "update the fav strings on the post" do
@post.add_favorite!(@user)
@post.reload
assert_equal("fav:#{@user.id}", @post.fav_string)
assert(Favorite.exists?(:user_id => @user.id, :post_id => @post.id))
@post.add_favorite!(@user)
assert_raises(Favorite::Error) { @post.add_favorite!(@user) }
@post.reload
assert_equal("fav:#{@user.id}", @post.fav_string)
assert(Favorite.exists?(:user_id => @user.id, :post_id => @post.id))