updated tests, switched to rails 3.0.0rc2

This commit is contained in:
albert
2010-08-26 14:36:02 -04:00
parent 694b6f0813
commit ad39553aac
48 changed files with 898 additions and 406 deletions

View File

@@ -152,7 +152,7 @@ class Artist < ActiveRecord::Base
module FactoryMethods
def new_with_defaults(params)
returning(Artist.new) do |artist|
Artist.new.tap do |artist|
if params[:name]
artist.name = params[:name]
post = Post.find_by_tags("source:http* #{artist.name}").first

View File

@@ -5,8 +5,8 @@ class Comment < ActiveRecord::Base
belongs_to :creator, :class_name => "User"
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
after_save :update_last_commented_at
after_destroy :update_last_commented_at
attr_accessible :body
attr_accessor :do_not_bump_post
scope :recent, :order => "comments.id desc", :limit => 6
scope :search_body, lambda {|query| where("body_index @@ plainto_tsquery(?)", query).order("comments.id DESC")}
@@ -21,10 +21,30 @@ class Comment < ActiveRecord::Base
end
def update_last_commented_at
if Comment.where(["post_id = ?", post_id]).count <= Danbooru.config.comment_threshold
if Comment.where(["post_id = ?", post_id]).count <= Danbooru.config.comment_threshold && !do_not_bump_post
execute_sql("UPDATE posts SET last_commented_at = ? WHERE id = ?", created_at, post_id)
end
end
def vote!(is_positive)
if !CurrentUser.user.can_comment_vote?
raise CommentVote::Error.new("You can only vote ten times an hour on comments")
elsif !is_positive && creator.is_janitor_or_higher?
raise CommentVote::Error.new("You cannot downvote janitor/moderator/admin comments")
elsif votes.find_by_user_id(CurrentUser.user.id).nil?
if is_positive
update_attribute(:score, score + 1)
else
update_attribute(:score, score - 1)
end
votes.create(:user_id => CurrentUser.user.id)
else
raise CommentVote::Error.new("You have already voted for this comment")
end
end
end
Comment.connection.extend(PostgresExtensions)

View File

@@ -2,20 +2,10 @@ class CommentVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :comment_id
belongs_to :comment
belongs_to :user
after_save :update_comment_score
def self.prune!
destroy_all(["created_at < ?", 14.days.ago])
end
def update_comment_score
if is_positive
comment.increment!(:score)
else
comment.decrement!(:score)
end
end
end

View File

@@ -8,11 +8,11 @@ class IpBan < ActiveRecord::Base
end
def self.search(user_ids)
comments = count_by_ip_addr("comments", user_ids, "creator_id")
posts = count_by_ip_addr("post_versions", user_ids, "updater_id")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id")
pools = count_by_ip_addr("pool_updates", user_ids, "updater_id")
wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id")
comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr")
posts = count_by_ip_addr("post_versions", user_ids, "updater_id", "updater_ip_addr")
notes = count_by_ip_addr("note_versions", user_ids, "updater_id", "updater_ip_addr")
pools = count_by_ip_addr("pool_versions", user_ids, "updater_id", "updater_ip_addr")
wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id", "updater_ip_addr")
return {
"comments" => comments,
@@ -23,7 +23,7 @@ class IpBan < ActiveRecord::Base
}
end
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id")
select_all_sql("SELECT ip_addr, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY ip_addr ORDER BY count(*) DESC", user_ids)
def self.count_by_ip_addr(table, user_ids, user_id_field = "user_id", ip_addr_field = "ip_addr")
select_all_sql("SELECT #{ip_addr_field}, count(*) FROM #{table} WHERE #{user_id_field} IN (?) GROUP BY #{ip_addr_field} ORDER BY count(*) DESC", user_ids)
end
end

View File

@@ -91,7 +91,7 @@ class Note < ActiveRecord::Base
end
def self.build_relation(params)
relation = where()
relation = where("TRUE")
if !params[:query].blank?
query = params[:query].scan(/\S+/).join(" & ")

View File

