* Renamed Post.find_by_tags into Post.tag_match, made into a full fledged scope

* Post.tag_match no longer takes an options hash (use other arel builders instead)
This commit is contained in:
albert
2011-01-28 17:40:22 -05:00
parent 7051b016f5
commit 2053e6ad8c
13 changed files with 35 additions and 139 deletions

View File

@@ -159,7 +159,7 @@ class Artist < ActiveRecord::Base
Artist.new.tap do |artist|
if params[:name]
artist.name = params[:name]
post = Post.find_by_tags("source:http* #{artist.name}").first
post = Post.tag_match("source:http* #{artist.name}").first
unless post.nil? || post.source.blank?
artist.url_string = post.source
end

View File

@@ -30,6 +30,8 @@ class Post < ActiveRecord::Base
scope :commented_before, lambda {|date| where("last_commented_at < ?", date).order("last_commented_at DESC")}
scope :available_for_moderation, lambda {where(["id NOT IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
scope :hidden_from_moderation, lambda {where(["id IN (SELECT pd.post_id FROM post_disapprovals pd WHERE pd.user_id = ?)", CurrentUser.id])}
scope :before_id, lambda {|id| where(["posts.id < ?", options[:before_id]])}
scope :tag_match, lambda {|query| Post.tag_match_helper(query)}
module FileMethods
def delete_files
@@ -463,15 +465,15 @@ class Post < ActiveRecord::Base
relation
end
def find_by_tags(q, options = {})
def tag_match_helper(q)
unless q.is_a?(Hash)
q = Tag.parse_query(q)
end
if q[:status] == "deleted"
relation = RemovedPost.where("TRUE")
relation = RemovedPost.scoped
else
relation = where("TRUE")
relation = Post.scoped
end
relation = add_range_relation(q[:post_id], "posts.id", relation)
@@ -487,10 +489,6 @@ class Post < ActiveRecord::Base
relation = add_range_relation(q[:character_tag_count], "posts.tag_count_character", relation)
relation = add_range_relation(q[:tag_count], "posts.tag_count", relation)
if options[:before_id]
relation = relation.where(["posts.id < ?", options[:before_id]])
end
if q[:md5].any?
relation = relation.where(["posts.md5 IN (?)", q[:md5]])
end
@@ -564,18 +562,6 @@ class Post < ActiveRecord::Base
relation = relation.order("posts.id DESC")
end
if options[:limit]
relation = relation.limit(options[:limit])
end
if options[:offset]
relation = relation.offset(options[:offset])
end
if options[:select]
relation = relation.select(options[:select])
end
relation
end
end
@@ -649,7 +635,7 @@ class Post < ActiveRecord::Base
tags = tags.to_s
count = Cache.get("pfc:#{Cache.sanitize(tags)}")
if count.nil?
count = Post.find_by_tags("#{tags}").count
count = Post.tag_match("#{tags}").count
if count > Danbooru.config.posts_per_page * 10
Cache.put("pfc:#{Cache.sanitize(tags)}", count, (count * 4).minutes)
end

View File

@@ -13,7 +13,7 @@ class RemovedPost < ActiveRecord::Base
def fast_count(tags)
count = Cache.get("rpfc:#{Cache.sanitize(tags)}")
if count.nil?
count = RemovedPost.find_by_tags("#{tags}").count
count = RemovedPost.tag_match("#{tags}").count
if count > Danbooru.config.posts_per_page * 10
Cache.put("rpfc:#{Cache.sanitize(tags)}", count, (count * 4).minutes)
end

View File

@@ -109,7 +109,7 @@ class Tag < ActiveRecord::Base
module UpdateMethods
def mass_edit(start_tags, result_tags, updater_id, updater_ip_addr)
updater = User.find(updater_id)
Post.find_by_tags(start_tags).each do |p|
Post.tag_match(start_tags).each do |p|
start = TagAlias.to_aliased(scan_tags(start_tags))
result = TagAlias.to_aliased(scan_tags(result_tags))
tags = (p.tag_array - start + result).join(" ")

View File

@@ -52,7 +52,7 @@ class TagAlias < ActiveRecord::Base
end
def update_posts
Post.find_by_tags(antecedent_name).find_each do |post|
Post.tag_match(antecedent_name).find_each do |post|
escaped_antecedent_name = Regexp.escape(antecedent_name)
fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{consequent_name} ").strip

View File

@@ -110,7 +110,7 @@ class TagImplication < ActiveRecord::Base
end
def update_posts
Post.find_by_tags(antecedent_name).find_each do |post|
Post.tag_match(antecedent_name).find_each do |post|
escaped_antecedent_name = Regexp.escape(antecedent_name)
fixed_tags = post.tag_string.sub(/(?:\A| )#{escaped_antecedent_name}(?:\Z| )/, " #{antecedent_name} #{descendant_names} ").strip
post.update_attributes(

View File

@@ -24,7 +24,7 @@ class TagSubscription < ActiveRecord::Base
def process
post_ids = tag_query_array.inject([]) do |all, tag|
all += Post.find_by_tags(tag, :limit => Danbooru.config.tag_subscription_post_limit / 3, :select => "posts.id", :order => "posts.id desc").map(&:id)
all += Post.tag_match(tag).limit(Danbooru.config.tag_subscription_post_limit / 3).select("posts.id").order("posts.id desc").map(&:id)
end
self.post_ids = post_ids.sort.reverse.slice(0, Danbooru.config.tag_subscription_post_limit).join(",")
end