diff --git a/app/models/pool.rb b/app/models/pool.rb index 63ad094b6..56c050f94 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -18,6 +18,10 @@ class Pool < ActiveRecord::Base attr_accessible :is_deleted, :as => [:janitor, :moderator, :admin] module SearchMethods + def deleted + where("is_deleted = true") + end + def undeleted where("is_deleted = false") end @@ -193,7 +197,7 @@ class Pool < ActiveRecord::Base return if is_deleted? update_attributes(:post_ids => add_number_to_string(post.id, post_ids), :post_count => post_count + 1) - post.add_pool!(self) + post.add_pool!(self, true) clear_post_id_array end @@ -202,7 +206,7 @@ class Pool < ActiveRecord::Base return if is_deleted? update_attributes(:post_ids => remove_number_from_string(post.id, post_ids), :post_count => post_count - 1) - post.remove_pool!(self) + post.remove_pool!(self, true) clear_post_id_array end @@ -231,12 +235,12 @@ class Pool < ActiveRecord::Base added.each do |post_id| post = Post.find(post_id) - post.add_pool!(self) + post.add_pool!(self, true) end removed.each do |post_id| post = Post.find(post_id) - post.remove_pool!(self) + post.remove_pool!(self, true) end self.post_count = post_id_array.size diff --git a/app/models/post.rb b/app/models/post.rb index fc5946ec7..0b753eb9e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -629,17 +629,17 @@ class Post < ActiveRecord::Base pool_string =~ /(?:\A| )pool:#{pool_id}(?:\Z| )/ end - def add_pool!(pool) + def add_pool!(pool, force = false) return if belongs_to_pool?(pool) - return if pool.is_deleted? + return if pool.is_deleted? && !force self.pool_string = "#{pool_string} pool:#{pool.id}".strip update_column(:pool_string, pool_string) unless new_record? pool.add!(self) end - def remove_pool!(pool) + def remove_pool!(pool, force = false) return unless belongs_to_pool?(pool) - return if pool.is_deleted? + return if pool.is_deleted? && !force self.pool_string = pool_string.gsub(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ").strip update_column(:pool_string, pool_string) unless new_record? pool.remove!(self) diff --git a/script/fixes/021_fix_deleted_pool_strings.rb b/script/fixes/021_fix_deleted_pool_strings.rb new file mode 100644 index 000000000..d3cb8f0d0 --- /dev/null +++ b/script/fixes/021_fix_deleted_pool_strings.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) + +ActiveRecord::Base.connection.execute("set statement_timeout = 0") + +CurrentUser.user = User.admins.first +CurrentUser.ip_addr = "127.0.0.1" + +Pool.deleted.where("post_count > 0").find_each do |pool| + Post.where("id in (?)", pool.post_id_array).tag_match("-pool:#{pool.id}").find_each do |post| + post.add_pool!(pool, true) + end +end diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index 2e6d592c2..1195da53b 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -120,6 +120,29 @@ class PoolTest < ActiveSupport::TestCase assert_equal(1, @pool.post_count) end end + + context "to a deleted pool" do + setup do + @pool.update_attribute(:is_deleted, true) + @pool.post_ids = "#{@pool.post_ids} #{@p2.id}" + @pool.synchronize! + @pool.save + @pool.reload + @p2.reload + end + + should "add the post to the pool" do + assert_equal("#{@p1.id} #{@p2.id}", @pool.post_ids) + end + + should "add the pool to the post" do + assert_equal("pool:#{@pool.id}", @p2.pool_string) + end + + should "increment the post count" do + assert_equal(2, @pool.post_count) + end + end end context "by removing a post" do