diff --git a/app/models/application_record.rb b/app/models/application_record.rb index d73c4dd7a..dfd4aeb49 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -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]) diff --git a/app/models/artist.rb b/app/models/artist.rb index e7e1b04ba..b2177b3d1 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -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") diff --git a/app/models/artist_commentary_version.rb b/app/models/artist_commentary_version.rb index f7cdbc9f7..b3adc0fab 100644 --- a/app/models/artist_commentary_version.rb +++ b/app/models/artist_commentary_version.rb @@ -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) diff --git a/app/models/artist_version.rb b/app/models/artist_version.rb index fcee3e6ea..0d946d64a 100644 --- a/app/models/artist_version.rb +++ b/app/models/artist_version.rb @@ -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)) diff --git a/app/models/ban.rb b/app/models/ban.rb index ec572db97..50466ec5f 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -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? diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index a3315361a..6a6b3dfd3 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -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)) diff --git a/app/models/comment.rb b/app/models/comment.rb index b84a2ef26..29097d414 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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]) diff --git a/app/models/dmail.rb b/app/models/dmail.rb index f38ef4818..e2c0eca09 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -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]) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index f6bcf5fa2..8c711a2cc 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -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 diff --git a/app/models/janitor_trial.rb b/app/models/janitor_trial.rb index 37b5a0d24..15ec98678 100644 --- a/app/models/janitor_trial.rb +++ b/app/models/janitor_trial.rb @@ -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 diff --git a/app/models/mod_action.rb b/app/models/mod_action.rb index fc28d0ee7..a47f3ea17 100644 --- a/app/models/mod_action.rb +++ b/app/models/mod_action.rb @@ -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]) diff --git a/app/models/note.rb b/app/models/note.rb index ed6219669..ddf05457e 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -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 diff --git a/app/models/note_version.rb b/app/models/note_version.rb index 56696c20a..bce8ac3c3 100644 --- a/app/models/note_version.rb +++ b/app/models/note_version.rb @@ -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 diff --git a/app/models/pool.rb b/app/models/pool.rb index a75d31555..a84e20226 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -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 diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb index ac2f20dae..385fc3221 100644 --- a/app/models/pool_version.rb +++ b/app/models/pool_version.rb @@ -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) diff --git a/app/models/post_appeal.rb b/app/models/post_appeal.rb index c52afc21f..6a1ac0dd8 100644 --- a/app/models/post_appeal.rb +++ b/app/models/post_appeal.rb @@ -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)) diff --git a/app/models/post_approval.rb b/app/models/post_approval.rb index 3300c90e8..8f5327fdd 100644 --- a/app/models/post_approval.rb +++ b/app/models/post_approval.rb @@ -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) diff --git a/app/models/post_disapproval.rb b/app/models/post_disapproval.rb index fd2e26676..d85e102e9 100644 --- a/app/models/post_disapproval.rb +++ b/app/models/post_disapproval.rb @@ -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? diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index 014f4bf87..eaba5cc18 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -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)) diff --git a/app/models/upload.rb b/app/models/upload.rb index f535baa61..4c2c9c1a8 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -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]) diff --git a/app/models/user.rb b/app/models/user.rb index a015816fc..4a4e93020 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index 60d41f160..4583589dd 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -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]) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index e6c12e684..a893ab9ec 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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 diff --git a/app/models/wiki_page_version.rb b/app/models/wiki_page_version.rb index e5ed4a4b0..1770ee81c 100644 --- a/app/models/wiki_page_version.rb +++ b/app/models/wiki_page_version.rb @@ -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 diff --git a/app/views/moderator/post/disapprovals/index.html.erb b/app/views/moderator/post/disapprovals/index.html.erb index c28f0fca7..092c9da15 100644 --- a/app/views/moderator/post/disapprovals/index.html.erb +++ b/app/views/moderator/post/disapprovals/index.html.erb @@ -3,7 +3,7 @@

Disapprovals

<%= 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] } %> diff --git a/app/views/users/search.html.erb b/app/views/users/search.html.erb index da0b82c1a..830769a54 100644 --- a/app/views/users/search.html.erb +++ b/app/views/users/search.html.erb @@ -2,9 +2,7 @@