Model#search: factor out username search.

This commit is contained in:
evazion
2019-08-19 17:58:23 -05:00
parent 9a3e9747d8
commit c3ad7f6112
26 changed files with 41 additions and 189 deletions

View File

@@ -109,6 +109,18 @@ class ApplicationRecord < ActiveRecord::Base
end
end
def search_user_attribute(attr, params)
if params["#{attr}_id"]
numeric_attribute_matches("#{attr}_id", params["#{attr}_id"])
elsif params["#{attr}_name"]
where(attr => User.search(name_matches: params["#{attr}_name"]).reorder(nil))
elsif params[attr]
where(attr => User.search(params[attr]).reorder(nil))
else
all
end
end
def apply_default_order(params)
if params[:order] == "custom"
parse_ids = Tag.parse_helper(params[:id])

View File

@@ -511,14 +511,7 @@ class Artist < ApplicationRecord
q = q.attribute_matches(:is_active, params[:is_active])
q = q.attribute_matches(:is_banned, params[:is_banned])
if params[:creator_name].present?
q = q.where("artists.creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase)
end
if params[:creator_id].present?
q = q.where("artists.creator_id = ?", params[:creator_id].to_i)
end
q = q.search_user_attribute(:creator, params)
if params[:has_tag].to_s.truthy?
q = q.joins(:tag).where("tags.post_count > 0")

View File

@@ -4,10 +4,7 @@ class ArtistCommentaryVersion < ApplicationRecord
def self.search(params)
q = super
if params[:updater_id]
q = q.where("updater_id = ?", params[:updater_id].to_i)
end
q = q.search_user_attribute(:updater, params)
if params[:post_id]
q = q.where("post_id = ?", params[:post_id].to_i)

View File

@@ -14,13 +14,7 @@ class ArtistVersion < ApplicationRecord
q = q.where("name like ? escape E'\\\\'", params[:name].to_escaped_for_sql_like)
end
if params[:updater_name].present?
q = q.updater_name(params[:updater_name])
end
if params[:updater_id].present?
q = q.where(updater_id: params[:updater_id].split(",").map(&:to_i))
end
q = q.search_user_attribute(:updater, params)
if params[:artist_id].present?
q = q.where(artist_id: params[:artist_id].split(",").map(&:to_i))

View File

@@ -28,22 +28,8 @@ class Ban < ApplicationRecord
def self.search(params)
q = super
if params[:banner_name]
q = q.where("banner_id = (select _.id from users _ where lower(_.name) = ?)", params[:banner_name].mb_chars.downcase)
end
if params[:banner_id]
q = q.where("banner_id = ?", params[:banner_id].to_i)
end
if params[:user_name]
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].mb_chars.downcase)
end
if params[:user_id]
q = q.where("user_id = ?", params[:user_id].to_i)
end
q = q.search_user_attribute(:banner, params)
q = q.search_user_attribute(:user, params)
q = q.attribute_matches(:reason, params[:reason_matches])
q = q.expired if params[:expired].to_s.truthy?

View File

@@ -30,21 +30,8 @@ class BulkUpdateRequest < ApplicationRecord
def search(params = {})
q = super
if params[:user_name].present?
q = q.where(user_id: User.name_to_id(params[:user_name]))
end
if params[:user_id].present?
q = q.where(user_id: params[:user_id].split(",").map(&:to_i))
end
if params[:approver_name].present?
q = q.where(approver_id: User.name_to_id(params[:approver_name]))
end
if params[:approver_id].present?
q = q.where(approver_id: params[:approver_id].split(",").map(&:to_i))
end
q = q.search_user_attribute(:user, params)
q = q.search_user_attribute(:approver, params)
if params[:forum_topic_id].present?
q = q.where(forum_topic_id: params[:forum_topic_id].split(",").map(&:to_i))

View File

@@ -48,14 +48,7 @@ class Comment < ApplicationRecord
q = q.post_tags_match(params[:post_tags_match])
end
if params[:creator_name].present?
q = q.for_creator_name(params[:creator_name])
end
if params[:creator_id].present?
q = q.for_creator(params[:creator_id].to_i)
end
q = q.search_user_attribute(:creator, params)
q = q.attribute_matches(:is_deleted, params[:is_deleted])
q = q.attribute_matches(:is_sticky, params[:is_sticky])
q = q.attribute_matches(:do_not_bump_post, params[:do_not_bump_post])

