From 88ac91f5f3e4a7d3a7871a657694b2b0287a8db4 Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Sep 2022 04:16:28 -0500 Subject: [PATCH] search: refactor to pass in the current user explicitly. --- app/controllers/legacy_controller.rb | 2 +- app/controllers/pools_controller.rb | 2 +- app/logical/autocomplete_service.rb | 10 ++--- app/logical/concerns/searchable.rb | 8 ++-- app/models/ai_tag.rb | 4 +- app/models/api_key.rb | 4 +- app/models/application_record.rb | 5 ++- app/models/artist.rb | 4 +- app/models/artist_commentary.rb | 4 +- app/models/artist_commentary_version.rb | 4 +- app/models/artist_url.rb | 4 +- app/models/artist_version.rb | 4 +- app/models/background_job.rb | 4 +- app/models/ban.rb | 4 +- app/models/bulk_update_request.rb | 4 +- app/models/comment.rb | 4 +- app/models/comment_vote.rb | 4 +- app/models/dmail.rb | 4 +- app/models/dtext_link.rb | 4 +- app/models/email_address.rb | 4 +- app/models/favorite.rb | 4 +- app/models/favorite_group.rb | 4 +- app/models/forum_post.rb | 4 +- app/models/forum_post_vote.rb | 10 ++--- app/models/forum_topic.rb | 4 +- app/models/forum_topic_visit.rb | 4 +- app/models/ip_ban.rb | 4 +- app/models/ip_geolocation.rb | 4 +- app/models/media_asset.rb | 6 +-- app/models/media_metadata.rb | 4 +- app/models/mod_action.rb | 4 +- app/models/moderation_report.rb | 4 +- app/models/news_update.rb | 4 +- app/models/note.rb | 4 +- app/models/note_version.rb | 4 +- app/models/pixiv_ugoira_frame_data.rb | 4 +- app/models/pool.rb | 4 +- app/models/pool_version.rb | 4 +- app/models/post.rb | 15 ++++--- app/models/post_appeal.rb | 4 +- app/models/post_approval.rb | 4 +- app/models/post_disapproval.rb | 4 +- app/models/post_flag.rb | 4 +- app/models/post_replacement.rb | 4 +- app/models/post_version.rb | 4 +- app/models/post_vote.rb | 4 +- app/models/rate_limit.rb | 4 +- app/models/saved_search.rb | 4 +- app/models/tag.rb | 4 +- app/models/tag_implication.rb | 2 +- app/models/tag_relationship.rb | 4 +- app/models/tag_version.rb | 4 +- app/models/upgrade_code.rb | 4 +- app/models/upload.rb | 4 +- app/models/upload_media_asset.rb | 4 +- app/models/user.rb | 7 +-- app/models/user_action.rb | 4 +- app/models/user_event.rb | 4 +- app/models/user_feedback.rb | 4 +- app/models/user_name_change_request.rb | 4 +- app/models/user_session.rb | 4 +- app/models/user_upgrade.rb | 4 +- app/models/wiki_page.rb | 4 +- app/models/wiki_page_version.rb | 4 +- test/test_helper.rb | 6 +++ test/unit/application_record.rb | 17 ------- test/unit/artist_commentary_test.rb | 16 +++---- test/unit/artist_test.rb | 59 +++++++++++++------------ test/unit/artist_url_test.rb | 6 --- test/unit/ban_test.rb | 17 +------ test/unit/bulk_update_request_test.rb | 3 +- test/unit/comment_test.rb | 8 +--- test/unit/concerns/searchable.rb | 8 +--- test/unit/dmail_test.rb | 26 ++++------- test/unit/forum_post_test.rb | 7 +-- test/unit/forum_topic_test.rb | 8 ++-- test/unit/note_test.rb | 4 +- test/unit/pool_test.rb | 28 ++++++------ test/unit/post_approval_test.rb | 3 +- test/unit/post_disapproval_test.rb | 5 +-- test/unit/user_test.rb | 6 +-- test/unit/wiki_page_test.rb | 3 +- 82 files changed, 233 insertions(+), 280 deletions(-) diff --git a/app/controllers/legacy_controller.rb b/app/controllers/legacy_controller.rb index 479e94f7d..8b0503fc7 100644 --- a/app/controllers/legacy_controller.rb +++ b/app/controllers/legacy_controller.rb @@ -20,7 +20,7 @@ class LegacyController < ApplicationController end def tags - @tags = Tag.limit(100).search(params).paginate(params[:page], :limit => params[:limit]) + @tags = Tag.limit(100).search(params, CurrentUser.user).paginate(params[:page], :limit => params[:limit]) end def unavailable diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index 8e36babcb..e6bd4e000 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -27,7 +27,7 @@ class PoolsController < ApplicationController limit = params[:limit].presence || CurrentUser.user.per_page search = search_params.presence || ActionController::Parameters.new(category: "series") - @pools = authorize Pool.search(search).paginate(params[:page], limit: limit, search_count: params[:search]) + @pools = authorize Pool.search(search, CurrentUser.user).paginate(params[:page], limit: limit, search_count: params[:search]) respond_with(@pools) end diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index d86edba71..852df877b 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -296,7 +296,7 @@ class AutocompleteService # @param string [String] the name of the pool # @return [Array] the autocomplete results def autocomplete_pool(string) - pools = Pool.undeleted.name_contains(string).search(order: "post_count").limit(limit) + pools = Pool.undeleted.name_contains(string).search({ order: "post_count" }, current_user).limit(limit) pools.map do |pool| { type: "pool", label: pool.pretty_name, value: pool.name, id: pool.id, post_count: pool.post_count, category: pool.category } @@ -307,7 +307,7 @@ class AutocompleteService # @param string [String] the name of the favgroup # @return [Array] the autocomplete results def autocomplete_favorite_group(string) - favgroups = FavoriteGroup.visible(current_user).where(creator: current_user).name_contains(string).search(order: "post_count").limit(limit) + favgroups = FavoriteGroup.visible(current_user).where(creator: current_user).name_contains(string).search({ order: "post_count" }, current_user).limit(limit) favgroups.map do |favgroup| { label: favgroup.pretty_name, value: favgroup.name, post_count: favgroup.post_count } @@ -331,7 +331,7 @@ class AutocompleteService # @return [Array] the autocomplete results def autocomplete_artist(string) string = string + "*" unless string.include?("*") - artists = Artist.undeleted.name_matches(string).search(order: "post_count").limit(limit) + artists = Artist.undeleted.name_matches(string).search({ order: "post_count" }, current_user).limit(limit) artists.map do |artist| { type: "tag", label: artist.pretty_name, value: artist.name, category: Tag.categories.artist } @@ -343,7 +343,7 @@ class AutocompleteService # @return [Array] the autocomplete results def autocomplete_wiki_page(string) string = string + "*" unless string.include?("*") - wiki_pages = WikiPage.undeleted.title_matches(string).search(order: "post_count").limit(limit) + wiki_pages = WikiPage.undeleted.title_matches(string).search({ order: "post_count" }, current_user).limit(limit) wiki_pages.map do |wiki_page| { type: "tag", label: wiki_page.pretty_title, value: wiki_page.title, category: wiki_page.tag&.category } @@ -355,7 +355,7 @@ class AutocompleteService # @return [Array] the autocomplete results def autocomplete_user(string) string = string + "*" unless string.include?("*") - users = User.search(name_matches: string, current_user_first: true, order: "post_upload_count").limit(limit) + users = User.search({ name_matches: string, current_user_first: true, order: "post_upload_count" }, current_user).limit(limit) users.map do |user| { type: "user", label: user.pretty_name, value: user.name, id: user.id, level: user.level_string.downcase } diff --git a/app/logical/concerns/searchable.rb b/app/logical/concerns/searchable.rb index d9cb41404..367c12a4e 100644 --- a/app/logical/concerns/searchable.rb +++ b/app/logical/concerns/searchable.rb @@ -202,7 +202,7 @@ module Searchable end end - def search_attributes(params, *attributes, current_user: CurrentUser.user) + def search_attributes(params, attributes, current_user:) SearchContext.new(all, params, current_user).search_attributes(attributes) end @@ -560,7 +560,7 @@ module Searchable if model == User && params["#{attr}_name"].present? name = params["#{attr}_name"] if name.include?("*") - relation = visible(relation, attr).where(attr => User.visible(current_user).search(name_matches: name).reorder(nil)) + relation = visible(relation, attr).where(attr => User.visible(current_user).search({ name_matches: name }, current_user).reorder(nil)) else relation = visible(relation, attr).where(attr => User.visible(current_user).find_by_name(name)) end @@ -581,7 +581,7 @@ module Searchable end if parameter_hash?(params[attr]) - relation = visible(relation, attr).includes(attr).references(attr).where(attr => model.visible(current_user).search(params[attr]).reorder(nil)) + relation = visible(relation, attr).includes(attr).references(attr).where(attr => model.visible(current_user).search(params[attr], current_user).reorder(nil)) end relation @@ -600,7 +600,7 @@ module Searchable return none if params["#{attr}_type"].present? && params["#{attr}_type"] != model_key model_specified = true model = Kernel.const_get(model_key) - relation = visible(relation, attr).where(attr => model.visible(current_user).search(params[model_key])) + relation = visible(relation, attr).where(attr => model.visible(current_user).search(params[model_key], current_user)) end if params["#{attr}_id"].present? diff --git a/app/models/ai_tag.rb b/app/models/ai_tag.rb index 467383e8e..613d90b08 100644 --- a/app/models/ai_tag.rb +++ b/app/models/ai_tag.rb @@ -22,8 +22,8 @@ class AITag < ApplicationRecord where(tag: Tag.find_by_name_or_alias(name)) end - def self.search(params) - q = search_attributes(params, :media_asset, :tag, :post, :score) + def self.search(params, current_user) + q = search_attributes(params, [:media_asset, :tag, :post, :score], current_user: current_user) if params[:tag_name].present? q = q.named(params[:tag_name]) diff --git a/app/models/api_key.rb b/app/models/api_key.rb index aab57fffb..b4410eca6 100644 --- a/app/models/api_key.rb +++ b/app/models/api_key.rb @@ -23,8 +23,8 @@ class ApiKey < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :key, :user) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :key, :user], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index fccce823b..56e739462 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -27,12 +27,13 @@ class ApplicationRecord < ActiveRecord::Base # of results; assume there are too many pages to count. # @param count [Integer] the precalculated number of search results, or nil to calculate it # @param defaults [Hash] The default params for the search - def paginated_search(params, page: params[:page], limit: params[:limit], count_pages: params[:search].present?, count: nil, defaults: {}) + # @param current_user [User] The user performing the search + def paginated_search(params, page: params[:page], limit: params[:limit], count_pages: params[:search].present?, count: nil, defaults: {}, current_user: CurrentUser.user) search_params = params.fetch(:search, {}).permit! search_params = defaults.merge(search_params).with_indifferent_access max_limit = (params[:format] == "sitemap") ? 10_000 : 1_000 - search(search_params).paginate(page, limit: limit, max_limit: max_limit, count: count, search_count: count_pages) + search(search_params, current_user).paginate(page, limit: limit, max_limit: max_limit, count: count, search_count: count_pages) end end end diff --git a/app/models/artist.rb b/app/models/artist.rb index 257cd6c17..3175979f7 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -277,8 +277,8 @@ class Artist < ApplicationRecord end end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :other_names, :urls, :wiki_page, :tag_alias, :tag) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :other_names, :urls, :wiki_page, :tag_alias, :tag], current_user: current_user) if params[:any_other_name_like] q = q.any_other_name_like(params[:any_other_name_like]) diff --git a/app/models/artist_commentary.rb b/app/models/artist_commentary.rb index fea1c48b4..a3c7ca05f 100644 --- a/app/models/artist_commentary.rb +++ b/app/models/artist_commentary.rb @@ -32,8 +32,8 @@ class ArtistCommentary < ApplicationRecord where_text_matches(%i[original_title original_description translated_title translated_description], query) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post], current_user: current_user) if params[:text_matches].present? q = q.text_matches(params[:text_matches]) diff --git a/app/models/artist_commentary_version.rb b/app/models/artist_commentary_version.rb index 46a6da6b5..bf8e863fa 100644 --- a/app/models/artist_commentary_version.rb +++ b/app/models/artist_commentary_version.rb @@ -4,8 +4,8 @@ class ArtistCommentaryVersion < ApplicationRecord belongs_to :post belongs_to_updater - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post, :updater) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post, :updater], current_user: current_user) q = q.where_text_matches(%i[original_title original_description translated_title translated_description], params[:text_matches]) q.apply_default_order(params) diff --git a/app/models/artist_url.rb b/app/models/artist_url.rb index 8597f5166..0e1131c9c 100644 --- a/app/models/artist_url.rb +++ b/app/models/artist_url.rb @@ -17,8 +17,8 @@ class ArtistURL < ApplicationRecord [is_active, url] end - def self.search(params = {}) - q = search_attributes(params, :id, :created_at, :updated_at, :url, :is_active, :artist) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :url, :is_active, :artist], current_user: current_user) q = q.urls_match(params[:url_matches]) case params[:order] diff --git a/app/models/artist_version.rb b/app/models/artist_version.rb index 9165d5aca..2bf49ca9a 100644 --- a/app/models/artist_version.rb +++ b/app/models/artist_version.rb @@ -16,8 +16,8 @@ class ArtistVersion < ApplicationRecord end module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names, :updater, :artist) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names, :updater, :artist], current_user: current_user) if params[:order] == "name" q = q.order("artist_versions.name").default_order diff --git a/app/models/background_job.rb b/app/models/background_job.rb index f501a0ba0..fd267cd69 100644 --- a/app/models/background_job.rb +++ b/app/models/background_job.rb @@ -31,8 +31,8 @@ class BackgroundJob < GoodJob::Job where_json_contains(:serialized_params, { job_class: class_name }) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :queue_name, :priority, :serialized_params, :scheduled_at, :performed_at, :finished_at, :error, :active_job_id, :concurrency_key, :cron_key, :retried_good_job_id, :cron_at) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :queue_name, :priority, :serialized_params, :scheduled_at, :performed_at, :finished_at, :error, :active_job_id, :concurrency_key, :cron_key, :retried_good_job_id, :cron_at], current_user: current_user) if params[:name].present? q = q.name_matches(params[:name]) diff --git a/app/models/ban.rb b/app/models/ban.rb index 6f3d8d304..ded13db53 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -18,8 +18,8 @@ class Ban < ApplicationRecord scope :expired, -> { where("bans.created_at + bans.duration <= ?", Time.zone.now) } scope :active, -> { unexpired } - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :duration, :reason, :user, :banner) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :duration, :reason, :user, :banner], current_user: current_user) q = q.expired if params[:expired].to_s.truthy? q = q.unexpired if params[:expired].to_s.falsy? diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index c40511ada..61d6cca9d 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -29,8 +29,8 @@ class BulkUpdateRequest < ApplicationRecord scope :has_topic, -> { where.not(forum_topic: nil) } module SearchMethods - def search(params = {}) - q = search_attributes(params, :id, :created_at, :updated_at, :script, :tags, :user, :forum_topic, :forum_post, :approver) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :script, :tags, :user, :forum_topic, :forum_post, :approver], current_user: current_user) if params[:status].present? q = q.where(status: params[:status].split(",")) diff --git a/app/models/comment.rb b/app/models/comment.rb index 616a43c19..666a46ebc 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -32,8 +32,8 @@ class Comment < ApplicationRecord ) module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score, :post, :creator, :updater) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score, :post, :creator, :updater], current_user: current_user) case params[:order] when "post_id", "post_id_desc" diff --git a/app/models/comment_vote.rb b/app/models/comment_vote.rb index f098464b2..de55d453e 100644 --- a/app/models/comment_vote.rb +++ b/app/models/comment_vote.rb @@ -24,8 +24,8 @@ class CommentVote < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :comment, :user) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :score, :is_deleted, :comment, :user], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/dmail.rb b/app/models/dmail.rb index ad46e0a22..09d6e952a 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -104,8 +104,8 @@ class Dmail < ApplicationRecord end end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_read, :is_deleted, :title, :body, :to, :from) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_read, :is_deleted, :title, :body, :to, :from], current_user: current_user) q = q.where_text_matches([:title, :body], params[:message_matches]) q = q.folder_matches(params[:folder]) diff --git a/app/models/dtext_link.rb b/app/models/dtext_link.rb index d353291d5..f3f45534b 100644 --- a/app/models/dtext_link.rb +++ b/app/models/dtext_link.rb @@ -38,8 +38,8 @@ class DtextLink < ApplicationRecord links end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :link_type, :link_target, :model, :linked_wiki, :linked_tag) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :link_type, :link_target, :model, :linked_wiki, :linked_tag], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 0f0b7f39f..d4911b251 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -46,8 +46,8 @@ class EmailAddress < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :user, :address, :normalized_address, :is_verified, :is_deliverable) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :user, :address, :normalized_address, :is_verified, :is_deliverable], current_user: current_user) q = q.restricted(params[:is_restricted]) diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 24c721cbc..9846182a8 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -20,8 +20,8 @@ class Favorite < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :post, :user) + def self.search(params, current_user) + q = search_attributes(params, [:id, :post, :user], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index af4cf28b8..a2e605646 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -41,8 +41,8 @@ class FavoriteGroup < ApplicationRecord end end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :name, :is_public, :post_ids, :creator) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :name, :is_public, :post_ids, :creator], current_user: current_user) if params[:name_contains].present? q = q.name_contains(params[:name_contains]) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 3555ec55c..3112b3d33 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -56,8 +56,8 @@ class ForumPost < ApplicationRecord where(id: dtext_links).or(where(id: bur_links)) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :body, :creator, :updater, :topic, :dtext_links, :votes, :tag_alias, :tag_implication, :bulk_update_request) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :body, :creator, :updater, :topic, :dtext_links, :votes, :tag_alias, :tag_implication, :bulk_update_request], current_user: current_user) if params[:linked_to].present? q = q.wiki_link_matches(params[:linked_to]) diff --git a/app/models/forum_post_vote.rb b/app/models/forum_post_vote.rb index b74381df1..798a58484 100644 --- a/app/models/forum_post_vote.rb +++ b/app/models/forum_post_vote.rb @@ -17,14 +17,14 @@ class ForumPostVote < ApplicationRecord all end - def self.forum_post_matches(params) + def self.forum_post_matches(params, current_user) return all if params.blank? - where(forum_post_id: ForumPost.search(params).reorder(nil).select(:id)) + where(forum_post_id: ForumPost.search(params, current_user).reorder(nil).select(:id)) end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :score, :creator, :forum_post) - q = q.forum_post_matches(params[:forum_post]) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :score, :creator, :forum_post], current_user: current_user) + q = q.forum_post_matches(params[:forum_post], current_user) q.apply_default_order(params) end diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 6d317ff35..952dc58b3 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -87,8 +87,8 @@ class ForumTopic < ApplicationRecord order(updated_at: :desc) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_sticky, :is_locked, :is_deleted, :category_id, :title, :response_count, :creator, :updater, :forum_posts, :bulk_update_requests, :tag_aliases, :tag_implications) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_sticky, :is_locked, :is_deleted, :category_id, :title, :response_count, :creator, :updater, :forum_posts, :bulk_update_requests, :tag_aliases, :tag_implications], current_user: current_user) if params[:is_private].to_s.truthy? q = q.private_only diff --git a/app/models/forum_topic_visit.rb b/app/models/forum_topic_visit.rb index 06cf8e856..efbadc4fd 100644 --- a/app/models/forum_topic_visit.rb +++ b/app/models/forum_topic_visit.rb @@ -16,8 +16,8 @@ class ForumTopicVisit < ApplicationRecord where("user_id = ? and last_read_at < ?", user.id, user.last_forum_read_at).delete_all end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic, :last_read_at) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :user, :forum_topic, :last_read_at], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/ip_ban.rb b/app/models/ip_ban.rb index 9e70a70b6..aebd3e8c1 100644 --- a/app/models/ip_ban.rb +++ b/app/models/ip_ban.rb @@ -34,8 +34,8 @@ class IpBan < ApplicationRecord true end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :ip_addr, :reason, :is_deleted, :category, :hit_count, :last_hit_at, :creator) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :ip_addr, :reason, :is_deleted, :category, :hit_count, :last_hit_at, :creator], current_user: current_user) case params[:order] when /\A(created_at|updated_at|last_hit_at)(?:_(asc|desc))?\z/i diff --git a/app/models/ip_geolocation.rb b/app/models/ip_geolocation.rb index 46f0eb4c9..f9b62d91c 100644 --- a/app/models/ip_geolocation.rb +++ b/app/models/ip_geolocation.rb @@ -16,8 +16,8 @@ class IpGeolocation < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :ip_addr, :network, :asn, :is_proxy, :latitude, :longitude, :organization, :time_zone, :continent, :country, :region, :city, :carrier) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :ip_addr, :network, :asn, :is_proxy, :latitude, :longitude, :organization, :time_zone, :continent, :country, :region, :city, :carrier], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/media_asset.rb b/app/models/media_asset.rb index 661c70e72..ab7bbbb89 100644 --- a/app/models/media_asset.rb +++ b/app/models/media_asset.rb @@ -197,11 +197,11 @@ class MediaAsset < ApplicationRecord AITagQuery.search(tag_string, relation: self, score_range: score_range) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :status, :md5, :file_ext, :file_size, :image_width, :image_height, :file_key, :is_public) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :status, :md5, :file_ext, :file_size, :image_width, :image_height, :file_key, :is_public], current_user: current_user) if params[:metadata].present? - q = q.joins(:media_metadata).merge(MediaMetadata.search(metadata: params[:metadata])) + q = q.joins(:media_metadata).merge(MediaMetadata.search({ metadata: params[:metadata] }, current_user)) end if params[:ai_tags_match].present? diff --git a/app/models/media_metadata.rb b/app/models/media_metadata.rb index 23c8d99d0..fe9c3fbdc 100644 --- a/app/models/media_metadata.rb +++ b/app/models/media_metadata.rb @@ -16,8 +16,8 @@ class MediaMetadata < ApplicationRecord attribute :metadata belongs_to :media_asset - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :media_asset, :metadata) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :media_asset, :metadata], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/mod_action.rb b/app/models/mod_action.rb index 4d70f452e..aa43c6b76 100644 --- a/app/models/mod_action.rb +++ b/app/models/mod_action.rb @@ -78,8 +78,8 @@ class ModAction < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :category, :description, :creator) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :category, :description, :creator], current_user: current_user) case params[:order] when "created_at_asc" diff --git a/app/models/moderation_report.rb b/app/models/moderation_report.rb index 1e565eaea..bf559dcf2 100644 --- a/app/models/moderation_report.rb +++ b/app/models/moderation_report.rb @@ -82,8 +82,8 @@ class ModerationReport < ApplicationRecord where(model: Comment.where(creator: user)).or(where(model: ForumPost.where(creator: user))).or(where(model: Dmail.received.where(from: user))) end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :reason, :creator, :model, :status) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :creator, :model, :status], current_user: current_user) if params[:recipient_id].present? q = q.received_by(User.search(id: params[:recipient_id])) diff --git a/app/models/news_update.rb b/app/models/news_update.rb index 394cfe4d3..8d169e821 100644 --- a/app/models/news_update.rb +++ b/app/models/news_update.rb @@ -13,8 +13,8 @@ class NewsUpdate < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :message, :creator, :updater) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :message, :creator, :updater], current_user: current_user) q.apply_default_order(params) end end diff --git a/app/models/note.rb b/app/models/note.rb index 1d39c82e1..3bde8579f 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -19,8 +19,8 @@ class Note < ApplicationRecord scope :active, -> { where(is_active: true) } module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :post) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :post], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/note_version.rb b/app/models/note_version.rb index a27bc6820..97d402565 100644 --- a/app/models/note_version.rb +++ b/app/models/note_version.rb @@ -5,8 +5,8 @@ class NoteVersion < ApplicationRecord belongs_to :note belongs_to_updater :counter_cache => "note_update_count" - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :updater, :note, :post) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :updater, :note, :post], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/pixiv_ugoira_frame_data.rb b/app/models/pixiv_ugoira_frame_data.rb index e9a872474..b54349e83 100644 --- a/app/models/pixiv_ugoira_frame_data.rb +++ b/app/models/pixiv_ugoira_frame_data.rb @@ -11,8 +11,8 @@ class PixivUgoiraFrameData < ApplicationRecord [:post] end - def self.search(params) - q = search_attributes(params, :id, :data, :content_type, :post, :md5) + def self.search(params, current_user) + q = search_attributes(params, [:id, :data, :content_type, :post, :md5], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/pool.rb b/app/models/pool.rb index b788897b9..2e40a5cd3 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -37,8 +37,8 @@ class Pool < ApplicationRecord order(updated_at: :desc) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids, :dtext_links) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids, :dtext_links], current_user: current_user) if params[:post_tags_match] q = q.post_tags_match(params[:post_tags_match]) diff --git a/app/models/pool_version.rb b/app/models/pool_version.rb index ecb263404..99bda8529 100644 --- a/app/models/pool_version.rb +++ b/app/models/pool_version.rb @@ -33,8 +33,8 @@ class PoolVersion < ApplicationRecord where_ilike(:name, name) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :pool_id, :post_ids, :added_post_ids, :removed_post_ids, :updater_id, :description, :description_changed, :name, :name_changed, :version, :is_active, :is_deleted, :category) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :pool_id, :post_ids, :added_post_ids, :removed_post_ids, :updater_id, :description, :description_changed, :name, :name_changed, :version, :is_active, :is_deleted, :category], current_user: current_user) if params[:post_id] q = q.for_post_id(params[:post_id].to_i) diff --git a/app/models/post.rb b/app/models/post.rb index fd402b73e..acc104fbe 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -533,14 +533,14 @@ class Post < ApplicationRecord in "-child", ids next if ids.blank? - children.search(id: ids).each do |post| + children.where_numeric_matches(:id, ids).each do |post| post.update!(parent_id: nil) end in "child", ids next if ids.blank? - Post.search(id: ids).where.not(id: id).limit(10).each do |post| + Post.where_numeric_matches(:id, ids).where.not(id: id).limit(10).each do |post| post.update!(parent_id: id) end @@ -1214,11 +1214,11 @@ class Post < ApplicationRecord end def note_matches(query) - where(notes: Note.search(body_matches: query).reorder(nil)) + where(notes: Note.where_text_matches(:body, query)) end def comment_matches(query) - where(comments: Comment.search(body_matches: query).reorder(nil)) + where(comments: Comment.where_text_matches(:body, query)) end def saved_search_matches(label, current_user = User.anonymous) @@ -1411,17 +1411,18 @@ class Post < ApplicationRecord post_query.with_implicit_metatags.posts end - def search(params) + def search(params, current_user) q = search_attributes( params, - :id, :created_at, :updated_at, :rating, :source, :pixiv_id, :fav_count, + [:id, :created_at, :updated_at, :rating, :source, :pixiv_id, :fav_count, :score, :up_score, :down_score, :md5, :file_ext, :file_size, :image_width, :image_height, :tag_count, :has_children, :has_active_children, :is_pending, :is_flagged, :is_deleted, :is_banned, :last_comment_bumped_at, :last_commented_at, :last_noted_at, :uploader, :approver, :parent, :artist_commentary, :flags, :appeals, :notes, :comments, :children, - :approvals, :replacements, :pixiv_ugoira_frame_data + :approvals, :replacements, :pixiv_ugoira_frame_data], + current_user: current_user ) if params[:tags].present? diff --git a/app/models/post_appeal.rb b/app/models/post_appeal.rb index ec663eab4..de0c5612d 100644 --- a/app/models/post_appeal.rb +++ b/app/models/post_appeal.rb @@ -23,8 +23,8 @@ class PostAppeal < ApplicationRecord end module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :creator, :post) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :status, :creator, :post], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/post_approval.rb b/app/models/post_approval.rb index f2dd2acf5..146cba18b 100644 --- a/app/models/post_approval.rb +++ b/app/models/post_approval.rb @@ -36,8 +36,8 @@ class PostApproval < ApplicationRecord post.uploader.upload_limit.update_limit!(is_pending, true) end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :user, :post) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :user, :post], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/post_disapproval.rb b/app/models/post_disapproval.rb index 1df0d5476..5da6e0cd9 100644 --- a/app/models/post_disapproval.rb +++ b/app/models/post_disapproval.rb @@ -34,8 +34,8 @@ class PostDisapproval < ApplicationRecord end end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :message, :reason, :post) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :message, :reason, :post], current_user: current_user) q = q.with_message if params[:has_message].to_s.truthy? q = q.without_message if params[:has_message].to_s.falsy? diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index 135328a60..a11ab2a92 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -58,8 +58,8 @@ class PostFlag < ApplicationRecord end end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :post, :creator) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :status, :post, :creator], current_user: current_user) if params[:category] q = q.category_matches(params[:category]) diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index bbebbfffe..0fa35736a 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -21,8 +21,8 @@ class PostReplacement < ApplicationRecord concerning :Search do class_methods do - def search(params = {}) - q = search_attributes(params, :id, :created_at, :updated_at, :md5, :old_md5, :file_ext, :old_file_ext, :original_url, :replacement_url, :creator, :post) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :md5, :old_md5, :file_ext, :old_file_ext, :original_url, :replacement_url, :creator, :post], current_user: current_user) q.apply_default_order(params) end end diff --git a/app/models/post_version.rb b/app/models/post_version.rb index 120b40434..e8f84d752 100644 --- a/app/models/post_version.rb +++ b/app/models/post_version.rb @@ -39,8 +39,8 @@ class PostVersion < ApplicationRecord where_ilike(:tags, tag) end - def search(params) - q = search_attributes(params, :id, :updated_at, :updater_id, :post_id, :tags, :added_tags, :removed_tags, :rating, :rating_changed, :parent_id, :parent_changed, :source, :source_changed, :version) + def search(params, current_user) + q = search_attributes(params, [:id, :updated_at, :updater_id, :post_id, :tags, :added_tags, :removed_tags, :rating, :rating_changed, :parent_id, :parent_changed, :source, :source_changed, :version], current_user: current_user) if params[:changed_tags] q = q.changed_tags_include_all(params[:changed_tags].scan(/[^[:space:]]+/)) diff --git a/app/models/post_vote.rb b/app/models/post_vote.rb index e0d69651e..841e6d252 100644 --- a/app/models/post_vote.rb +++ b/app/models/post_vote.rb @@ -31,8 +31,8 @@ class PostVote < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :user, :post) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :score, :is_deleted, :user, :post], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/rate_limit.rb b/app/models/rate_limit.rb index 6745ce4fd..4eecc7514 100644 --- a/app/models/rate_limit.rb +++ b/app/models/rate_limit.rb @@ -17,8 +17,8 @@ class RateLimit < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :limited, :points, :action, :key) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :limited, :points, :action, :key], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index 63d8af7b0..c4d547f64 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -110,8 +110,8 @@ class SavedSearch < ApplicationRecord concerning :Search do class_methods do - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :query) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :query], current_user: current_user) if params[:label] q = q.labeled(params[:label]) diff --git a/app/models/tag.rb b/app/models/tag.rb index f67a5f626..207d77d73 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -319,8 +319,8 @@ class Tag < ApplicationRecord abbreviation_matches(abbrev.escape_wildcards).order(post_count: :desc).first end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deprecated, :category, :post_count, :name, :wiki_page, :artist, :antecedent_alias, :consequent_aliases, :antecedent_implications, :consequent_implications, :dtext_links) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_deprecated, :category, :post_count, :name, :wiki_page, :artist, :antecedent_alias, :consequent_aliases, :antecedent_implications, :consequent_implications, :dtext_links], current_user: current_user) if params[:fuzzy_name_matches].present? q = q.fuzzy_name_matches(params[:fuzzy_name_matches]) diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index bcee82de8..f808ab8ad 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -45,7 +45,7 @@ class TagImplication < TagRelationship concerning :SearchMethods do class_methods do - def search(params) + def search(params, current_user) q = super if params[:implied_from].present? diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index 34df8fb15..947af139d 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -82,8 +82,8 @@ class TagRelationship < ApplicationRecord where(status: status.downcase) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :antecedent_name, :consequent_name, :reason, :creator, :approver, :forum_post, :forum_topic, :antecedent_tag, :consequent_tag, :antecedent_wiki, :consequent_wiki) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :antecedent_name, :consequent_name, :reason, :creator, :approver, :forum_post, :forum_topic, :antecedent_tag, :consequent_tag, :antecedent_wiki, :consequent_wiki], current_user: current_user) if params[:name_matches].present? q = q.name_matches(params[:name_matches]) diff --git a/app/models/tag_version.rb b/app/models/tag_version.rb index 262c3d4af..67ec16bc1 100644 --- a/app/models/tag_version.rb +++ b/app/models/tag_version.rb @@ -9,8 +9,8 @@ class TagVersion < ApplicationRecord where_like(:name, Tag.normalize_name(name)) end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :version, :name, :category, :is_deprecated, :tag, :updater, :previous_version) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :version, :name, :category, :is_deprecated, :tag, :updater, :previous_version], current_user: current_user) if params[:name_matches].present? q = q.name_matches(params[:name_matches]) diff --git a/app/models/upgrade_code.rb b/app/models/upgrade_code.rb index 698151779..ab41c53c2 100644 --- a/app/models/upgrade_code.rb +++ b/app/models/upgrade_code.rb @@ -32,8 +32,8 @@ class UpgradeCode < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :code, :status, :creator, :redeemer, :user_upgrade) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :code, :status, :creator, :redeemer, :user_upgrade], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/upload.rb b/app/models/upload.rb index 09f2f7bb3..f59fa1224 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -92,8 +92,8 @@ class Upload < ApplicationRecord where(upload_media_assets.where("upload_media_assets.upload_id = uploads.id").arel.exists) end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :source, :referer_url, :status, :media_asset_count, :uploader, :upload_media_assets, :media_assets, :posts) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :source, :referer_url, :status, :media_asset_count, :uploader, :upload_media_assets, :media_assets, :posts], current_user: current_user) if params[:ai_tags_match].present? min_score = params.fetch(:min_score, 50).to_i diff --git a/app/models/upload_media_asset.rb b/app/models/upload_media_asset.rb index 7b0e3172c..9c3d9809d 100644 --- a/app/models/upload_media_asset.rb +++ b/app/models/upload_media_asset.rb @@ -36,8 +36,8 @@ class UploadMediaAsset < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :status, :source_url, :page_url, :error, :upload, :media_asset, :post) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :status, :source_url, :page_url, :error, :upload, :media_asset, :post], current_user: current_user) if params[:is_posted].to_s.truthy? q = q.where.associated(:post) diff --git a/app/models/user.rb b/app/models/user.rb index 0427b0847..e71eb1721 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -612,18 +612,19 @@ class User < ApplicationRecord end module SearchMethods - def search(params) + def search(params, current_user) params = params.dup params[:name_matches] = params.delete(:name) if params[:name].present? q = search_attributes( params, - :id, :created_at, :updated_at, :name, :level, :post_upload_count, + [:id, :created_at, :updated_at, :name, :level, :post_upload_count, :post_update_count, :note_update_count, :favorite_count, :posts, :note_versions, :artist_commentary_versions, :post_appeals, :post_approvals, :artist_versions, :comments, :wiki_page_versions, :feedback, :forum_topics, :forum_posts, :forum_post_votes, - :tag_aliases, :tag_implications, :bans, :inviter + :tag_aliases, :tag_implications, :bans, :inviter], + current_user: current_user ) if params[:name_matches].present? diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 93b831fcd..d8d1f75bc 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -95,8 +95,8 @@ class UserAction < ApplicationRecord all end - def self.search(params) - q = search_attributes(params, :event_type, :user, :model) + def self.search(params, current_user) + q = search_attributes(params, [:event_type, :user, :model], current_user: current_user) case params[:order] when "event_at_asc" diff --git a/app/models/user_event.rb b/app/models/user_event.rb index cf362b515..baddf7e65 100644 --- a/app/models/user_event.rb +++ b/app/models/user_event.rb @@ -33,8 +33,8 @@ class UserEvent < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :category, :user, :user_session) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :category, :user, :user_session], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index ea415a136..2f1731761 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -32,8 +32,8 @@ class UserFeedback < ApplicationRecord order(created_at: :desc) end - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :category, :body, :is_deleted, :creator, :user) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :category, :body, :is_deleted, :creator, :user], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/user_name_change_request.rb b/app/models/user_name_change_request.rb index 11b59617d..494385b50 100644 --- a/app/models/user_name_change_request.rb +++ b/app/models/user_name_change_request.rb @@ -20,8 +20,8 @@ class UserNameChangeRequest < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :user, :original_name, :desired_name) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :user, :original_name, :desired_name], current_user: current_user) q.apply_default_order(params) end diff --git a/app/models/user_session.rb b/app/models/user_session.rb index ee5adaff7..acc1d6a44 100644 --- a/app/models/user_session.rb +++ b/app/models/user_session.rb @@ -17,8 +17,8 @@ class UserSession < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :session_id, :user_agent, :ip_addr, :ip_geolocation) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :session_id, :user_agent, :ip_addr, :ip_geolocation], current_user: current_user) q = q.apply_default_order(params) q end diff --git a/app/models/user_upgrade.rb b/app/models/user_upgrade.rb index 8fe15b633..a2cf35ed0 100644 --- a/app/models/user_upgrade.rb +++ b/app/models/user_upgrade.rb @@ -102,8 +102,8 @@ class UserUpgrade < ApplicationRecord end end - def self.search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :upgrade_type, :status, :transaction_id, :payment_processor, :recipient, :purchaser) + def self.search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :upgrade_type, :status, :transaction_id, :payment_processor, :recipient, :purchaser], current_user: current_user) if params[:is_gifted].to_s.truthy? q = q.gifted diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 4468332a0..240824ccc 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -61,8 +61,8 @@ class WikiPage < ApplicationRecord order(updated_at: :desc) end - def search(params = {}) - q = search_attributes(params, :id, :created_at, :updated_at, :is_locked, :is_deleted, :body, :title, :other_names, :tag, :artist, :dtext_links) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :is_locked, :is_deleted, :body, :title, :other_names, :tag, :artist, :dtext_links], current_user: current_user) q = q.where_text_matches([:title, :body], params[:title_or_body_matches]) if params[:title_normalize].present? diff --git a/app/models/wiki_page_version.rb b/app/models/wiki_page_version.rb index bcf6fbee5..896160b76 100644 --- a/app/models/wiki_page_version.rb +++ b/app/models/wiki_page_version.rb @@ -7,8 +7,8 @@ class WikiPageVersion < ApplicationRecord belongs_to :tag, primary_key: :name, foreign_key: :title, optional: true module SearchMethods - def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :title, :body, :other_names, :is_locked, :is_deleted, :updater, :wiki_page, :tag) + def search(params, current_user) + q = search_attributes(params, [:id, :created_at, :updated_at, :title, :body, :other_names, :is_locked, :is_deleted, :updater, :wiki_page, :tag], current_user: current_user) q.apply_default_order(params) end diff --git a/test/test_helper.rb b/test/test_helper.rb index d5d74871a..fd8c46007 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -64,6 +64,12 @@ class ActiveSupport::TestCase def as(user, &block) CurrentUser.scoped(user, &block) end + + def assert_search_equals(expected_results, current_user: CurrentUser.user, **params) + results = subject.class.search(params, current_user) + + assert_equal(Array(expected_results).map(&:id), results.ids) + end end class ActionDispatch::IntegrationTest diff --git a/test/unit/application_record.rb b/test/unit/application_record.rb index 0a8b2e50a..ade33efda 100644 --- a/test/unit/application_record.rb +++ b/test/unit/application_record.rb @@ -5,23 +5,6 @@ class ApplicationRecordTest < ActiveSupport::TestCase @tags = FactoryBot.create_list(:tag, 3, post_count: 1) end - context "ApplicationRecord#search" do - should "support the id param" do - assert_equal([@tags.first], Tag.search(id: @tags.first.id)) - end - - should "support ranges in the id param" do - assert_equal(@tags.reverse, Tag.search(id: ">=1")) - assert_equal(@tags.reverse, Tag.search(id: "#{@tags[0].id}..#{@tags[2].id}")) - assert_equal(@tags.reverse, Tag.search(id: @tags.map(&:id).join(","))) - end - - should "support the created_at and updated_at params" do - assert_equal(@tags.reverse, Tag.search(created_at: ">=#{@tags.first.created_at}")) - assert_equal(@tags.reverse, Tag.search(updated_at: ">=#{@tags.first.updated_at}")) - end - end - context "ApplicationRecord#parallel_each" do context "in threaded mode" do should "set CurrentUser correctly" do diff --git a/test/unit/artist_commentary_test.rb b/test/unit/artist_commentary_test.rb index 7319b073f..6ae873972 100644 --- a/test/unit/artist_commentary_test.rb +++ b/test/unit/artist_commentary_test.rb @@ -29,16 +29,16 @@ class ArtistCommentaryTest < ActiveSupport::TestCase end should "find the correct match" do - assert_equal([@artcomm1.id], ArtistCommentary.search(post_id: @post1.id.to_s).map(&:id)) - assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "foo").map(&:id)) - assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "f*").map(&:id)) - assert_equal([@artcomm1.id], ArtistCommentary.search(post_tags_match: "artcomm1").map(&:id)) + assert_search_equals(@artcomm1, post_id: @post1.id.to_s) + assert_search_equals(@artcomm1, text_matches: "foo") + assert_search_equals(@artcomm1, text_matches: "f*") + assert_search_equals(@artcomm1, post_tags_match: "artcomm1") - assert_equal([@artcomm1.id], ArtistCommentary.search(original_present: "yes").map(&:id)) - assert_equal([@artcomm2.id], ArtistCommentary.search(original_present: "no").map(&:id)) + assert_search_equals(@artcomm1, original_present: "yes") + assert_search_equals(@artcomm2, original_present: "no") - assert_equal([@artcomm1.id], ArtistCommentary.search(translated_present: "yes").map(&:id)) - assert_equal([@artcomm2.id], ArtistCommentary.search(translated_present: "no").map(&:id)) + assert_search_equals(@artcomm1, translated_present: "yes") + assert_search_equals(@artcomm2, translated_present: "no") end end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 16324b1fe..5b13ceaa0 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -2,14 +2,14 @@ require 'test_helper' class ArtistTest < ActiveSupport::TestCase def assert_artist_found(expected_name, source_url) - artists = Artist.search(url_matches: source_url).to_a + artists = Artist.search({ url_matches: source_url }, current_user: User.anonymous).to_a assert_equal(1, artists.size) assert_equal(expected_name, artists.first.name, "Testing URL: #{source_url}") end def assert_artist_not_found(source_url) - artists = Artist.search(url_matches: source_url).to_a + artists = Artist.search({ url_matches: source_url }, current_user: User.anonymous).to_a assert_equal(0, artists.size, "Testing URL: #{source_url}") end @@ -455,50 +455,51 @@ class ArtistTest < ActiveSupport::TestCase end should "search on its name should return results" do - artist = FactoryBot.create(:artist, :name => "artist") + artist1 = create(:artist, name: "artist") + artist2 = create(:artist, name: "bkub") - assert_not_nil(Artist.search(:name => "artist").first) - assert_not_nil(Artist.search(:name_like => "artist").first) - assert_not_nil(Artist.search(:any_name_matches => "artist").first) - assert_not_nil(Artist.search(:any_name_matches => "/art/").first) + assert_search_equals(artist1, name: "artist") + assert_search_equals(artist1, name_like: "artist") + assert_search_equals(artist1, any_name_matches: "artist") + assert_search_equals(artist1, any_name_matches: "/art/") end should "search on other names should return matches" do - artist = FactoryBot.create(:artist, :name => "artist", :other_names_string => "aaa ccc_ddd") + artist = create(:artist, name: "artist", other_names_string: "aaa ccc_ddd") - assert_nil(Artist.search(any_other_name_like: "*artist*").first) - assert_not_nil(Artist.search(any_other_name_like: "*aaa*").first) - assert_not_nil(Artist.search(any_other_name_like: "*ccc_ddd*").first) - assert_not_nil(Artist.search(name: "artist").first) - assert_not_nil(Artist.search(:any_name_matches => "aaa").first) - assert_not_nil(Artist.search(:any_name_matches => "/a/").first) + assert_search_equals([], any_other_name_like: "*artist*") + assert_search_equals(artist, any_other_name_like: "*aaa*") + assert_search_equals(artist, any_other_name_like: "*ccc_ddd*") + assert_search_equals(artist, name: "artist") + assert_search_equals(artist, any_name_matches: "aaa") + assert_search_equals(artist, any_name_matches: "/a/") end should "search on group name and return matches" do - cat_or_fish = FactoryBot.create(:artist, :name => "cat_or_fish") - yuu = FactoryBot.create(:artist, :name => "yuu", :group_name => "cat_or_fish") + cat_or_fish = create(:artist, name: "cat_or_fish") + yuu = create(:artist, name: "yuu", group_name: "cat_or_fish") - assert_not_nil(Artist.search(:group_name => "cat_or_fish").first) - assert_not_nil(Artist.search(:any_name_matches => "cat_or_fish").first) - assert_not_nil(Artist.search(:any_name_matches => "/cat/").first) + assert_search_equals(yuu, group_name: "cat_or_fish") + assert_search_equals([yuu, cat_or_fish], any_name_matches: "cat_or_fish") + assert_search_equals([yuu, cat_or_fish], any_name_matches: "/cat/") end should "search on url and return matches" do - bkub = FactoryBot.create(:artist, name: "bkub", url_string: "http://bkub.com") + bkub = create(:artist, name: "bkub", url_string: "http://bkub.com") - assert_equal([bkub.id], Artist.search(url_matches: "bkub").map(&:id)) - assert_equal([bkub.id], Artist.search(url_matches: "*bkub*").map(&:id)) - assert_equal([bkub.id], Artist.search(url_matches: "/rifyu|bkub/").map(&:id)) - assert_equal([bkub.id], Artist.search(url_matches: "http://bkub.com/test.jpg").map(&:id)) + assert_search_equals(bkub, url_matches: "bkub") + assert_search_equals(bkub, url_matches: "*bkub*") + assert_search_equals(bkub, url_matches: "/rifyu|bkub/") + assert_search_equals(bkub, url_matches: "http://bkub.com/test.jpg") end should "search on has_tag and return matches" do - bkub = FactoryBot.create(:artist, name: "bkub") - none = FactoryBot.create(:artist, name: "none") - post = FactoryBot.create(:post, tag_string: "bkub") + bkub = create(:artist, name: "bkub") + none = create(:artist, name: "none") + post = create(:post, tag_string: "bkub") - assert_equal(bkub.id, Artist.search(has_tag: "true").first.id) - assert_equal(none.id, Artist.search(has_tag: "false").first.id) + assert_search_equals(bkub, has_tag: "true") + assert_search_equals(none, has_tag: "false") end should "revert to prior versions" do diff --git a/test/unit/artist_url_test.rb b/test/unit/artist_url_test.rb index 6483b85fb..9336ad966 100644 --- a/test/unit/artist_url_test.rb +++ b/test/unit/artist_url_test.rb @@ -1,10 +1,6 @@ require 'test_helper' class ArtistURLTest < ActiveSupport::TestCase - def assert_search_equals(results, conditions) - assert_equal(results.map(&:id), subject.search(conditions).map(&:id)) - end - context "An artist url" do setup do CurrentUser.user = FactoryBot.create(:user) @@ -173,8 +169,6 @@ class ArtistURLTest < ActiveSupport::TestCase end context "#search method" do - subject { ArtistURL } - should "work" do @bkub = create(:artist, name: "bkub", is_deleted: false, url_string: "https://bkub.com") @masao = create(:artist, name: "masao", is_deleted: true, url_string: "-https://masao.com") diff --git a/test/unit/ban_test.rb b/test/unit/ban_test.rb index 11df1024e..668cf8e2a 100644 --- a/test/unit/ban_test.rb +++ b/test/unit/ban_test.rb @@ -51,22 +51,9 @@ class BanTest < ActiveSupport::TestCase context "Searching for a ban" do should "find a given ban" do - CurrentUser.user = FactoryBot.create(:admin_user) + ban = create(:ban) - user = FactoryBot.create(:user) - ban = FactoryBot.create(:ban, user: user) - params = { - user_name: user.name, - banner_name: ban.banner.name, - reason: ban.reason, - expired: false, - order: :id_desc - } - - bans = Ban.search(params) - - assert_equal(1, bans.length) - assert_equal(ban.id, bans.first.id) + assert_search_equals(ban, user_name: ban.user.name, banner_name: ban.banner.name, reason: ban.reason, expired: false, order: :id_desc) end context "by user id" do diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 1bf03143b..c89b06b69 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -780,8 +780,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase end should "work" do - assert_equal([@bur2.id, @bur1.id], BulkUpdateRequest.search.map(&:id)) - assert_equal([@bur1.id], BulkUpdateRequest.search(user_name: @admin.name, approver_name: @admin.name, status: "approved").map(&:id)) + assert_search_equals(@bur1, user_name: @admin.name, approver_name: @admin.name, status: "approved") end end end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 8ae12ddb3..4cdd3a092 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -141,17 +141,13 @@ class CommentTest < ActiveSupport::TestCase c2 = FactoryBot.create(:comment, :body => "aaa ddd") c3 = FactoryBot.create(:comment, :body => "eee") - matches = Comment.search(body_matches: "aaa") - assert_equal(2, matches.count) - assert_equal(c2.id, matches.all[0].id) - assert_equal(c1.id, matches.all[1].id) + assert_search_equals([c2, c1], body_matches: "aaa") end should "default to id_desc order when searched with no options specified" do comms = FactoryBot.create_list(:comment, 3) - matches = Comment.search({}) - assert_equal([comms[2].id, comms[1].id, comms[0].id], matches.map(&:id)) + assert_search_equals([comms[2], comms[1], comms[0]]) end context "that is edited by a moderator" do diff --git a/test/unit/concerns/searchable.rb b/test/unit/concerns/searchable.rb index e9ca6de5d..23446e84a 100644 --- a/test/unit/concerns/searchable.rb +++ b/test/unit/concerns/searchable.rb @@ -1,12 +1,6 @@ require 'test_helper' class SearchableTest < ActiveSupport::TestCase - def assert_search_equals(results, current_user: User.anonymous, **params) - as(current_user) do - assert_equal(Array(results).map(&:id), subject.search(**params).ids) - end - end - context "#search method" do subject { Post } @@ -19,7 +13,7 @@ class SearchableTest < ActiveSupport::TestCase context "for a nonexistent attribute" do should "raise an error" do assert_raises(ArgumentError) do - Post.search_attributes({ answer: 42 }, :answer) + Post.search_attributes({ answer: 42 }, [:answer], current_user: User.anonymous) end end end diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index 62ce43600..5f25bff0f 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -37,27 +37,19 @@ class DmailTest < ActiveSupport::TestCase context "search" do should "return results based on title contents" do - dmail = FactoryBot.create(:dmail, :title => "xxx", :owner => @user) + dmail = create(:dmail, title: "xxx", owner: @user) - matches = Dmail.search(title_matches: "x*") - assert_equal([dmail.id], matches.map(&:id)) - - matches = Dmail.search(title_matches: "X*") - assert_equal([dmail.id], matches.map(&:id)) - - matches = Dmail.search(message_matches: "xxx") - assert_equal([dmail.id], matches.map(&:id)) - - matches = Dmail.search(message_matches: "aaa") - assert(matches.empty?) + assert_search_equals(dmail, title_matches: "x*") + assert_search_equals(dmail, title_matches: "X*") + assert_search_equals(dmail, message_matches: "xxx") + assert_search_equals([], message_matches: "aaa") end should "return results based on body contents" do - dmail = FactoryBot.create(:dmail, :body => "xxx", :owner => @user) - matches = Dmail.search(message_matches: "xxx") - assert(matches.any?) - matches = Dmail.search(message_matches: "aaa") - assert(matches.empty?) + dmail = create(:dmail, body: "xxx", owner: @user) + + assert_search_equals(dmail, message_matches: "xxx") + assert_search_equals([], message_matches: "aaa") end end diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 340295493..b29b646a4 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -142,9 +142,10 @@ class ForumPostTest < ActiveSupport::TestCase end should "be searchable by body content" do - post = FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => "xxx") - assert_equal(1, ForumPost.search(body_matches: "xxx").count) - assert_equal(0, ForumPost.search(body_matches: "aaa").count) + post = create(:forum_post, topic: @topic, body: "xxx") + + assert_search_equals(post, body_matches: "xxx") + assert_search_equals([], body_matches: "aaa") end should "initialize its creator" do diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb index b7d244415..207fa4f75 100644 --- a/test/unit/forum_topic_test.rb +++ b/test/unit/forum_topic_test.rb @@ -44,13 +44,13 @@ class ForumTopicTest < ActiveSupport::TestCase end should "be searchable by title" do - assert_equal(1, ForumTopic.search(title: "xxx").count) - assert_equal(0, ForumTopic.search(title: "aaa").count) + assert_search_equals(@topic, title: "xxx") + assert_search_equals([], title: "aaa") end should "be searchable by category id" do - assert_equal(1, ForumTopic.search(:category_id => 0).count) - assert_equal(0, ForumTopic.search(:category_id => 1).count) + assert_search_equals(@topic, category_id: 0) + assert_search_equals([], category_id: 1) end should "initialize its creator" do diff --git a/test/unit/note_test.rb b/test/unit/note_test.rb index 4981ff66a..6129156f1 100644 --- a/test/unit/note_test.rb +++ b/test/unit/note_test.rb @@ -135,13 +135,13 @@ class NoteTest < ActiveSupport::TestCase context "where the body contains the string 'aaa'" do should "return a hit" do - assert_equal(1, Note.search(body_matches: "aaa").count) + assert_search_equals(@note, body_matches: "aaa") end end context "where the body contains the string 'bbb'" do should "return no hits" do - assert_equal(0, Note.search(body_matches: "bbb").count) + assert_search_equals([], body_matches: "bbb") end end end diff --git a/test/unit/pool_test.rb b/test/unit/pool_test.rb index a3ac59fe5..8e2ba5f32 100644 --- a/test/unit/pool_test.rb +++ b/test/unit/pool_test.rb @@ -18,11 +18,11 @@ class PoolTest < ActiveSupport::TestCase assert_equal(@pool.id, Pool.find_by_name("test pool").id) - assert_equal([@pool.id], Pool.search(name_contains: "test pool").map(&:id)) - assert_equal([@pool.id], Pool.search(name_contains: "tes").map(&:id)) - assert_equal([@pool.id], Pool.search(name_matches: "test pool").map(&:id)) - assert_equal([@pool.id], Pool.search(name_matches: "testing pool").map(&:id)) - assert_equal([], Pool.search(name_matches: "tes").map(&:id)) + assert_search_equals(@pool, name_contains: "test pool") + assert_search_equals(@pool, name_contains: "tes") + assert_search_equals(@pool, name_matches: "test pool") + assert_search_equals(@pool, name_matches: "testing pool") + assert_search_equals([], name_matches: "tes") end should "find pools by post id" do @@ -31,8 +31,8 @@ class PoolTest < ActiveSupport::TestCase @post1 = create(:post, tag_string: "pool:pool1") @post2 = create(:post, tag_string: "pool:pool2") - assert_equal([@pool1.id], Pool.search(post_ids_include_any: @post1.id).pluck(:id)) - assert_equal([@pool2.id, @pool1.id], Pool.search(post_ids_include_any: "#{@post1.id} #{@post2.id}").pluck(:id)) + assert_search_equals(@pool1, post_ids_include_any: @post1.id) + assert_search_equals([@pool2, @pool1], post_ids_include_any: "#{@post1.id} #{@post2.id}") end should "find pools by post id count" do @@ -41,7 +41,7 @@ class PoolTest < ActiveSupport::TestCase @post1 = create(:post, tag_string: "pool:pool1") @post2 = create(:post, tag_string: "pool:pool1") - assert_equal([@pool1.id], Pool.search(post_id_count: 2).pluck(:id)) + assert_search_equals(@pool1, post_id_count: 2) end should "find pools by post tags" do @@ -51,13 +51,13 @@ class PoolTest < ActiveSupport::TestCase @post2 = create(:post, tag_string: "pool:pool1 fumimi") @post3 = create(:post, tag_string: "pool:pool2 bkub fumimi") - assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "bkub").pluck(:id)) - assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "fumimi").pluck(:id)) - assert_equal([@pool2.id], Pool.search(post_tags_match: "bkub fumimi").pluck(:id)) + assert_search_equals([@pool2, @pool1], post_tags_match: "bkub") + assert_search_equals([@pool2, @pool1], post_tags_match: "fumimi") + assert_search_equals(@pool2, post_tags_match: "bkub fumimi") - assert_equal(2, Pool.search(post_tags_match: "bkub").count) - assert_equal(2, Pool.search(post_tags_match: "fumimi").count) - assert_equal(1, Pool.search(post_tags_match: "bkub fumimi").count) + assert_equal(2, Pool.search({ post_tags_match: "bkub" }, current_user: User.anonymous).count) + assert_equal(2, Pool.search({ post_tags_match: "fumimi" }, current_user: User.anonymous).count) + assert_equal(1, Pool.search({ post_tags_match: "bkub fumimi" }, current_user: User.anonymous).count) end end diff --git a/test/unit/post_approval_test.rb b/test/unit/post_approval_test.rb index d974501dd..6b7c7575c 100644 --- a/test/unit/post_approval_test.rb +++ b/test/unit/post_approval_test.rb @@ -95,9 +95,8 @@ class PostApprovalTest < ActiveSupport::TestCase CurrentUser.scoped(@approver) do @post.update!(tag_string: "touhou") @approval = create(:post_approval, post: @post, user: @approver) - @approvals = PostApproval.search(user_name: @approver.name, post_tags_match: "touhou", post_id: @post.id) - assert_equal([@approval.id], @approvals.map(&:id)) + assert_search_equals(@approval, user_name: @approver.name, post_tags_match: "touhou", post_id: @post.id) end end end diff --git a/test/unit/post_disapproval_test.rb b/test/unit/post_disapproval_test.rb index 92c3065a0..9ba87fbae 100644 --- a/test/unit/post_disapproval_test.rb +++ b/test/unit/post_disapproval_test.rb @@ -79,9 +79,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase disapproval1 = FactoryBot.create(:post_disapproval, user: @approver, post: @post1, reason: "breaks_rules") disapproval2 = FactoryBot.create(:post_disapproval, user: @approver, post: @post2, reason: "poor_quality", message: "bad anatomy") - assert_equal([disapproval1.id], PostDisapproval.search(reason: "breaks_rules").pluck(:id)) - assert_equal([disapproval2.id], PostDisapproval.search(message: "bad anatomy").pluck(:id)) - assert_equal([disapproval1.id, disapproval2.id], PostDisapproval.where(user: @approver).pluck(:id)) + assert_search_equals([disapproval1], reason: "breaks_rules") + assert_search_equals([disapproval2], message: "bad anatomy") end end end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 0ba75f627..39f495b1d 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -294,9 +294,9 @@ class UserTest < ActiveSupport::TestCase user2.save(validate: false) user3.save(validate: false) - assert_equal([user2.id, user1.id], User.search(name: "foo*").map(&:id)) - assert_equal([user2.id], User.search(name: "foo\*bar").map(&:id)) - assert_equal([user3.id], User.search(name: "bar\\\*baz").map(&:id)) + assert_search_equals([user2, user1], name: "foo*") + assert_search_equals(user2, name: "foo\*bar") + assert_search_equals(user3, name: "bar\\\*baz") end end diff --git a/test/unit/wiki_page_test.rb b/test/unit/wiki_page_test.rb index cc8e23d61..4a754b886 100644 --- a/test/unit/wiki_page_test.rb +++ b/test/unit/wiki_page_test.rb @@ -20,8 +20,7 @@ class WikiPageTest < ActiveSupport::TestCase end should "search other names with wildcards" do - matches = WikiPage.search(other_names_match: "fo*") - assert_equal([@wiki_page.id], matches.map(&:id)) + assert_search_equals(@wiki_page, other_names_match: "fo*") end should "create versions" do