* 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

@@ -21,7 +21,7 @@ class ArtistsController < ApplicationController
@artist = Artist.find(params[:id])
if @artist
@posts = Danbooru.config.select_posts_visible_to_user(CurrentUser.user, Post.find_by_tags(@artist.name, :limit => 6))
@posts = Danbooru.config.select_posts_visible_to_user(CurrentUser.user, Post.tag_match(@artist.name).limit(6))
end
respond_with(@artist)

View File

@@ -30,7 +30,7 @@ class CommentsController < ApplicationController
private
def index_by_post
@posts = Post.find_by_tags(params[:tags]).commented_before(params[:before_date] || Time.now).limit(8)
@posts = Post.tag_match(params[:tags]).commented_before(params[:before_date] || Time.now).limit(8)
respond_with(@posts) do |format|
format.html {render :action => "index_by_post"}
end

View File

@@ -51,7 +51,7 @@ module PostSets
def load_posts
@count = Post.fast_count(tags)
@posts = Post.find_by_tags(tags, :before_id => before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
@posts = Post.tag_match(tags).before_id(before_id).all(:order => "posts.id desc", :limit => limit, :offset => offset)
end
def load_suggestions

View File

@@ -1,6 +1,6 @@
class RelatedTagCalculator
def self.find_tags(tag, limit)
Post.find_by_tags(tag, :limit => limit, :select => "posts.tag_string", :order => "posts.md5").map(&:tag_string)
Post.tag_match(tag).limit(limit).select("posts.tag_string").order("posts.md5").map(&:tag_string)
end
def self.calculate_from_sample_to_array(tags, category_constraint = nil)

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

View File

@@ -3,103 +3,13 @@ require "test_helper"
class PostsControllerTest < ActionController::TestCase
context "The posts controller" do
setup do
@users = {}
@users[:anon] = AnonymousUser.new
@users[:member] = Factory.create(:user)
CurrentUser.user = @users[:member]
CurrentUser.ip_addr = "127.0.0.1"
@users[:banned] = Factory.create(:banned_user)
@users[:priv] = Factory.create(:privileged_user)
@users[:contrib] = Factory.create(:contributor_user)
@users[:janitor] = Factory.create(:janitor_user)
@users[:mod] = Factory.create(:moderator_user)
@users[:admin] = Factory.create(:admin_user)
end
teardown do
@users = nil
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @users[:member]}
assert_response :success
end
end
context "create action" do
should "create a post" do
post :create, {:post => {:source => "", :file => upload_jpeg("#{Rails.root}/test/files/test.jpg"), :tag_string => "hoge", :rating => "s"}}, {:user_id => @users[:member].id}
p = Post.last
assert_equal("hoge", p.tag_string)
assert_equal("jpg", p.file_ext)
assert_equal("s", p.rating)
assert_equal("uploader:#{@users[:member].name}", p.uploader_string)
assert_equal(true, File.exists?(p.file_path))
assert_equal(true, File.exists?(p.preview_path))
end
end
context "update action" do
setup do
@p1 = Factory.create(:post, :tag_string => "hoge")
end
context "index action" do
should "update a post" do
put :update, {:post => {:tags => "moge", :rating => "Explicit"}, :id => @p1.id}, {:user_id => @users[:member].id}
@p1.reload
assert_equal("moge", p1.cached_tags)
assert_equal("e", p1.rating)
assert_equal(2, p1.tag_history.size)
post :update, {:post => {:rating => "Safe"}, :id => p1.id}, {:user_id => 3}
assert_equal(3, p1.tag_history.size)
p1.update_attribute(:is_rating_locked, true)
post :update, {:post => {:rating => "Questionable"}, :id => p1.id}, {:user_id => 3}
p1.reload
assert_equal("s", p1.rating)
end
end
should "update a post" do
end
should "list posts" do
get :index, {}, {:user_id => 3}
assert_response :success
get :index, {:tags => "tag1"}, {:user_id => 3}
assert_response :success
get :index, {:format => "json"}, {:user_id => 3}
assert_response :success
get :index, {:format => "xml"}, {:user_id => 3}
assert_response :success
get :index, {:tags => "-tag1"}, {:user_id => 3}
assert_response :success
end
should "list posts through an atom feed" do
get :atom, {}, {:user_id => 3}
assert_response :success
get :atom, {:tags => "tag1"}, {:user_id => 3}
assert_response :success
end
should "display a post" do
get :show, {:id => 1}, {:user_id => 3}
assert_response :success
end
end
def test_popular
get :popular_by_day, {}, {:user_id => 3}
assert_response :success
end
end

View File