View File

@@ -143,22 +143,8 @@ class Dmail < ApplicationRecord
q = q.attribute_matches(:title, params[:title_matches])
q = q.attribute_matches(:body, params[:message_matches], index_column: :message_index)
if params[:to_name].present?
q = q.to_name_matches(params[:to_name])
end
if params[:to_id].present?
q = q.where("to_id = ?", params[:to_id].to_i)
end
if params[:from_name].present?
q = q.from_name_matches(params[:from_name])
end
if params[:from_id].present?
q = q.where("from_id = ?", params[:from_id].to_i)
end
q = q.search_user_attribute(:to, params)
q = q.search_user_attribute(:from, params)
params[:is_spam] = false unless params[:is_spam].present?
q = q.attribute_matches(:is_spam, params[:is_spam])

View File

@@ -48,9 +48,7 @@ class ForumPost < ApplicationRecord
q = super
q = q.permitted
if params[:creator_id].present?
q = q.where("forum_posts.creator_id = ?", params[:creator_id].to_i)
end
q = q.search_user_attribute(:creator, params)
if params[:topic_id].present?
q = q.where("forum_posts.topic_id = ?", params[:topic_id].to_i)
@@ -62,10 +60,6 @@ class ForumPost < ApplicationRecord
q = q.attribute_matches(:body, params[:body_matches], index_column: :text_index)
if params[:creator_name].present?
q = q.creator_name(params[:creator_name].tr(" ", "_"))
end
if params[:topic_category_id].present?
q = q.joins(:topic).where("forum_topics.category_id = ?", params[:topic_category_id].to_i)
end

View File

@@ -9,15 +9,7 @@ class JanitorTrial < ApplicationRecord
def self.search(params)
q = super.where(status: "active")
if params[:user_name]
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].mb_chars.downcase)
end
if params[:user_id]
q = q.where("user_id = ?", params[:user_id].to_i)
end
q = q.search_user_attribute(:user, params)
q.apply_default_order(params)
end

View File

@@ -56,14 +56,7 @@ class ModAction < ApplicationRecord
q = super
q = q.attribute_matches(:description, params[:description_matches])
if params[:creator_id].present?
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:creator_name].present?
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].mb_chars.downcase)
end
q = q.search_user_attribute(:creator, params)
if params[:category].present?
q = q.attribute_matches(:category, params[:category])

View File

@@ -25,6 +25,7 @@ class Note < ApplicationRecord
q = q.attribute_matches(:body, params[:body_matches], index_column: :body_index)
q = q.attribute_matches(:is_active, params[:is_active])
q = q.search_user_attribute(:creator, params)
if params[:post_id].present?
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
@@ -34,14 +35,6 @@ class Note < ApplicationRecord
q = q.post_tags_match(params[:post_tags_match])
end
if params[:creator_name].present?
q = q.creator_name(params[:creator_name].tr(" ", "_"))
end
if params[:creator_id].present?
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
end
q.apply_default_order(params)
end
end

View File

@@ -4,10 +4,6 @@ class NoteVersion < ApplicationRecord
def self.search(params)
q = super
if params[:updater_id]
q = q.where(updater_id: params[:updater_id].split(",").map(&:to_i))
end
if params[:post_id]
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))
end
@@ -18,6 +14,7 @@ class NoteVersion < ApplicationRecord
q = q.attribute_matches(:is_active, params[:is_active])
q = q.attribute_matches(:body, params[:body_matches])
q = q.search_user_attribute(:updater, params)
q.apply_default_order(params)
end

View File