@@ -36,6 +36,8 @@ class Pool < ActiveRecord::Base
end
def add_post!(post)
return if post_ids =~ /(?:\A| )#{post.id}(?:\Z| )/
self.post_ids += " #{post.id}"
self.post_ids.strip!
save

View File

@@ -183,7 +183,8 @@ class Post < ActiveRecord::Base
module ApprovalMethods
def unapprove!(reason)
raise Unapproval::Error.new("You can't unapprove a post more than once") if is_flagged?
raise Unapproval::Error.new("This post has already been flagged") if is_flagged?
raise Unapproval::Error.new("This post has already been unapproved once") unless unapproval.nil?
unapproval = create_unapproval(
:unapprover_id => CurrentUser.user.id,
@@ -194,14 +195,15 @@ class Post < ActiveRecord::Base
if unapproval.errors.any?
raise Unapproval::Error.new(unapproval.errors.full_messages.join("; "))
end
toggle!(:is_flagged)
update_attribute(:is_flagged, true)
end
def approve!
update_attributes(
:is_pending => false
)
self.is_flagged = false
self.is_pending = false
self.approver_string = "approver:#{CurrentUser.user.name}"
save!
end
end
@@ -349,7 +351,7 @@ class Post < ActiveRecord::Base
user_id = user
end
return false if fav_string =~ /fav:#{user_id}/
return false if fav_string =~ /(?:\A| )fav:#{user_id}(?:\Z| )/
self.fav_string += " fav:#{user_id}"
self.fav_string.strip!
@@ -467,9 +469,9 @@ class Post < ActiveRecord::Base
end
if q[:status] == "deleted"
relation = RemovedPost.where()
relation = RemovedPost.where("TRUE")
else
relation = where()
relation = where("TRUE")
end
relation = add_range_relation(q[:post_id], "posts.id", relation)
@@ -584,7 +586,7 @@ class Post < ActiveRecord::Base
end
def uploader_id
uploader_string[9..-1]
uploader_string[9..-1].to_i
end
def uploader_name
@@ -602,14 +604,17 @@ class Post < ActiveRecord::Base
module PoolMethods
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
@@ -635,7 +640,8 @@ class Post < ActiveRecord::Base
end
module CountMethods
def fast_count(tags)
def fast_count(tags = "")
tags = tags.to_s
count = Cache.get("pfc:#{Cache.sanitize(tags)}")
if count.nil?
count = Post.find_by_tags("#{tags}").count
@@ -645,20 +651,14 @@ class Post < ActiveRecord::Base
end
count
end
def fast_delete_count(tags)
fast_count("#{tags} status:deleted")
end
end
module CacheMethods
def expire_cache(tag_name)
if Post.fast_count("") < 1000
Cache.delete("pfc:")
Cache.delete("pfdc:")
end
Cache.delete("pfc:#{Cache.sanitize(tag_name)}")
Cache.delete("pfdc:#{Cache.sanitize(tag_name)}")
end
end
@@ -755,6 +755,7 @@ class Post < ActiveRecord::Base
decrement_tag_post_counts
execute_sql("DELETE FROM posts WHERE id = #{id}")
update_parent_on_destroy
tag_array.each {|x| expire_cache(x)}
end
end
end

View File

@@ -2,15 +2,5 @@ class PostVote < ActiveRecord::Base
class Error < Exception ; end
attr_accessor :is_positive
validates_uniqueness_of :ip_addr, :scope => :post_id
after_save :update_post_score
belongs_to :post
def update_post_score
if is_positive
post.increment!(:score)
else
post.decrement!(:score)
end
end
end

View File

@@ -1,4 +1,6 @@
class RemovedPost < ActiveRecord::Base
has_one :unapproval, :dependent => :destroy, :foreign_key => "post_id"
module RemovalMethods
def unremove!
Post.transaction do
@@ -8,6 +10,17 @@ class RemovedPost < ActiveRecord::Base
end
end
def fast_count(tags)
count = Cache.get("rpfc:#{Cache.sanitize(tags)}")
if count.nil?
count = RemovedPost.find_by_tags("#{tags}").count
if count > Danbooru.config.posts_per_page * 10
Cache.put("rpfc:#{Cache.sanitize(tags)}", count, (count * 4).minutes)
end
end
count
end
include Post::FileMethods
include Post::ImageMethods
include Post::TagMethods

View File

@@ -92,7 +92,7 @@ class Tag < ActiveRecord::Base
tag
else
returning Tag.new do |tag|
Tag.new.tap do |tag|
tag.name = name
tag.category = category
tag.save
@@ -217,22 +217,22 @@ class Tag < ActiveRecord::Base
if token =~ /\A(-uploader|uploader|-pool|pool|-fav|fav|sub|md5|-rating|rating|width|height|mpixels|score|filesize|source|id|date|order|status|tagcount|gentags|arttags|chartags|copytags):(.+)\Z/
case $1
when "-uploader"
q[:tags][:exclude] << "uploader:#{User.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "uploader:#{User.name_to_id($2)}"
when "uploader"
q[:tags][:related] << "uploader:#{User.name_to_id(token)}"
q[:tags][:related] << "uploader:#{User.name_to_id($2)}"
when "-pool"
q[:tags][:exclude] << "pool:#{Pool.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "pool:#{Pool.name_to_id($2)}"
when "pool"
q[:tags][:related] << "pool:#{Pool.name_to_id(token)}"
q[:tags][:related] << "pool:#{Pool.name_to_id($2)}"
when "-fav"
q[:tags][:exclude] << "fav:#{User.name_to_id(token[1..-1])}"
q[:tags][:exclude] << "fav:#{User.name_to_id($2)}"
when "fav"
q[:tags][:related] << "fav:#{User.name_to_id(token)}"
q[:tags][:related] << "fav:#{User.name_to_id($2)}"
when "sub"
q[:subscriptions] << $2

View File

@@ -49,6 +49,9 @@ class Upload < ActiveRecord::Base
module ConversionMethods
def process!
CurrentUser.user = uploader
CurrentUser.ip_addr = uploader_ip_addr
update_attribute(:status, "processing")
if is_downloadable?
download_from_source(temp_file_path)
@@ -70,10 +73,13 @@ class Upload < ActiveRecord::Base
update_attribute(:status, "error: " + post.errors.full_messages.join(", "))
end
rescue RuntimeError => x
ensure
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
def convert_to_post
returning Post.new do |p|
Post.new.tap do |p|
p.tag_string = tag_string
p.md5 = md5
p.file_ext = file_ext
@@ -81,8 +87,6 @@ class Upload < ActiveRecord::Base
p.image_height = image_height
p.uploader_id = uploader_id
p.uploader_ip_addr = uploader_ip_addr
p.updater_id = uploader_id
p.updater_ip_addr = uploader_ip_addr
p.rating = rating
p.source = source
p.file_size = file_size

View File

@@ -42,7 +42,7 @@ class User < ActiveRecord::Base
module ClassMethods
def name_to_id(name)
Cache.get("uni:#{Cache.sanitize(name)}") do
select_value_sql("SELECT id FROM users WHERE name = ?", name.downcase)
select_value_sql("SELECT id FROM users WHERE lower(name) = ?", name.downcase)
end
end
@@ -111,7 +111,7 @@ class User < ActiveRecord::Base
module FavoriteMethods
def favorite_posts(options = {})
favorites_table = Favorite.table_name_for(self)
favorites_table = Favorite.table_name_for(id)
before_id = options[:before]
before_id_sql_fragment = "AND favorites.id < #{before_id.to_i}" if before_id
limit = options[:limit] || 20
@@ -227,9 +227,9 @@ class User < ActiveRecord::Base
end
def upload_limit
deleted_count = Post.where("is_deleted = true and user_id = ?", id).count
deleted_count = RemovedPost.where("user_id = ?", id).count
unapproved_count = Post.where("is_pending = true and user_id = ?", id).count
approved_count = Post.where("is_flagged = false and is_deleted = false and is_pending = false and user_id = ?", id).count
approved_count = Post.where("is_flagged = false and is_pending = false and user_id = ?", id).count
limit = base_upload_limit + (approved_count / 10) - (deleted_count / 4) - unapproved_count