From 27c80ba6218678ff367a541e523324a7b11ca294 Mon Sep 17 00:00:00 2001 From: Toks Date: Tue, 17 Dec 2013 12:14:32 -0500 Subject: [PATCH] #1326: Add checkbox to move favorites --- .../moderator/post/posts_controller.rb | 2 +- app/models/post.rb | 11 ++++--- .../post/posts/confirm_delete.html.erb | 9 ++++++ test/unit/post_test.rb | 30 +++++++++++-------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/app/controllers/moderator/post/posts_controller.rb b/app/controllers/moderator/post/posts_controller.rb index 67b4cb30a..0a410cfb0 100644 --- a/app/controllers/moderator/post/posts_controller.rb +++ b/app/controllers/moderator/post/posts_controller.rb @@ -13,7 +13,7 @@ module Moderator @post = ::Post.find(params[:id]) if params[:commit] == "Delete" @post.flag!(params[:reason]) - @post.delete!(:reason => params[:reason]) + @post.delete!(:reason => params[:reason], :move_favorites => params[:move_favorites].present?) end redirect_to(post_path(@post)) end diff --git a/app/models/post.rb b/app/models/post.rb index 18b91a1cb..a16c963e4 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -658,7 +658,7 @@ class Post < ActiveRecord::Base end def favorited_users - favorited_user_ids.map {|id| User.find_by_id(id)} + favorited_user_ids.map {|id| User.find(id)} end end @@ -929,12 +929,10 @@ class Post < ActiveRecord::Base def give_favorites_to_parent return if parent.nil? - favorited_user_ids.each do |user_id| - parent.add_favorite!(User.find(user_id)) - remove_favorite!(User.find(user_id)) + favorited_users.each do |user| + remove_favorite!(user) + parent.add_favorite!(user) end - - update_column(:score, 0) end def post_is_not_its_own_parent @@ -987,6 +985,7 @@ class Post < ActiveRecord::Base update_column(:is_pending, false) update_column(:is_flagged, false) update_column(:is_banned, true) if options[:ban] || has_tag?("banned_artist") + give_favorites_to_parent if options[:move_favorites] unless options[:without_mod_action] if options[:reason] diff --git a/app/views/moderator/post/posts/confirm_delete.html.erb b/app/views/moderator/post/posts/confirm_delete.html.erb index dc2948665..ec7335ed7 100644 --- a/app/views/moderator/post/posts/confirm_delete.html.erb +++ b/app/views/moderator/post/posts/confirm_delete.html.erb @@ -5,6 +5,15 @@ <%= form_tag(delete_moderator_post_post_path, :style => "clear: both;", :class => "simple_form") do %> + <% if @post.parent_id %> +
+ +
+ <% end %> +
<%= text_area_tag "reason" %> diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index cc378dec8..bc1c478bc 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -183,24 +183,30 @@ class PostTest < ActiveSupport::TestCase context "Deleting a post with" do context "a parent" do - should "not reset the has_children flag of the parent" do + should "not reassign favorites to the parent by default" do p1 = FactoryGirl.create(:post) - c1 = FactoryGirl.create(:post, :parent_id => p1.id) - c1.delete! - p1.reload - assert_equal(true, p1.has_children?) - end - - should "not reassign favorites to the parent" do - p1 = FactoryGirl.create(:post) - c1 = FactoryGirl.create(:post, :parent_id => p1.id) - user = FactoryGirl.create(:user) + c1 = FactoryGirl.create(:post, :parent_id => p1.id, :score => 1) + user = FactoryGirl.create(:gold_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)) - assert_equal(0, c1.score) + assert_equal(2, c1.score) + assert_equal(0, p1.score) + end + + should "reassign favorites to the parent if specified" do + p1 = FactoryGirl.create(:post) + c1 = FactoryGirl.create(:post, :parent_id => p1.id, :score => 1) + user = FactoryGirl.create(:gold_user) + c1.add_favorite!(user) + c1.delete!(:move_favorites => true) + p1.reload + assert(!Favorite.exists?(:post_id => c1.id, :user_id => user.id), "Child should not still have favorites") + assert(Favorite.exists?(:post_id => p1.id, :user_id => user.id), "Parent should have favorites") + assert_equal(1, c1.score) + assert_equal(1, p1.score) end should "not update the parent's has_children flag" do