Merge pull request #4007 from evazion/fix-4004
Fix #4004: Add additional order by metatags for posts
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
class PostQueryBuilder
|
||||
attr_accessor :query_string
|
||||
attr_accessor :query_string, :read_only
|
||||
|
||||
def initialize(query_string)
|
||||
def initialize(query_string, read_only: false)
|
||||
@query_string = query_string
|
||||
@read_only = read_only
|
||||
end
|
||||
|
||||
def add_range_relation(arr, field, relation)
|
||||
@@ -84,6 +85,28 @@ class PostQueryBuilder
|
||||
relation
|
||||
end
|
||||
|
||||
def table_for_metatag(metatag)
|
||||
if metatag.in?(Tag::COUNT_METATAGS)
|
||||
metatag[/(?<table>[a-z]+)_count\z/i, :table]
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def tables_for_query(q)
|
||||
metatags = q.keys
|
||||
metatags << q[:order].remove(/_(asc|desc)\z/i) if q[:order].present?
|
||||
|
||||
tables = metatags.map { |metatag| table_for_metatag(metatag.to_s) }
|
||||
tables.compact.uniq
|
||||
end
|
||||
|
||||
def add_joins(q, relation)
|
||||
tables = tables_for_query(q)
|
||||
relation = relation.with_stats(tables)
|
||||
relation
|
||||
end
|
||||
|
||||
def hide_deleted_posts?(q)
|
||||
return false if CurrentUser.admin_mode?
|
||||
return false if q[:status].in?(%w[deleted active any all])
|
||||
@@ -91,14 +114,12 @@ class PostQueryBuilder
|
||||
return CurrentUser.user.hide_deleted_posts?
|
||||
end
|
||||
|
||||
def build(relation = nil)
|
||||
def build
|
||||
unless query_string.is_a?(Hash)
|
||||
q = Tag.parse_query(query_string)
|
||||
end
|
||||
|
||||
if relation.nil?
|
||||
relation = Post.where("true")
|
||||
end
|
||||
relation = read_only ? PostReadOnly.all : Post.all
|
||||
|
||||
if q[:tag_count].to_i > Danbooru.config.tag_query_limit
|
||||
raise ::Post::SearchError.new("You cannot search for more than #{Danbooru.config.tag_query_limit} tags at a time")
|
||||
@@ -108,6 +129,7 @@ class PostQueryBuilder
|
||||
relation = relation.where("posts.rating = 's'")
|
||||
end
|
||||
|
||||
relation = add_joins(q, relation)
|
||||
relation = add_range_relation(q[:post_id], "posts.id", relation)
|
||||
relation = add_range_relation(q[:mpixels], "posts.image_width * posts.image_height / 1000000.0", relation)
|
||||
relation = add_range_relation(q[:ratio], "ROUND(1.0 * posts.image_width / GREATEST(1, posts.image_height), 2)", relation)
|
||||
@@ -123,6 +145,10 @@ class PostQueryBuilder
|
||||
end
|
||||
relation = add_range_relation(q[:post_tag_count], "posts.tag_count", relation)
|
||||
|
||||
Tag::COUNT_METATAGS.each do |column|
|
||||
relation = add_range_relation(q[column.to_sym], "posts.#{column}", relation)
|
||||
end
|
||||
|
||||
if q[:md5]
|
||||
relation = relation.where("posts.md5": q[:md5])
|
||||
end
|
||||
@@ -545,6 +571,11 @@ class PostQueryBuilder
|
||||
when "filesize_asc"
|
||||
relation = relation.order("posts.file_size ASC")
|
||||
|
||||
when /\A(?<column>#{Tag::COUNT_METATAGS.join("|")})(_(?<direction>asc|desc))?\z/i
|
||||
column = $~[:column]
|
||||
direction = $~[:direction] || "desc"
|
||||
relation = relation.order(column => direction, :id => direction)
|
||||
|
||||
when "tagcount", "tagcount_desc"
|
||||
relation = relation.order("posts.tag_count DESC")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user