* Fixed favorites deletion
* Fixed parenting when dealing with post deletion
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
class Favorite < ActiveRecord::Base
|
||||
belongs_to :post
|
||||
scope :for_user, lambda {|user_id| where("user_id % 100 = #{user_id.to_i % 100} and user_id = #{user_id.to_i}")}
|
||||
|
||||
# this is necessary because there's no trigger for deleting favorites
|
||||
def self.destroy_all(hash)
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -532,10 +532,6 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
module FavoriteMethods
|
||||
def delete_favorites
|
||||
Favorite.delete_all(:post_id => id)
|
||||
end
|
||||
|
||||
def favorited_by?(user_id)
|
||||
fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
|
||||
end
|
||||
@@ -896,10 +892,11 @@ class Post < ActiveRecord::Base
|
||||
|
||||
module ClassMethods
|
||||
def update_has_children_flag_for(post_id)
|
||||
return if post_id.nil?
|
||||
has_children = Post.exists?(["is_deleted = ? AND parent_id = ?", false, post_id])
|
||||
execute_sql("UPDATE posts SET has_children = ? WHERE id = ?", has_children, post_id)
|
||||
end
|
||||
|
||||
|
||||
def recalculate_has_children_for_all_posts
|
||||
transaction do
|
||||
execute_sql("UPDATE posts SET has_children = false WHERE has_children = true")
|
||||
@@ -920,7 +917,8 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def update_parent_on_destroy
|
||||
Post.update_has_children_flag_for(parent_id)
|
||||
Post.update_has_children_flag_for(id)
|
||||
Post.update_has_children_flag_for(parent_id) if parent_id
|
||||
Post.update_has_children_flag_for(parent_id_was) if parent_id_was && parent_id != parent_id_was
|
||||
end
|
||||
|
||||
@@ -931,8 +929,8 @@ class Post < ActiveRecord::Base
|
||||
children.first.update_column(:parent_id, nil)
|
||||
else
|
||||
cached_children = children
|
||||
Post.update_all({:parent_id => cached_children[0].id}, :id => cached_children[1..-1].map(&:id))
|
||||
cached_children[0].update_column(:parent_id, nil)
|
||||
Post.update_all({:parent_id => cached_children[0].id}, :id => cached_children[1..-1].map(&:id))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -975,12 +973,12 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
Post.transaction do
|
||||
update_column(:is_deleted, true)
|
||||
give_favorites_to_parent
|
||||
update_children_on_destroy
|
||||
delete_favorites
|
||||
decrement_tag_post_counts
|
||||
update_column(:is_deleted, true)
|
||||
update_parent_on_destroy
|
||||
decrement_tag_post_counts
|
||||
update_column(:parent_id, nil)
|
||||
tag_array.each {|x| expire_cache(x)}
|
||||
ModAction.create(:description => "deleted post ##{id}")
|
||||
end
|
||||
|
||||
@@ -198,14 +198,14 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def add_favorite!(post)
|
||||
return if Favorite.exists?(:user_id => id, :post_id => post.id)
|
||||
return if Favorite.for_user(id).exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.create(:user_id => id, :post_id => post.id)
|
||||
increment!(:favorite_count)
|
||||
post.add_favorite!(self)
|
||||
end
|
||||
|
||||
def remove_favorite!(post)
|
||||
return unless Favorite.exists?(:user_id => id, :post_id => post.id)
|
||||
return unless Favorite.for_user(id).exists?(:user_id => id, :post_id => post.id)
|
||||
Favorite.destroy_all(:user_id => id, :post_id => post.id)
|
||||
decrement!(:favorite_count)
|
||||
post.remove_favorite!(self)
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
<% if post.is_deleted? %>
|
||||
<div class="ui-corner-all ui-state-error notice">
|
||||
This post has been deleted
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<% if (post.is_flagged? || post.is_deleted?) && post.flags.any? %>
|
||||
<div class="ui-corner-all ui-state-highlight notice">
|
||||
This post has been flagged for deletion: <%= post_flag_reasons(post) %>
|
||||
|
||||
@@ -113,29 +113,44 @@ class PostTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "Destroying a post with a parent" do
|
||||
should "reassign favorites to the parent" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
user = Factory.create(:user)
|
||||
c1.add_favorite!(user)
|
||||
c1.delete!
|
||||
p1.reload
|
||||
assert(!Favorite.exists?(:post_id => c1.id, :user_id => user.id))
|
||||
assert(Favorite.exists?(:post_id => p1.id, :user_id => user.id))
|
||||
end
|
||||
|
||||
should "update the parent's has_children flag" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
c1.delete!
|
||||
p1.reload
|
||||
assert(!p1.has_children?, "Parent should not have children")
|
||||
end
|
||||
end
|
||||
|
||||
context "Destroying a post with" do
|
||||
context "a parent" do
|
||||
should "reset the has_children flag of the parent" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
c1.delete!
|
||||
p1.reload
|
||||
assert_equal(false, p1.has_children?)
|
||||
end
|
||||
|
||||
should "reassign favorites to the parent" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
user = Factory.create(:user)
|
||||
c1.add_favorite!(user)
|
||||
c1.delete!
|
||||
p1.reload
|
||||
assert(!Favorite.exists?(:post_id => c1.id, :user_id => user.id))
|
||||
assert(Favorite.exists?(:post_id => p1.id, :user_id => user.id))
|
||||
end
|
||||
|
||||
should "update the parent's has_children flag" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
c1.delete!
|
||||
p1.reload
|
||||
assert(!p1.has_children?, "Parent should not have children")
|
||||
end
|
||||
end
|
||||
|
||||
context "one child" do
|
||||
should "remove the has_children flag" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
p1.delete!
|
||||
assert_equal(false, p1.has_children?)
|
||||
end
|
||||
|
||||
should "remove the parent of that child" do
|
||||
p1 = Factory.create(:post)
|
||||
c1 = Factory.create(:post, :parent_id => p1.id)
|
||||
|
||||
Reference in New Issue
Block a user