@@ -62,14 +62,7 @@ class Pool < ApplicationRecord
end
q = q.attribute_matches(:description, params[:description_matches])
if params[:creator_name].present?
q = q.where("pools.creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase)
end
if params[:creator_id].present?
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
end
q = q.search_user_attribute(:creator, params)
if params[:category] == "series"
q = q.series

View File

@@ -12,13 +12,7 @@ class PoolVersion < ApplicationRecord
def search(params)
q = super
if params[:updater_id].present?
q = q.for_user(params[:updater_id].to_i)
end
if params[:updater_name].present?
q = q.where("updater_id = (select _.id from users _ where lower(_.name) = ?)", params[:updater_name].mb_chars.downcase)
end
q = q.search_user_attribute(:updater, params)
if params[:pool_id].present?
q = q.where("pool_id = ?", params[:pool_id].to_i)

View File

@@ -30,14 +30,7 @@ class PostAppeal < ApplicationRecord
q = super
q = q.attribute_matches(:reason, params[:reason_matches])
if params[:creator_id].present?
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
end
if params[:creator_name].present?
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].mb_chars.downcase.strip.tr(" ", "_"))
end
q = q.search_user_attribute(:creator, params)
if params[:post_id].present?
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))

View File

@@ -40,13 +40,12 @@ class PostApproval < ApplicationRecord
def search(params)
q = super
params[:user_id] = User.name_to_id(params[:user_name]) if params[:user_name]
if params[:post_tags_match].present?
q = q.post_tags_match(params[:post_tags_match])
end
q = q.attribute_matches(:user_id, params[:user_id])
q = q.search_user_attribute(:user, params)
q = q.attribute_matches(:post_id, params[:post_id])
q.apply_default_order(params)

View File

@@ -55,12 +55,11 @@ class PostDisapproval < ApplicationRecord
q = super
q = q.attribute_matches(:post_id, params[:post_id])
q = q.attribute_matches(:user_id, params[:user_id])
q = q.attribute_matches(:message, params[:message_matches])
q = q.search_user_attribute(:user, params)
q = q.search_text_attribute(:message, params)
q = q.post_tags_match(params[:post_tags_match]) if params[:post_tags_match].present?
q = q.where(user_id: User.search(name_matches: params[:creator_name])) if params[:creator_name].present?
q = q.where(reason: params[:reason]) if params[:reason].present?
q = q.with_message if params[:has_message].to_s.truthy?

View File

@@ -33,14 +33,7 @@ class PostReplacement < ApplicationRecord
q = q.attribute_matches(:file_ext, params[:file_ext])
q = q.attribute_matches(:md5_was, params[:md5_was])
q = q.attribute_matches(:md5, params[:md5])
if params[:creator_id].present?
q = q.where(creator_id: params[:creator_id].split(",").map(&:to_i))
end
if params[:creator_name].present?
q = q.where(creator_id: User.name_to_id(params[:creator_name]))
end
q = q.search_user_attribute(:creator, params)
if params[:post_id].present?
q = q.where(post_id: params[:post_id].split(",").map(&:to_i))

View File

@@ -181,13 +181,7 @@ class Upload < ApplicationRecord
def search(params)
q = super
if params[:uploader_id].present?
q = q.attribute_matches(:uploader_id, params[:uploader_id])
end
if params[:uploader_name].present?
q = q.where(uploader_id: User.name_to_id(params[:uploader_name]))
end
q = q.search_user_attribute(:uploader, params)
if params[:source].present?
q = q.where(source: params[:source])

View File

@@ -710,20 +710,16 @@ class User < ApplicationRecord
q = q.search_text_attribute(:name, params)
q = q.attribute_matches(:level, params[:level])
q = q.attribute_matches(:inviter_id, params[:inviter_id])
q = q.attribute_matches(:post_upload_count, params[:post_upload_count])
q = q.attribute_matches(:post_update_count, params[:post_update_count])
q = q.attribute_matches(:note_update_count, params[:note_update_count])
q = q.attribute_matches(:favorite_count, params[:favorite_count])
q = q.search_user_attribute(:inviter, params)
if params[:name_matches].present?
q = q.where_ilike(:name, normalize_name(params[:name_matches]))
end
if params[:inviter].present?
q = q.where(inviter_id: search(params[:inviter]))
end
if params[:min_level].present?
q = q.where("level >= ?", params[:min_level].to_i)
end

View File