@@ -417,7 +417,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaa bbb")
post3 = Factory.create(:post, :tag_string => "bbb ccc")
relation = Post.find_by_tags("aaa")
relation = Post.tag_match("aaa")
assert_equal(2, relation.count)
assert_equal(post2.id, relation.all[0].id)
assert_equal(post1.id, relation.all[1].id)
@@ -427,7 +427,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaa bbb")
post3 = Factory.create(:post, :tag_string => "bbb ccc")
relation = Post.find_by_tags("aaa bbb")
relation = Post.tag_match("aaa bbb")
assert_equal(1, relation.count)
assert_equal(post2.id, relation.first.id)
end
@@ -436,7 +436,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaa bbb")
post3 = Factory.create(:post, :tag_string => "bbb ccc")
relation = Post.find_by_tags("aaa -bbb")
relation = Post.tag_match("aaa -bbb")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -445,7 +445,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaab bbb")
post3 = Factory.create(:post, :tag_string => "bbb ccc")
relation = Post.find_by_tags("a*")
relation = Post.tag_match("a*")
assert_equal(2, relation.count)
assert_equal(post2.id, relation.all[0].id)
assert_equal(post1.id, relation.all[1].id)
@@ -455,7 +455,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :tag_string => "aaa")
post2 = Factory.create(:post, :tag_string => "aaab bbb")
post3 = Factory.create(:post, :tag_string => "bbb ccc")
relation = Post.find_by_tags("a* bbb")
relation = Post.tag_match("a* bbb")
assert_equal(1, relation.count)
assert_equal(post2.id, relation.first.id)
end
@@ -464,13 +464,13 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post)
post2 = Factory.create(:post)
post3 = Factory.create(:post)
relation = Post.find_by_tags("id:#{post2.id}")
relation = Post.tag_match("id:#{post2.id}")
assert_equal(1, relation.count)
assert_equal(post2.id, relation.first.id)
relation = Post.find_by_tags("id:>#{post2.id}")
relation = Post.tag_match("id:>#{post2.id}")
assert_equal(1, relation.count)
assert_equal(post3.id, relation.first.id)
relation = Post.find_by_tags("id:<#{post2.id}")
relation = Post.tag_match("id:<#{post2.id}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -481,7 +481,7 @@ class PostTest < ActiveSupport::TestCase
post3 = Factory.create(:post)
user = Factory.create(:user)
post1.add_favorite(user)
relation = Post.find_by_tags("fav:#{user.name}")
relation = Post.tag_match("fav:#{user.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -492,7 +492,7 @@ class PostTest < ActiveSupport::TestCase
post3 = Factory.create(:post)
pool = Factory.create(:pool)
post1.add_pool(pool)
relation = Post.find_by_tags("pool:#{pool.name}")
relation = Post.tag_match("pool:#{pool.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -506,7 +506,7 @@ class PostTest < ActiveSupport::TestCase
post3 = Factory.create(:post)
end
relation = Post.find_by_tags("uploader:#{CurrentUser.user.name}")
relation = Post.tag_match("uploader:#{CurrentUser.user.name}")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -515,7 +515,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :md5 => "abcd")
post2 = Factory.create(:post)
post3 = Factory.create(:post)
relation = Post.find_by_tags("md5:abcd")
relation = Post.tag_match("md5:abcd")
assert_equal(1, relation.count)
assert_equal(post1.id, relation.first.id)
end
@@ -524,7 +524,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :source => "abcd")
post2 = Factory.create(:post, :source => "abcdefg")
post3 = Factory.create(:post, :source => "xyz")
relation = Post.find_by_tags("source:abcde")
relation = Post.tag_match("source:abcde")
assert_equal(1, relation.count)
assert_equal(post2.id, relation.first.id)
end
@@ -535,7 +535,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :rating => "s")
post2 = Factory.create(:post, :rating => "q")
post3 = Factory.create(:post, :rating => "e")
relation = Post.find_by_tags("rating:e")
relation = Post.tag_match("rating:e")
assert_equal(1, relation.count)
assert_equal(post3.id, relation.first.id)
end
@@ -544,7 +544,7 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :rating => "s")
post2 = Factory.create(:post, :rating => "s")
post3 = Factory.create(:post, :rating => "e")
relation = Post.find_by_tags("-rating:s")
relation = Post.tag_match("-rating:s")
assert_equal(1, relation.count)
assert_equal(post3.id, relation.first.id)
end
@@ -553,11 +553,11 @@ class PostTest < ActiveSupport::TestCase
post1 = Factory.create(:post, :rating => "s")
post2 = Factory.create(:post, :rating => "s")
post3 = Factory.create(:post, :rating => "e", :score => 5, :image_width => 1000)
relation = Post.find_by_tags("order:id")
relation = Post.tag_match("order:id")
assert_equal(post1.id, relation.first.id)
relation = Post.find_by_tags("order:mpixels")
relation = Post.tag_match("order:mpixels")
assert_equal(post3.id, relation.first.id)
relation = Post.find_by_tags("order:landscape")
relation = Post.tag_match("order:landscape")
assert_equal(post3.id, relation.first.id)
end
end