pools: stop using the pool_string field (#4160).

Stop using the pool_string field internally, but keep maintaining it
until we can drop it later.

* Stop using the pool_string for `pool:<name>` metatag searches.
* Stop using the pool_string in the `Post#pools` method. This is used to
  get the list of pools on post show pages.
This commit is contained in:
evazion
2019-09-06 22:03:40 -05:00
parent 3bd8a5c4e3
commit dc4d2e54b2
6 changed files with 69 additions and 42 deletions

View File

@@ -211,10 +211,38 @@ class PostQueryBuilder
end
end
if q[:pool] == "none"
relation = relation.where("posts.pool_string = ''")
elsif q[:pool] == "any"
relation = relation.where("posts.pool_string != ''")
q[:pool].to_a.each do |pool_name|
case pool_name
when "none"
relation = relation.where.not(id: Pool.select("unnest(post_ids)"))
when "any"
relation = relation.where(id: Pool.select("unnest(post_ids)"))
when "series"
relation = relation.where(id: Pool.series.select("unnest(post_ids)"))
when "collection"
relation = relation.where(id: Pool.collection.select("unnest(post_ids)"))
when /\*/
relation = relation.where(id: Pool.name_matches(pool_name).select("unnest(post_ids)"))
else
relation = relation.where(id: Pool.named(pool_name).select("unnest(post_ids)"))
end
end
q[:pool_neg].to_a.each do |pool_name|
case pool_name
when "none"
relation = relation.where(id: Pool.select("unnest(post_ids)"))
when "any"
relation = relation.where.not(id: Pool.select("unnest(post_ids)"))
when "series"
relation = relation.where.not(id: Pool.series.select("unnest(post_ids)"))
when "collection"
relation = relation.where.not(id: Pool.collection.select("unnest(post_ids)"))
when /\*/
relation = relation.where.not(id: Pool.name_matches(pool_name).select("unnest(post_ids)"))
else
relation = relation.where.not(id: Pool.named(pool_name).select("unnest(post_ids)"))
end
end
if q[:saved_searches]

View File

@@ -112,16 +112,20 @@ class Pool < ApplicationRecord
normalize_name(name).mb_chars.downcase
end
def self.find_by_name(name)
def self.named(name)
if name =~ /^\d+$/
where("pools.id = ?", name.to_i).first
where("pools.id = ?", name.to_i)
elsif name
where_ilike(:name, normalize_name_for_search(name)).first
where_ilike(:name, normalize_name_for_search(name))
else
nil
end
end
def self.find_by_name(name)
named(name).try(:first)
end
def versions
if PoolArchive.enabled?
PoolArchive.where("pool_id = ?", id).order("id asc")

View File

@@ -1020,11 +1020,7 @@ class Post < ApplicationRecord
module PoolMethods
def pools
@pools ||= begin
return Pool.none if pool_string.blank?
pool_ids = pool_string.scan(/\d+/)
Pool.where(id: pool_ids).series_first
end
Pool.where("pools.post_ids && array[?]", id).series_first
end
def has_active_pools?

View File

@@ -619,37 +619,15 @@ class Tag < ApplicationRecord
q[:disapproval_neg] << g2
when "-pool"
if g2.downcase == "none"
q[:pool] = "any"
elsif g2.downcase == "any"
q[:pool] = "none"
elsif g2.downcase == "series"
q[:tags][:exclude] << "pool:series"
elsif g2.downcase == "collection"
q[:tags][:exclude] << "pool:collection"
else
q[:tags][:exclude] << "pool:#{Pool.name_to_id(g2)}"
end
q[:pool_neg] ||= []
q[:pool_neg] << g2
when "pool"
if g2.downcase == "none"
q[:pool] = "none"
elsif g2.downcase == "any"
q[:pool] = "any"
elsif g2.downcase == "series"
q[:tags][:related] << "pool:series"
elsif g2.downcase == "collection"
q[:tags][:related] << "pool:collection"
elsif g2.include?("*")
pool_ids = Pool.search(name_matches: g2, order: "post_count").limit(Danbooru.config.tag_query_limit).pluck(:id)
q[:tags][:include] += pool_ids.map { |id| "pool:#{id}" }
else
q[:tags][:related] << "pool:#{Pool.name_to_id(g2)}"
end
q[:pool] ||= []
q[:pool] << g2
when "ordpool"
pool_id = Pool.name_to_id(g2)
q[:tags][:related] << "pool:#{pool_id}"
q[:ordpool] = pool_id
when "-favgroup"