@@ -45,22 +45,8 @@ class UserFeedback < ApplicationRecord
q = super
q = q.attribute_matches(:body, params[:body_matches])
if params[:user_id].present?
q = q.for_user(params[:user_id].to_i)
end
if params[:user_name].present?
q = q.where("user_id = (select _.id from users _ where lower(_.name) = ?)", params[:user_name].mb_chars.downcase.strip.tr(" ", "_"))
end
if params[:creator_id].present?
q = q.where("creator_id = ?", params[:creator_id].to_i)
end
if params[:creator_name].present?
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].mb_chars.downcase.strip.tr(" ", "_"))
end
q = q.search_user_attribute(:user, params)
q = q.search_user_attribute(:creator, params)
if params[:category].present?
q = q.where("category = ?", params[:category])

View File

@@ -57,20 +57,13 @@ class WikiPage < ApplicationRecord
q = q.where("title LIKE ? ESCAPE E'\\\\'", params[:title].mb_chars.downcase.strip.tr(" ", "_").to_escaped_for_sql_like)
end
if params[:creator_id].present?
q = q.where("creator_id = ?", params[:creator_id])
end
q = q.search_user_attribute(:creator, params)
q = q.attribute_matches(:body, params[:body_matches], index_column: :body_index, ts_config: "danbooru")
if params[:other_names_match].present?
q = q.other_names_match(params[:other_names_match])
end
if params[:creator_name].present?
q = q.where("creator_id = (select _.id from users _ where lower(_.name) = ?)", params[:creator_name].tr(" ", "_").mb_chars.downcase)
end
if params[:hide_deleted].to_s.truthy?
q = q.where("is_deleted = false")
end

View File

@@ -9,10 +9,6 @@ class WikiPageVersion < ApplicationRecord
def search(params)
q = super
if params[:updater_id].present?
q = q.for_user(params[:updater_id].to_i)
end
if params[:wiki_page_id].present?
q = q.where("wiki_page_id = ?", params[:wiki_page_id].to_i)
end
@@ -21,6 +17,7 @@ class WikiPageVersion < ApplicationRecord
q = q.attribute_matches(:body, params[:body])
q = q.attribute_matches(:is_locked, params[:is_locked])
q = q.attribute_matches(:is_deleted, params[:is_deleted])
q = q.search_user_attribute(:updater, params)
q.apply_default_order(params)
end

View File

@@ -3,7 +3,7 @@
<h1>Disapprovals</h1>
<%= simple_form_for(:search, url: moderator_post_disapprovals_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :creator_name, label: "Creator", input_html: { value: params[:search][:creator_name] } %>
<%= f.input :user_name, label: "Creator", input_html: { value: params[:search][:user_name] } %>
<%= f.input :post_id, label: "Post ID", input_html: { value: params[:search][:post_id] } %>
<%= f.input :post_tags_match, label: "Tags", input_html: { value: params[:search][:post_tags_match], data: { autocomplete: "tag-query" } } %>
<%= f.input :message_matches, label: "Message", input_html: { value: params[:search][:message_matches] } %>

View File

@@ -2,9 +2,7 @@
<div id="a-search">
<%= simple_form_for(:search, url: users_path, method: :get, defaults: { required: false }, html: { class: "inline-form" }) do |f| %>
<%= f.input :name_matches, label: "Name", hint: "Use * for wildcard", input_html: { value: params[:search][:name_matches], data: { autocomplete: "user" } } %>
<%= f.simple_fields_for :inviter do |sub| %>
<%= sub.input :name_matches, label: "Inviter Name", hint: "Use * for wildcard", input_html: { value: params.dig(:search, :inviter, :name_matches), data: { autocomplete: "user" } } %>
<% end %>
<%= f.input :inviter_name, label: "Inviter Name", hint: "Use * for wildcard", input_html: { value: params.dig(:search, :inviter_name), data: { autocomplete: "user" } } %>
<%= f.input :level, collection: User.level_hash.to_a, include_blank: true, selected: params[:search][:level] %>
<%= f.input :min_level, collection: User.level_hash.to_a, include_blank: true, selected: params[:search][:min_level] %>