separated pool/post updates; fixed unit tests

This commit is contained in:
albert
2011-03-13 17:49:34 -04:00
parent 4717726217
commit 90a41a334b
13 changed files with 83 additions and 1334 deletions

View File

@@ -5,8 +5,10 @@ class Pool < ActiveRecord::Base
belongs_to :updater, :class_name => "User"
has_many :versions, :class_name => "PoolVersion", :dependent => :destroy, :order => "pool_versions.id ASC"
before_validation :normalize_name
before_validation :normalize_post_ids
before_validation :initialize_creator, :on => :create
after_save :create_version
after_save :update_posts
attr_accessible :name, :description, :post_ids, :is_active, :post_id_array
def self.name_to_id(name)
@@ -35,22 +37,36 @@ class Pool < ActiveRecord::Base
self.name = name.downcase
end
def normalize_post_ids
self.post_ids = post_ids.gsub(/\s\s+/, " ")
self.post_ids = post_ids.gsub(/^\s+/, "")
self.post_ids = post_ids.gsub(/\s+$/, "")
end
def revert_to!(version)
self.post_ids = version.post_ids
save
end
def update_posts
post_id_array.each do |post_id|
post = Post.find(post_id)
post.add_pool(self)
end
end
def add_post!(post)
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
self.post_ids += " #{post.id}"
self.post_ids = post_ids.strip
clear_post_id_array
save
end
def remove_post!(post)
self.post_ids = post_ids.gsub(/(?:\A| )#{post.id}(?:\Z| )/, " ")
self.post_ids = post_ids.strip
clear_post_id_array
save
end

View File

@@ -22,6 +22,7 @@ class Post < ActiveRecord::Base
has_many :notes, :dependent => :destroy
has_many :comments
has_many :children, :class_name => "Post", :foreign_key => "parent_id", :order => "posts.id"
has_many :disapprovals, :class_name => "PostDisapproval"
validates_uniqueness_of :md5
validates_presence_of :parent, :if => lambda {|rec| !rec.parent_id.nil?}
validate :validate_parent_does_not_have_a_parent
@@ -615,19 +616,24 @@ class Post < ActiveRecord::Base
end
module PoolMethods
def pools
@pools ||= begin
pool_ids = pool_string.scan(/\d+/)
Pool.where(["id in (?)", pool_ids])
end
end
def add_pool(pool)
return if pool_string =~ /(?:\A| )pool:#{pool.id}(?:\Z| )/
self.pool_string += " pool:#{pool.id}"
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.add_post!(self)
end
def remove_pool(pool)
self.pool_string.gsub!(/(?:\A| )pool:#{pool.id}(?:\Z| )/, " ")
self.pool_string.strip!
execute_sql("UPDATE posts SET pool_string = ? WHERE id = ?", pool_string, id)
pool.remove_post!(self)
end
end

View File

@@ -198,7 +198,7 @@ class Tag < ActiveRecord::Base
output[:exclude] << tag[1..-1]
elsif tag =~ /\*/
matches = Tag.by_pattern(tag).all(:select => "name", :limit => 25, :order => "post_count DESC").map(&:name)
matches = Tag.name_matches(tag).all(:select => "name", :limit => 25, :order => "post_count DESC").map(&:name)
matches = ["~no_matches~"] if matches.empty?
output[:include] += matches