fixes #2731: Expunged posts should clear favorite groups

This commit is contained in:
Albert Yi
2016-11-01 13:44:36 -07:00
parent cb1c694074
commit 91793fff82
4 changed files with 92 additions and 0 deletions

View File

@@ -18,6 +18,11 @@ class FavoriteGroup < ActiveRecord::Base
where("favorite_groups.creator_id = ?", user_id)
end
def for_post(post_id)
regexp = "(^#{post_id}$|^#{post_id} | #{post_id}$| #{post_id} )"
where("favorite_groups.post_ids ~ ?", regexp)
end
def named(name)
where("lower(name) = ?", name.to_s.mb_chars.downcase.strip)
end

View File

@@ -959,6 +959,12 @@ class Post < ActiveRecord::Base
groups.uniq
end
end
def remove_from_fav_groups
FavoriteGroup.for_post(id).find_each do |group|
group.remove!(self)
end
end
end
module UploaderMethods
@@ -1313,6 +1319,7 @@ class Post < ActiveRecord::Base
update_children_on_destroy
decrement_tag_post_counts
remove_from_all_pools
remove_from_fav_groups
destroy
update_parent_on_destroy
end

View File

@@ -0,0 +1,4 @@
FactoryGirl.define do
factory(:favorite_group) do
end
end

View File

@@ -0,0 +1,76 @@
require 'test_helper'
class FavoriteTest < ActiveSupport::TestCase
def setup
super
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@fav_group = FactoryGirl.create(:favorite_group, creator: @user, name: "blah")
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "searching by post id" do
context "when the id is the only one" do
setup do
@fav_group.post_ids = "1"
@fav_group.save
end
should "return the fav group" do
assert_equal(@fav_group.id, FavoriteGroup.for_post(1).first.try(:id))
end
end
context "when the id is in the beginning" do
setup do
@fav_group.post_ids = "1 2"
@fav_group.save
end
should "return the fav group" do
assert_equal(@fav_group.id, FavoriteGroup.for_post(1).first.try(:id))
end
end
context "when the id is in the middle" do
setup do
@fav_group.post_ids = "3 1 2"
@fav_group.save
end
should "return the fav group" do
assert_equal(@fav_group.id, FavoriteGroup.for_post(1).first.try(:id))
end
end
context "when the id is in the end" do
setup do
@fav_group.post_ids = "2 1"
@fav_group.save
end
should "return the fav group" do
assert_equal(@fav_group.id, FavoriteGroup.for_post(1).first.try(:id))
end
end
end
context "expunging a post" do
setup do
@post = FactoryGirl.create(:post)
@fav_group.add!(@post)
end
should "remove it from all favorite groups" do
@post.expunge!
@fav_group.reload
assert_equal("", @fav_group.post_ids)
end
end
end