From 073ab8ee963b2998a2fefba634309cffb37b28ac Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 6 Jan 2012 18:20:18 -0500 Subject: [PATCH] * Fixed favorites deletion * Fixed parenting when dealing with post deletion --- app/models/favorite.rb | 5 ++ app/models/post.rb | 18 +++--- app/models/user.rb | 4 +- .../posts/partials/show/_notices.html.erb | 6 ++ test/unit/post_test.rb | 57 ++++++++++++------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 198bbdccc..24a2ba940 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index 05506c457..9bff461a4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 1b181b5d1..a578287af 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/views/posts/partials/show/_notices.html.erb b/app/views/posts/partials/show/_notices.html.erb index 824523511..24c986ad0 100644 --- a/app/views/posts/partials/show/_notices.html.erb +++ b/app/views/posts/partials/show/_notices.html.erb @@ -1,3 +1,9 @@ +<% if post.is_deleted? %> +
+ This post has been deleted +
+<% end %> + <% if (post.is_flagged? || post.is_deleted?) && post.flags.any? %>
This post has been flagged for deletion: <%= post_flag_reasons(post) %> diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index d392ea092..a997e0258 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -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)