added artists, comments

This commit is contained in:
albert
2010-02-15 13:59:58 -05:00
parent 3d9d6e229a
commit e9c2d1e636
28 changed files with 1044 additions and 7 deletions

155
app/models/artist.rb Normal file
View File

@@ -0,0 +1,155 @@
class Artist < ActiveRecord::Base
attr_accessor :updater_id, :updater_ip_addr
before_save :normalize_name
after_save :create_version
after_save :commit_url_string
validates_uniqueness_of :name
belongs_to :updater, :class_name => "User"
belongs_to :creator, :class_name => "User"
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
has_many :artist_urls, :dependent => :destroy
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name"
accepts_nested_attributes_for :wiki_page
module UrlMethods
module ClassMethods
def find_all_by_url(url)
url = ArtistUrl.normalize(url)
artists = []
while artists.empty? && url.size > 10
u = url.sub(/\/+$/, "") + "/"
u = u.to_escaped_for_sql_like.gsub(/\*/, '%') + '%'
artists += Artist.joins(:artist_urls).where(["artists.is_active = TRUE AND artist_urls.normalized_url LIKE ? ESCAPE E'\\\\'", u]).all(:order => "artists.name")
url = File.dirname(url) + "/"
end
artists.uniq_by {|x| x.name}.slice(0, 20)
end
end
def self.included(m)
m.extend(ClassMethods)
end
def commit_url_string
if @url_string
artist_urls.clear
@url_string.scan(/\S+/).each do |url|
artist_urls.create(:url => url)
end
end
end
def url_string=(string)
@url_string = string
end
def url_string
@url_string || artist_urls.map {|x| x.url}.join("\n")
end
end
module NameMethods
module ClassMethods
def normalize_name(name)
name.downcase.strip.gsub(/ /, '_')
end
end
def self.included(m)
m.extend(ClassMethods)
end
def normalize_name
self.name = Artist.normalize_name(name)
if other_names
self.other_names = other_names.split(/,/).map {|x| Artist.normalize_name(x)}.join(" ")
end
end
end
module GroupMethods
def member_names
members.map(&:name).join(", ")
end
end
module UpdaterMethods
def updater_name
User.find_name(updater_id).tr("_", " ")
end
end
module SearchMethods
def find_by_any_name(name)
build_relation(:name => name).first
end
def build_relation(params)
relation = Artist.where("is_active = TRUE")
case params[:name]
when /^http/
relation = relation.where("id IN (?)", find_all_by_url(params[:name]).map(&:id))
when /name:(.+)/
escaped_name = Artist.normalize_name($1).to_escaped_for_sql_like
relation = relation.where(["name LIKE ? ESCAPE E'\\\\'", escaped_name])
when /other:(.+)/
escaped_name = Artist.normalize_name($1)
relation = relation.where(["other_names_index @@ to_tsquery('danbooru', ?)", escaped_name])
when /group:(.+)/
escaped_name = Artist.normalize_name($1).to_escaped_for_sql_like
relation = relation.where(["group_name LIKE ? ESCAPE E'\\\\'", escaped_name])
when /./
normalized_name = Artist.normalize_name($1)
escaped_name = normalized_name.to_escaped_for_sql_like
relation = relation.where(["name LIKE ? ESCAPE E'\\\\' OR other_names_index @@ to_tsquery('danbooru', ?) OR group_name LIKE ? ESCAPE E'\\\\'", escaped_name, normalized_name, escaped_name])
end
if params[:id]
relation = relation.where(["id = ?", params[:id]])
end
relation
end
end
module VersionMethods
def create_version
ArtistVersion.create(
:artist_id => id,
:name => name,
:updater_id => updater_id,
:updater_ip_addr => updater_ip_addr,
:url_string => url_string,
:is_active => is_active,
:other_names => other_names,
:group_name => group_name
)
end
def revert_to!(version)
self.name = version.name
self.url_string = version.url_string
self.is_active = version.is_active
self.other_names = version.other_names
self.group_name = version.group_name
save
end
end
include UrlMethods
include NameMethods
include GroupMethods
include UpdaterMethods
extend SearchMethods
include VersionMethods
end

37
app/models/artist_url.rb Normal file
View File

@@ -0,0 +1,37 @@
class ArtistUrl < ActiveRecord::Base
before_save :normalize
validates_presence_of :url
belongs_to :artist
def self.normalize(url)
if url.nil?
nil
else
url.gsub!(/^http:\/\/blog\d+\.fc2/, "http://blog.fc2")
url.gsub!(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog.fc2")
url.gsub!(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog.fc2")
url.gsub!(/^http:\/\/img\d+\.pixiv\.net/, "http://img.pixiv.net")
url.gsub!(/\/+$/, "")
url + "/"
end
end
def self.normalize_for_search(url)
if url =~ /\.\w+$/ && url =~ /\w\/\w/
url = File.dirname(url)
end
url = url.gsub(/^http:\/\/blog\d+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/blog-imgs-\d+-\w+\.fc2/, "http://blog*.fc2")
url = url.gsub(/^http:\/\/img\d+\.pixiv\.net/, "http://img*.pixiv.net")
end
def normalize
self.normalized_url = self.class.normalize(url)
end
def to_s
url
end
end

View File

@@ -0,0 +1,8 @@
class ArtistVersion < ActiveRecord::Base
belongs_to :updater
belongs_to :artist
def updater_name
User.find_name(updater_id).tr("_", " ")
end
end

41
app/models/comment.rb Normal file
View File

@@ -0,0 +1,41 @@
class Comment < ActiveRecord::Base
class VotingError < Exception ; end
validates_format_of :body, :with => /\S/, :message => 'has no content'
belongs_to :post
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| {:conditions => ["body_index @@ plainto_tsquery(?)", query], :order => "id desc"}}
def update_last_commented_at
return if do_not_bump_post
comment_count = Comment.where(["post_id = ?", post_id]).count
if comment_count <= Danbooru.config.comment_threshold
execute_sql("UPDATE posts SET last_commented_at = ? WHERE id = ?", created_at, post_id)
end
end
def can_be_voted_by?(user)
!votes.exists?(["user_id = ?", user.id])
end
def vote!(user, is_positive)
if can_be_voted_by?(user)
if is_positive
increment!(:score)
else
decrement!(:score)
end
votes.create(:user_id => user.id)
else
raise VotingError.new("You have already voted for this comment")
end
end
end

View File

@@ -0,0 +1,8 @@
class CommentVote < ActiveRecord::Base
belongs_to :comment
belongs_to :user
def self.prune!
destroy_all(["created_at < ?", 14.days.ago])
end
end

View File

@@ -1,13 +0,0 @@
class Favorite
def self.table_name_for(user)
"favorites_#{user.id % 10}"
end
def self.create(user, post)
ActiveRecord::Base.connection.execute("INSERT INTO #{table_name_for(user)} (user_id, post_id) VALUES (#{user.id}, #{post.id})")
end
def self.destroy(user, post)
ActiveRecord::Base.connection.execute("DELETE FROM #{table_name_for(user)} WHERE user_id = #{user.id} AND post_id = #{post.id}")
end
end

View File

@@ -5,7 +5,7 @@ class Pool < ActiveRecord::Base
validates_format_of :name, :with => /\A[^\s;,]+\Z/, :on => :create, :message => "cannot have whitespace, commas, or semicolons"
belongs_to :creator, :class_name => "User"
belongs_to :updater, :class_name => "User"
has_many :versions, :class_name => "PoolVersion"
has_many :versions, :class_name => "PoolVersion", :dependent => :destroy
after_save :create_version
def self.create_anonymous(creator, creator_ip_addr)

View File

@@ -1,8 +1,7 @@
class Post < ActiveRecord::Base
attr_accessor :updater_id, :updater_ip_addr, :old_tag_string
belongs_to :updater, :class_name => "User"
has_one :unapproval
after_destroy :delete_files
after_destroy :delete_favorites
after_save :create_version
before_save :merge_old_tags
@@ -11,7 +10,10 @@ class Post < ActiveRecord::Base
before_save :update_tag_post_counts
before_save :set_tag_counts
has_many :versions, :class_name => "PostVersion"
belongs_to :updater, :class_name => "User"
has_one :unapproval, :dependent => :destroy
has_one :upload, :dependent => :destroy
has_many :versions, :class_name => "PostVersion", :dependent => :destroy
module FileMethods
def delete_files
@@ -225,6 +227,10 @@ class Post < ActiveRecord::Base
end
module FavoriteMethods
def delete_favorites
Favorite.destroy_all_for_post(self)
end
def add_favorite(user)
self.fav_string += " fav:#{user.name}"
self.fav_string.strip!

View File

@@ -1,6 +1,7 @@
class Tag < ActiveRecord::Base
attr_accessible :category
after_save :update_category_cache
has_one :wiki_page, :foreign_key => "name", :primary_key => "title"
scope :by_pattern, lambda {|name| where(["name LIKE ? ESCAPE E'\\\\'", name.to_escaped_for_sql_like])}
class CategoryMapping

View File

@@ -4,6 +4,7 @@ require "tmpdir"
class Upload < ActiveRecord::Base
attr_accessor :file, :image_width, :image_height, :file_ext, :md5, :file_size
belongs_to :uploader, :class_name => "User"
belongs_to :post
before_save :convert_cgi_file
def process!

View File

@@ -11,7 +11,7 @@ class User < ActiveRecord::Base
validates_inclusion_of :default_image_size, :in => %w(medium large original)
validates_confirmation_of :password
before_save :encrypt_password
after_save {|rec| Cache.put("user_name:#{rec.id}", rec.name)}
after_save :update_cache
scope :named, lambda {|name| where(["lower(name) = ?", name])}
def can_update?(object, foreign_key = :user_id)
@@ -21,7 +21,7 @@ class User < ActiveRecord::Base
module NameMethods
module ClassMethods
def find_name(user_id)
Cache.get("user_name:#{user_id}", 24.hours) do
Cache.get("un:#{user_id}") do
select_value_sql("SELECT name FROM users WHERE id = ?", user_id) || Danbooru.config.default_guest_name
end
end
@@ -34,6 +34,10 @@ class User < ActiveRecord::Base
def pretty_name
name.tr("_", " ")
end
def update_cache
Cache.put("un:#{id}", name)
end
end
module PasswordMethods

3
app/models/wiki_page.rb Normal file
View File

@@ -0,0 +1,3 @@
class WikiPage < ActiveRecord::Base
attr_accessor :updater_id, :updater_ip_addr
end