search: refactor to pass in the current user explicitly.

This commit is contained in:
evazion
2022-09-22 04:16:28 -05:00
parent b56b6c554b
commit 88ac91f5f3
82 changed files with 233 additions and 280 deletions

View File

@@ -20,7 +20,7 @@ class LegacyController < ApplicationController
end end
def tags 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 end
def unavailable def unavailable

View File

@@ -27,7 +27,7 @@ class PoolsController < ApplicationController
limit = params[:limit].presence || CurrentUser.user.per_page limit = params[:limit].presence || CurrentUser.user.per_page
search = search_params.presence || ActionController::Parameters.new(category: "series") 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) respond_with(@pools)
end end

View File

@@ -296,7 +296,7 @@ class AutocompleteService
# @param string [String] the name of the pool # @param string [String] the name of the pool
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_pool(string) 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| pools.map do |pool|
{ type: "pool", label: pool.pretty_name, value: pool.name, id: pool.id, post_count: pool.post_count, category: pool.category } { 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 # @param string [String] the name of the favgroup
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_favorite_group(string) 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| favgroups.map do |favgroup|
{ label: favgroup.pretty_name, value: favgroup.name, post_count: favgroup.post_count } { label: favgroup.pretty_name, value: favgroup.name, post_count: favgroup.post_count }
@@ -331,7 +331,7 @@ class AutocompleteService
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_artist(string) def autocomplete_artist(string)
string = string + "*" unless string.include?("*") 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| artists.map do |artist|
{ type: "tag", label: artist.pretty_name, value: artist.name, category: Tag.categories.artist } { type: "tag", label: artist.pretty_name, value: artist.name, category: Tag.categories.artist }
@@ -343,7 +343,7 @@ class AutocompleteService
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_wiki_page(string) def autocomplete_wiki_page(string)
string = string + "*" unless string.include?("*") 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| wiki_pages.map do |wiki_page|
{ type: "tag", label: wiki_page.pretty_title, value: wiki_page.title, category: wiki_page.tag&.category } { type: "tag", label: wiki_page.pretty_title, value: wiki_page.title, category: wiki_page.tag&.category }
@@ -355,7 +355,7 @@ class AutocompleteService
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_user(string) def autocomplete_user(string)
string = string + "*" unless string.include?("*") 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| users.map do |user|
{ type: "user", label: user.pretty_name, value: user.name, id: user.id, level: user.level_string.downcase } { type: "user", label: user.pretty_name, value: user.name, id: user.id, level: user.level_string.downcase }

View File

@@ -202,7 +202,7 @@ module Searchable
end end
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) SearchContext.new(all, params, current_user).search_attributes(attributes)
end end
@@ -560,7 +560,7 @@ module Searchable
if model == User && params["#{attr}_name"].present? if model == User && params["#{attr}_name"].present?
name = params["#{attr}_name"] name = params["#{attr}_name"]
if name.include?("*") 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 else
relation = visible(relation, attr).where(attr => User.visible(current_user).find_by_name(name)) relation = visible(relation, attr).where(attr => User.visible(current_user).find_by_name(name))
end end
@@ -581,7 +581,7 @@ module Searchable
end end
if parameter_hash?(params[attr]) 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 end
relation relation
@@ -600,7 +600,7 @@ module Searchable
return none if params["#{attr}_type"].present? && params["#{attr}_type"] != model_key return none if params["#{attr}_type"].present? && params["#{attr}_type"] != model_key
model_specified = true model_specified = true
model = Kernel.const_get(model_key) 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 end
if params["#{attr}_id"].present? if params["#{attr}_id"].present?

View File

@@ -22,8 +22,8 @@ class AITag < ApplicationRecord
where(tag: Tag.find_by_name_or_alias(name)) where(tag: Tag.find_by_name_or_alias(name))
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :media_asset, :tag, :post, :score) q = search_attributes(params, [:media_asset, :tag, :post, :score], current_user: current_user)
if params[:tag_name].present? if params[:tag_name].present?
q = q.named(params[:tag_name]) q = q.named(params[:tag_name])

View File

@@ -23,8 +23,8 @@ class ApiKey < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :key, :user) q = search_attributes(params, [:id, :created_at, :updated_at, :key, :user], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -27,12 +27,13 @@ class ApplicationRecord < ActiveRecord::Base
# of results; assume there are too many pages to count. # 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 count [Integer] the precalculated number of search results, or nil to calculate it
# @param defaults [Hash] The default params for the search # @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 = params.fetch(:search, {}).permit!
search_params = defaults.merge(search_params).with_indifferent_access search_params = defaults.merge(search_params).with_indifferent_access
max_limit = (params[:format] == "sitemap") ? 10_000 : 1_000 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 end
end end

View File

@@ -277,8 +277,8 @@ class Artist < ApplicationRecord
end end
end end
def search(params) 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) 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] if params[:any_other_name_like]
q = q.any_other_name_like(params[:any_other_name_like]) q = q.any_other_name_like(params[:any_other_name_like])

View File

@@ -32,8 +32,8 @@ class ArtistCommentary < ApplicationRecord
where_text_matches(%i[original_title original_description translated_title translated_description], query) where_text_matches(%i[original_title original_description translated_title translated_description], query)
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description, :post) 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? if params[:text_matches].present?
q = q.text_matches(params[:text_matches]) q = q.text_matches(params[:text_matches])

View File

@@ -4,8 +4,8 @@ class ArtistCommentaryVersion < ApplicationRecord
belongs_to :post belongs_to :post
belongs_to_updater belongs_to_updater
def self.search(params) 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) 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 = q.where_text_matches(%i[original_title original_description translated_title translated_description], params[:text_matches])
q.apply_default_order(params) q.apply_default_order(params)

View File

@@ -17,8 +17,8 @@ class ArtistURL < ApplicationRecord
[is_active, url] [is_active, url]
end end
def self.search(params = {}) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :url, :is_active, :artist) q = search_attributes(params, [:id, :created_at, :updated_at, :url, :is_active, :artist], current_user: current_user)
q = q.urls_match(params[:url_matches]) q = q.urls_match(params[:url_matches])
case params[:order] case params[:order]

View File

@@ -16,8 +16,8 @@ class ArtistVersion < ApplicationRecord
end end
module SearchMethods module SearchMethods
def search(params) 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) 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" if params[:order] == "name"
q = q.order("artist_versions.name").default_order q = q.order("artist_versions.name").default_order

View File

@@ -31,8 +31,8 @@ class BackgroundJob < GoodJob::Job
where_json_contains(:serialized_params, { job_class: class_name }) where_json_contains(:serialized_params, { job_class: class_name })
end end
def search(params) 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) 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? if params[:name].present?
q = q.name_matches(params[:name]) q = q.name_matches(params[:name])

View File

@@ -18,8 +18,8 @@ class Ban < ApplicationRecord
scope :expired, -> { where("bans.created_at + bans.duration <= ?", Time.zone.now) } scope :expired, -> { where("bans.created_at + bans.duration <= ?", Time.zone.now) }
scope :active, -> { unexpired } scope :active, -> { unexpired }
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :duration, :reason, :user, :banner) 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.expired if params[:expired].to_s.truthy?
q = q.unexpired if params[:expired].to_s.falsy? q = q.unexpired if params[:expired].to_s.falsy?

View File

@@ -29,8 +29,8 @@ class BulkUpdateRequest < ApplicationRecord
scope :has_topic, -> { where.not(forum_topic: nil) } scope :has_topic, -> { where.not(forum_topic: nil) }
module SearchMethods module SearchMethods
def search(params = {}) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :script, :tags, :user, :forum_topic, :forum_post, :approver) 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? if params[:status].present?
q = q.where(status: params[:status].split(",")) q = q.where(status: params[:status].split(","))

View File

@@ -32,8 +32,8 @@ class Comment < ApplicationRecord
) )
module SearchMethods module SearchMethods
def search(params) 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) 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] case params[:order]
when "post_id", "post_id_desc" when "post_id", "post_id_desc"

View File

@@ -24,8 +24,8 @@ class CommentVote < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :comment, :user) q = search_attributes(params, [:id, :created_at, :updated_at, :score, :is_deleted, :comment, :user], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -104,8 +104,8 @@ class Dmail < ApplicationRecord
end end
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :is_read, :is_deleted, :title, :body, :to, :from) 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.where_text_matches([:title, :body], params[:message_matches])
q = q.folder_matches(params[:folder]) q = q.folder_matches(params[:folder])

View File

@@ -38,8 +38,8 @@ class DtextLink < ApplicationRecord
links links
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :link_type, :link_target, :model, :linked_wiki, :linked_tag) 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) q.apply_default_order(params)
end end

View File

@@ -46,8 +46,8 @@ class EmailAddress < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :user, :address, :normalized_address, :is_verified, :is_deliverable) 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]) q = q.restricted(params[:is_restricted])

View File

@@ -20,8 +20,8 @@ class Favorite < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :post, :user) q = search_attributes(params, [:id, :post, :user], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -41,8 +41,8 @@ class FavoriteGroup < ApplicationRecord
end end
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :name, :is_public, :post_ids, :creator) q = search_attributes(params, [:id, :created_at, :updated_at, :name, :is_public, :post_ids, :creator], current_user: current_user)
if params[:name_contains].present? if params[:name_contains].present?
q = q.name_contains(params[:name_contains]) q = q.name_contains(params[:name_contains])

View File

@@ -56,8 +56,8 @@ class ForumPost < ApplicationRecord
where(id: dtext_links).or(where(id: bur_links)) where(id: dtext_links).or(where(id: bur_links))
end end
def search(params) 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) 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? if params[:linked_to].present?
q = q.wiki_link_matches(params[:linked_to]) q = q.wiki_link_matches(params[:linked_to])

View File

@@ -17,14 +17,14 @@ class ForumPostVote < ApplicationRecord
all all
end end
def self.forum_post_matches(params) def self.forum_post_matches(params, current_user)
return all if params.blank? 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 end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :score, :creator, :forum_post) 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]) q = q.forum_post_matches(params[:forum_post], current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -87,8 +87,8 @@ class ForumTopic < ApplicationRecord
order(updated_at: :desc) order(updated_at: :desc)
end end
def search(params) 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) 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? if params[:is_private].to_s.truthy?
q = q.private_only q = q.private_only

View File

@@ -16,8 +16,8 @@ class ForumTopicVisit < ApplicationRecord
where("user_id = ? and last_read_at < ?", user.id, user.last_forum_read_at).delete_all where("user_id = ? and last_read_at < ?", user.id, user.last_forum_read_at).delete_all
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic, :last_read_at) q = search_attributes(params, [:id, :created_at, :updated_at, :user, :forum_topic, :last_read_at], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -34,8 +34,8 @@ class IpBan < ApplicationRecord
true true
end end
def self.search(params) 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) 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] case params[:order]
when /\A(created_at|updated_at|last_hit_at)(?:_(asc|desc))?\z/i when /\A(created_at|updated_at|last_hit_at)(?:_(asc|desc))?\z/i

View File

@@ -16,8 +16,8 @@ class IpGeolocation < ApplicationRecord
end end
end end
def self.search(params) 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) 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) q.apply_default_order(params)
end end

View File

@@ -197,11 +197,11 @@ class MediaAsset < ApplicationRecord
AITagQuery.search(tag_string, relation: self, score_range: score_range) AITagQuery.search(tag_string, relation: self, score_range: score_range)
end end
def search(params) 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) 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? 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 end
if params[:ai_tags_match].present? if params[:ai_tags_match].present?

View File

@@ -16,8 +16,8 @@ class MediaMetadata < ApplicationRecord
attribute :metadata attribute :metadata
belongs_to :media_asset belongs_to :media_asset
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :media_asset, :metadata) q = search_attributes(params, [:id, :created_at, :updated_at, :media_asset, :metadata], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -78,8 +78,8 @@ class ModAction < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :description, :creator) q = search_attributes(params, [:id, :created_at, :updated_at, :category, :description, :creator], current_user: current_user)
case params[:order] case params[:order]
when "created_at_asc" when "created_at_asc"

View File

@@ -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))) where(model: Comment.where(creator: user)).or(where(model: ForumPost.where(creator: user))).or(where(model: Dmail.received.where(from: user)))
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :creator, :model, :status) q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :creator, :model, :status], current_user: current_user)
if params[:recipient_id].present? if params[:recipient_id].present?
q = q.received_by(User.search(id: params[:recipient_id])) q = q.received_by(User.search(id: params[:recipient_id]))

View File

@@ -13,8 +13,8 @@ class NewsUpdate < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :message, :creator, :updater) q = search_attributes(params, [:id, :created_at, :updated_at, :message, :creator, :updater], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end
end end

View File

@@ -19,8 +19,8 @@ class Note < ApplicationRecord
scope :active, -> { where(is_active: true) } scope :active, -> { where(is_active: true) }
module SearchMethods module SearchMethods
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :post) 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) q.apply_default_order(params)
end end

View File

@@ -5,8 +5,8 @@ class NoteVersion < ApplicationRecord
belongs_to :note belongs_to :note
belongs_to_updater :counter_cache => "note_update_count" belongs_to_updater :counter_cache => "note_update_count"
def self.search(params) 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) 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) q.apply_default_order(params)
end end

View File

@@ -11,8 +11,8 @@ class PixivUgoiraFrameData < ApplicationRecord
[:post] [:post]
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :data, :content_type, :post, :md5) q = search_attributes(params, [:id, :data, :content_type, :post, :md5], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -37,8 +37,8 @@ class Pool < ApplicationRecord
order(updated_at: :desc) order(updated_at: :desc)
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids, :dtext_links) 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] if params[:post_tags_match]
q = q.post_tags_match(params[:post_tags_match]) q = q.post_tags_match(params[:post_tags_match])

View File

@@ -33,8 +33,8 @@ class PoolVersion < ApplicationRecord
where_ilike(:name, name) where_ilike(:name, name)
end end
def search(params) 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) 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] if params[:post_id]
q = q.for_post_id(params[:post_id].to_i) q = q.for_post_id(params[:post_id].to_i)

View File

@@ -533,14 +533,14 @@ class Post < ApplicationRecord
in "-child", ids in "-child", ids
next if ids.blank? 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) post.update!(parent_id: nil)
end end
in "child", ids in "child", ids
next if ids.blank? 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) post.update!(parent_id: id)
end end
@@ -1214,11 +1214,11 @@ class Post < ApplicationRecord
end end
def note_matches(query) def note_matches(query)
where(notes: Note.search(body_matches: query).reorder(nil)) where(notes: Note.where_text_matches(:body, query))
end end
def comment_matches(query) def comment_matches(query)
where(comments: Comment.search(body_matches: query).reorder(nil)) where(comments: Comment.where_text_matches(:body, query))
end end
def saved_search_matches(label, current_user = User.anonymous) def saved_search_matches(label, current_user = User.anonymous)
@@ -1411,17 +1411,18 @@ class Post < ApplicationRecord
post_query.with_implicit_metatags.posts post_query.with_implicit_metatags.posts
end end
def search(params) def search(params, current_user)
q = search_attributes( q = search_attributes(
params, 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, :score, :up_score, :down_score, :md5, :file_ext, :file_size, :image_width,
:image_height, :tag_count, :has_children, :has_active_children, :image_height, :tag_count, :has_children, :has_active_children,
:is_pending, :is_flagged, :is_deleted, :is_banned, :is_pending, :is_flagged, :is_deleted, :is_banned,
:last_comment_bumped_at, :last_commented_at, :last_noted_at, :last_comment_bumped_at, :last_commented_at, :last_noted_at,
:uploader, :approver, :parent, :uploader, :approver, :parent,
:artist_commentary, :flags, :appeals, :notes, :comments, :children, :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? if params[:tags].present?

View File

@@ -23,8 +23,8 @@ class PostAppeal < ApplicationRecord
end end
module SearchMethods module SearchMethods
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :creator, :post) q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :status, :creator, :post], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -36,8 +36,8 @@ class PostApproval < ApplicationRecord
post.uploader.upload_limit.update_limit!(is_pending, true) post.uploader.upload_limit.update_limit!(is_pending, true)
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :user, :post) q = search_attributes(params, [:id, :created_at, :updated_at, :user, :post], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -34,8 +34,8 @@ class PostDisapproval < ApplicationRecord
end end
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :message, :reason, :post) 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.with_message if params[:has_message].to_s.truthy?
q = q.without_message if params[:has_message].to_s.falsy? q = q.without_message if params[:has_message].to_s.falsy?

View File

@@ -58,8 +58,8 @@ class PostFlag < ApplicationRecord
end end
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :post, :creator) q = search_attributes(params, [:id, :created_at, :updated_at, :reason, :status, :post, :creator], current_user: current_user)
if params[:category] if params[:category]
q = q.category_matches(params[:category]) q = q.category_matches(params[:category])

View File

@@ -21,8 +21,8 @@ class PostReplacement < ApplicationRecord
concerning :Search do concerning :Search do
class_methods do class_methods do
def search(params = {}) 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) 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) q.apply_default_order(params)
end end
end end

View File

@@ -39,8 +39,8 @@ class PostVersion < ApplicationRecord
where_ilike(:tags, tag) where_ilike(:tags, tag)
end end
def search(params) 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) 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] if params[:changed_tags]
q = q.changed_tags_include_all(params[:changed_tags].scan(/[^[:space:]]+/)) q = q.changed_tags_include_all(params[:changed_tags].scan(/[^[:space:]]+/))

View File

@@ -31,8 +31,8 @@ class PostVote < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :score, :is_deleted, :user, :post) q = search_attributes(params, [:id, :created_at, :updated_at, :score, :is_deleted, :user, :post], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -17,8 +17,8 @@ class RateLimit < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :limited, :points, :action, :key) q = search_attributes(params, [:id, :created_at, :updated_at, :limited, :points, :action, :key], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -110,8 +110,8 @@ class SavedSearch < ApplicationRecord
concerning :Search do concerning :Search do
class_methods do class_methods do
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :query) q = search_attributes(params, [:id, :created_at, :updated_at, :query], current_user: current_user)
if params[:label] if params[:label]
q = q.labeled(params[:label]) q = q.labeled(params[:label])

View File

@@ -319,8 +319,8 @@ class Tag < ApplicationRecord
abbreviation_matches(abbrev.escape_wildcards).order(post_count: :desc).first abbreviation_matches(abbrev.escape_wildcards).order(post_count: :desc).first
end end
def search(params) 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) 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? if params[:fuzzy_name_matches].present?
q = q.fuzzy_name_matches(params[:fuzzy_name_matches]) q = q.fuzzy_name_matches(params[:fuzzy_name_matches])

View File

@@ -45,7 +45,7 @@ class TagImplication < TagRelationship
concerning :SearchMethods do concerning :SearchMethods do
class_methods do class_methods do
def search(params) def search(params, current_user)
q = super q = super
if params[:implied_from].present? if params[:implied_from].present?

View File

@@ -82,8 +82,8 @@ class TagRelationship < ApplicationRecord
where(status: status.downcase) where(status: status.downcase)
end end
def search(params) 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) 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? if params[:name_matches].present?
q = q.name_matches(params[:name_matches]) q = q.name_matches(params[:name_matches])

View File

@@ -9,8 +9,8 @@ class TagVersion < ApplicationRecord
where_like(:name, Tag.normalize_name(name)) where_like(:name, Tag.normalize_name(name))
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :version, :name, :category, :is_deprecated, :tag, :updater, :previous_version) 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? if params[:name_matches].present?
q = q.name_matches(params[:name_matches]) q = q.name_matches(params[:name_matches])

View File

@@ -32,8 +32,8 @@ class UpgradeCode < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :code, :status, :creator, :redeemer, :user_upgrade) q = search_attributes(params, [:id, :created_at, :updated_at, :code, :status, :creator, :redeemer, :user_upgrade], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -92,8 +92,8 @@ class Upload < ApplicationRecord
where(upload_media_assets.where("upload_media_assets.upload_id = uploads.id").arel.exists) where(upload_media_assets.where("upload_media_assets.upload_id = uploads.id").arel.exists)
end end
def self.search(params) 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) 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? if params[:ai_tags_match].present?
min_score = params.fetch(:min_score, 50).to_i min_score = params.fetch(:min_score, 50).to_i

View File

@@ -36,8 +36,8 @@ class UploadMediaAsset < ApplicationRecord
end end
end end
def self.search(params) 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) 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? if params[:is_posted].to_s.truthy?
q = q.where.associated(:post) q = q.where.associated(:post)

View File

@@ -612,18 +612,19 @@ class User < ApplicationRecord
end end
module SearchMethods module SearchMethods
def search(params) def search(params, current_user)
params = params.dup params = params.dup
params[:name_matches] = params.delete(:name) if params[:name].present? params[:name_matches] = params.delete(:name) if params[:name].present?
q = search_attributes( q = search_attributes(
params, 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, :post_update_count, :note_update_count, :favorite_count, :posts,
:note_versions, :artist_commentary_versions, :post_appeals, :note_versions, :artist_commentary_versions, :post_appeals,
:post_approvals, :artist_versions, :comments, :wiki_page_versions, :post_approvals, :artist_versions, :comments, :wiki_page_versions,
:feedback, :forum_topics, :forum_posts, :forum_post_votes, :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? if params[:name_matches].present?

View File

@@ -95,8 +95,8 @@ class UserAction < ApplicationRecord
all all
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :event_type, :user, :model) q = search_attributes(params, [:event_type, :user, :model], current_user: current_user)
case params[:order] case params[:order]
when "event_at_asc" when "event_at_asc"

View File

@@ -33,8 +33,8 @@ class UserEvent < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :user, :user_session) q = search_attributes(params, [:id, :created_at, :updated_at, :category, :user, :user_session], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -32,8 +32,8 @@ class UserFeedback < ApplicationRecord
order(created_at: :desc) order(created_at: :desc)
end end
def search(params) def search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :body, :is_deleted, :creator, :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) q.apply_default_order(params)
end end

View File

@@ -20,8 +20,8 @@ class UserNameChangeRequest < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :user, :original_name, :desired_name) q = search_attributes(params, [:id, :created_at, :updated_at, :user, :original_name, :desired_name], current_user: current_user)
q.apply_default_order(params) q.apply_default_order(params)
end end

View File

@@ -17,8 +17,8 @@ class UserSession < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :session_id, :user_agent, :ip_addr, :ip_geolocation) 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 = q.apply_default_order(params)
q q
end end

View File

@@ -102,8 +102,8 @@ class UserUpgrade < ApplicationRecord
end end
end end
def self.search(params) def self.search(params, current_user)
q = search_attributes(params, :id, :created_at, :updated_at, :upgrade_type, :status, :transaction_id, :payment_processor, :recipient, :purchaser) 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? if params[:is_gifted].to_s.truthy?
q = q.gifted q = q.gifted

View File

@@ -61,8 +61,8 @@ class WikiPage < ApplicationRecord
order(updated_at: :desc) order(updated_at: :desc)
end end
def search(params = {}) 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) 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]) q = q.where_text_matches([:title, :body], params[:title_or_body_matches])
if params[:title_normalize].present? if params[:title_normalize].present?

View File

@@ -7,8 +7,8 @@ class WikiPageVersion < ApplicationRecord
belongs_to :tag, primary_key: :name, foreign_key: :title, optional: true belongs_to :tag, primary_key: :name, foreign_key: :title, optional: true
module SearchMethods module SearchMethods
def search(params) 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) 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) q.apply_default_order(params)
end end

View File

@@ -64,6 +64,12 @@ class ActiveSupport::TestCase
def as(user, &block) def as(user, &block)
CurrentUser.scoped(user, &block) CurrentUser.scoped(user, &block)
end 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 end
class ActionDispatch::IntegrationTest class ActionDispatch::IntegrationTest

View File

@@ -5,23 +5,6 @@ class ApplicationRecordTest < ActiveSupport::TestCase
@tags = FactoryBot.create_list(:tag, 3, post_count: 1) @tags = FactoryBot.create_list(:tag, 3, post_count: 1)
end 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 "ApplicationRecord#parallel_each" do
context "in threaded mode" do context "in threaded mode" do
should "set CurrentUser correctly" do should "set CurrentUser correctly" do

View File

@@ -29,16 +29,16 @@ class ArtistCommentaryTest < ActiveSupport::TestCase
end end
should "find the correct match" do should "find the correct match" do
assert_equal([@artcomm1.id], ArtistCommentary.search(post_id: @post1.id.to_s).map(&:id)) assert_search_equals(@artcomm1, post_id: @post1.id.to_s)
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "foo").map(&:id)) assert_search_equals(@artcomm1, text_matches: "foo")
assert_equal([@artcomm1.id], ArtistCommentary.search(text_matches: "f*").map(&:id)) assert_search_equals(@artcomm1, text_matches: "f*")
assert_equal([@artcomm1.id], ArtistCommentary.search(post_tags_match: "artcomm1").map(&:id)) assert_search_equals(@artcomm1, post_tags_match: "artcomm1")
assert_equal([@artcomm1.id], ArtistCommentary.search(original_present: "yes").map(&:id)) assert_search_equals(@artcomm1, original_present: "yes")
assert_equal([@artcomm2.id], ArtistCommentary.search(original_present: "no").map(&:id)) assert_search_equals(@artcomm2, original_present: "no")
assert_equal([@artcomm1.id], ArtistCommentary.search(translated_present: "yes").map(&:id)) assert_search_equals(@artcomm1, translated_present: "yes")
assert_equal([@artcomm2.id], ArtistCommentary.search(translated_present: "no").map(&:id)) assert_search_equals(@artcomm2, translated_present: "no")
end end
end end

View File

@@ -2,14 +2,14 @@ require 'test_helper'
class ArtistTest < ActiveSupport::TestCase class ArtistTest < ActiveSupport::TestCase
def assert_artist_found(expected_name, source_url) 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(1, artists.size)
assert_equal(expected_name, artists.first.name, "Testing URL: #{source_url}") assert_equal(expected_name, artists.first.name, "Testing URL: #{source_url}")
end end
def assert_artist_not_found(source_url) 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}") assert_equal(0, artists.size, "Testing URL: #{source_url}")
end end
@@ -455,50 +455,51 @@ class ArtistTest < ActiveSupport::TestCase
end end
should "search on its name should return results" do 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_search_equals(artist1, name: "artist")
assert_not_nil(Artist.search(:name_like => "artist").first) assert_search_equals(artist1, name_like: "artist")
assert_not_nil(Artist.search(:any_name_matches => "artist").first) assert_search_equals(artist1, any_name_matches: "artist")
assert_not_nil(Artist.search(:any_name_matches => "/art/").first) assert_search_equals(artist1, any_name_matches: "/art/")
end end
should "search on other names should return matches" do 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_search_equals([], any_other_name_like: "*artist*")
assert_not_nil(Artist.search(any_other_name_like: "*aaa*").first) assert_search_equals(artist, any_other_name_like: "*aaa*")
assert_not_nil(Artist.search(any_other_name_like: "*ccc_ddd*").first) assert_search_equals(artist, any_other_name_like: "*ccc_ddd*")
assert_not_nil(Artist.search(name: "artist").first) assert_search_equals(artist, name: "artist")
assert_not_nil(Artist.search(:any_name_matches => "aaa").first) assert_search_equals(artist, any_name_matches: "aaa")
assert_not_nil(Artist.search(:any_name_matches => "/a/").first) assert_search_equals(artist, any_name_matches: "/a/")
end end
should "search on group name and return matches" do should "search on group name and return matches" do
cat_or_fish = FactoryBot.create(:artist, :name => "cat_or_fish") cat_or_fish = create(:artist, name: "cat_or_fish")
yuu = FactoryBot.create(:artist, :name => "yuu", :group_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_search_equals(yuu, group_name: "cat_or_fish")
assert_not_nil(Artist.search(:any_name_matches => "cat_or_fish").first) assert_search_equals([yuu, cat_or_fish], any_name_matches: "cat_or_fish")
assert_not_nil(Artist.search(:any_name_matches => "/cat/").first) assert_search_equals([yuu, cat_or_fish], any_name_matches: "/cat/")
end end
should "search on url and return matches" do 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_search_equals(bkub, url_matches: "bkub")
assert_equal([bkub.id], Artist.search(url_matches: "*bkub*").map(&:id)) assert_search_equals(bkub, url_matches: "*bkub*")
assert_equal([bkub.id], Artist.search(url_matches: "/rifyu|bkub/").map(&:id)) assert_search_equals(bkub, url_matches: "/rifyu|bkub/")
assert_equal([bkub.id], Artist.search(url_matches: "http://bkub.com/test.jpg").map(&:id)) assert_search_equals(bkub, url_matches: "http://bkub.com/test.jpg")
end end
should "search on has_tag and return matches" do should "search on has_tag and return matches" do
bkub = FactoryBot.create(:artist, name: "bkub") bkub = create(:artist, name: "bkub")
none = FactoryBot.create(:artist, name: "none") none = create(:artist, name: "none")
post = FactoryBot.create(:post, tag_string: "bkub") post = create(:post, tag_string: "bkub")
assert_equal(bkub.id, Artist.search(has_tag: "true").first.id) assert_search_equals(bkub, has_tag: "true")
assert_equal(none.id, Artist.search(has_tag: "false").first.id) assert_search_equals(none, has_tag: "false")
end end
should "revert to prior versions" do should "revert to prior versions" do

View File

@@ -1,10 +1,6 @@
require 'test_helper' require 'test_helper'
class ArtistURLTest < ActiveSupport::TestCase 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 context "An artist url" do
setup do setup do
CurrentUser.user = FactoryBot.create(:user) CurrentUser.user = FactoryBot.create(:user)
@@ -173,8 +169,6 @@ class ArtistURLTest < ActiveSupport::TestCase
end end
context "#search method" do context "#search method" do
subject { ArtistURL }
should "work" do should "work" do
@bkub = create(:artist, name: "bkub", is_deleted: false, url_string: "https://bkub.com") @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") @masao = create(:artist, name: "masao", is_deleted: true, url_string: "-https://masao.com")

View File

@@ -51,22 +51,9 @@ class BanTest < ActiveSupport::TestCase
context "Searching for a ban" do context "Searching for a ban" do
should "find a given ban" do should "find a given ban" do
CurrentUser.user = FactoryBot.create(:admin_user) ban = create(:ban)
user = FactoryBot.create(:user) assert_search_equals(ban, user_name: ban.user.name, banner_name: ban.banner.name, reason: ban.reason, expired: false, order: :id_desc)
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)
end end
context "by user id" do context "by user id" do

View File

@@ -780,8 +780,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
end end
should "work" do should "work" do
assert_equal([@bur2.id, @bur1.id], BulkUpdateRequest.search.map(&:id)) assert_search_equals(@bur1, user_name: @admin.name, approver_name: @admin.name, status: "approved")
assert_equal([@bur1.id], BulkUpdateRequest.search(user_name: @admin.name, approver_name: @admin.name, status: "approved").map(&:id))
end end
end end
end end

View File

@@ -141,17 +141,13 @@ class CommentTest < ActiveSupport::TestCase
c2 = FactoryBot.create(:comment, :body => "aaa ddd") c2 = FactoryBot.create(:comment, :body => "aaa ddd")
c3 = FactoryBot.create(:comment, :body => "eee") c3 = FactoryBot.create(:comment, :body => "eee")
matches = Comment.search(body_matches: "aaa") assert_search_equals([c2, c1], body_matches: "aaa")
assert_equal(2, matches.count)
assert_equal(c2.id, matches.all[0].id)
assert_equal(c1.id, matches.all[1].id)
end end
should "default to id_desc order when searched with no options specified" do should "default to id_desc order when searched with no options specified" do
comms = FactoryBot.create_list(:comment, 3) 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 end
context "that is edited by a moderator" do context "that is edited by a moderator" do

View File

@@ -1,12 +1,6 @@
require 'test_helper' require 'test_helper'
class SearchableTest < ActiveSupport::TestCase 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 context "#search method" do
subject { Post } subject { Post }
@@ -19,7 +13,7 @@ class SearchableTest < ActiveSupport::TestCase
context "for a nonexistent attribute" do context "for a nonexistent attribute" do
should "raise an error" do should "raise an error" do
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Post.search_attributes({ answer: 42 }, :answer) Post.search_attributes({ answer: 42 }, [:answer], current_user: User.anonymous)
end end
end end
end end

View File

@@ -37,27 +37,19 @@ class DmailTest < ActiveSupport::TestCase
context "search" do context "search" do
should "return results based on title contents" 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_search_equals(dmail, title_matches: "x*")
assert_equal([dmail.id], matches.map(&:id)) assert_search_equals(dmail, title_matches: "X*")
assert_search_equals(dmail, message_matches: "xxx")
matches = Dmail.search(title_matches: "X*") assert_search_equals([], message_matches: "aaa")
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?)
end end
should "return results based on body contents" do should "return results based on body contents" do
dmail = FactoryBot.create(:dmail, :body => "xxx", :owner => @user) dmail = create(:dmail, body: "xxx", owner: @user)
matches = Dmail.search(message_matches: "xxx")
assert(matches.any?) assert_search_equals(dmail, message_matches: "xxx")
matches = Dmail.search(message_matches: "aaa") assert_search_equals([], message_matches: "aaa")
assert(matches.empty?)
end end
end end

View File

@@ -142,9 +142,10 @@ class ForumPostTest < ActiveSupport::TestCase
end end
should "be searchable by body content" do should "be searchable by body content" do
post = FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => "xxx") post = create(:forum_post, topic: @topic, body: "xxx")
assert_equal(1, ForumPost.search(body_matches: "xxx").count)
assert_equal(0, ForumPost.search(body_matches: "aaa").count) assert_search_equals(post, body_matches: "xxx")
assert_search_equals([], body_matches: "aaa")
end end
should "initialize its creator" do should "initialize its creator" do

View File

@@ -44,13 +44,13 @@ class ForumTopicTest < ActiveSupport::TestCase
end end
should "be searchable by title" do should "be searchable by title" do
assert_equal(1, ForumTopic.search(title: "xxx").count) assert_search_equals(@topic, title: "xxx")
assert_equal(0, ForumTopic.search(title: "aaa").count) assert_search_equals([], title: "aaa")
end end
should "be searchable by category id" do should "be searchable by category id" do
assert_equal(1, ForumTopic.search(:category_id => 0).count) assert_search_equals(@topic, category_id: 0)
assert_equal(0, ForumTopic.search(:category_id => 1).count) assert_search_equals([], category_id: 1)
end end
should "initialize its creator" do should "initialize its creator" do

View File

@@ -135,13 +135,13 @@ class NoteTest < ActiveSupport::TestCase
context "where the body contains the string 'aaa'" do context "where the body contains the string 'aaa'" do
should "return a hit" do should "return a hit" do
assert_equal(1, Note.search(body_matches: "aaa").count) assert_search_equals(@note, body_matches: "aaa")
end end
end end
context "where the body contains the string 'bbb'" do context "where the body contains the string 'bbb'" do
should "return no hits" do should "return no hits" do
assert_equal(0, Note.search(body_matches: "bbb").count) assert_search_equals([], body_matches: "bbb")
end end
end end
end end

View File

@@ -18,11 +18,11 @@ class PoolTest < ActiveSupport::TestCase
assert_equal(@pool.id, Pool.find_by_name("test pool").id) assert_equal(@pool.id, Pool.find_by_name("test pool").id)
assert_equal([@pool.id], Pool.search(name_contains: "test pool").map(&:id)) assert_search_equals(@pool, name_contains: "test pool")
assert_equal([@pool.id], Pool.search(name_contains: "tes").map(&:id)) assert_search_equals(@pool, name_contains: "tes")
assert_equal([@pool.id], Pool.search(name_matches: "test pool").map(&:id)) assert_search_equals(@pool, name_matches: "test pool")
assert_equal([@pool.id], Pool.search(name_matches: "testing pool").map(&:id)) assert_search_equals(@pool, name_matches: "testing pool")
assert_equal([], Pool.search(name_matches: "tes").map(&:id)) assert_search_equals([], name_matches: "tes")
end end
should "find pools by post id" do should "find pools by post id" do
@@ -31,8 +31,8 @@ class PoolTest < ActiveSupport::TestCase
@post1 = create(:post, tag_string: "pool:pool1") @post1 = create(:post, tag_string: "pool:pool1")
@post2 = create(:post, tag_string: "pool:pool2") @post2 = create(:post, tag_string: "pool:pool2")
assert_equal([@pool1.id], Pool.search(post_ids_include_any: @post1.id).pluck(:id)) assert_search_equals(@pool1, post_ids_include_any: @post1.id)
assert_equal([@pool2.id, @pool1.id], Pool.search(post_ids_include_any: "#{@post1.id} #{@post2.id}").pluck(:id)) assert_search_equals([@pool2, @pool1], post_ids_include_any: "#{@post1.id} #{@post2.id}")
end end
should "find pools by post id count" do should "find pools by post id count" do
@@ -41,7 +41,7 @@ class PoolTest < ActiveSupport::TestCase
@post1 = create(:post, tag_string: "pool:pool1") @post1 = create(:post, tag_string: "pool:pool1")
@post2 = 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 end
should "find pools by post tags" do should "find pools by post tags" do
@@ -51,13 +51,13 @@ class PoolTest < ActiveSupport::TestCase
@post2 = create(:post, tag_string: "pool:pool1 fumimi") @post2 = create(:post, tag_string: "pool:pool1 fumimi")
@post3 = create(:post, tag_string: "pool:pool2 bkub 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_search_equals([@pool2, @pool1], post_tags_match: "bkub")
assert_equal([@pool2.id, @pool1.id], Pool.search(post_tags_match: "fumimi").pluck(:id)) assert_search_equals([@pool2, @pool1], post_tags_match: "fumimi")
assert_equal([@pool2.id], Pool.search(post_tags_match: "bkub fumimi").pluck(:id)) 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: "bkub" }, current_user: User.anonymous).count)
assert_equal(2, Pool.search(post_tags_match: "fumimi").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").count) assert_equal(1, Pool.search({ post_tags_match: "bkub fumimi" }, current_user: User.anonymous).count)
end end
end end

View File

@@ -95,9 +95,8 @@ class PostApprovalTest < ActiveSupport::TestCase
CurrentUser.scoped(@approver) do CurrentUser.scoped(@approver) do
@post.update!(tag_string: "touhou") @post.update!(tag_string: "touhou")
@approval = create(:post_approval, post: @post, user: @approver) @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 end
end end

View File

@@ -79,9 +79,8 @@ class PostDisapprovalTest < ActiveSupport::TestCase
disapproval1 = FactoryBot.create(:post_disapproval, user: @approver, post: @post1, reason: "breaks_rules") 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") 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_search_equals([disapproval1], reason: "breaks_rules")
assert_equal([disapproval2.id], PostDisapproval.search(message: "bad anatomy").pluck(:id)) assert_search_equals([disapproval2], message: "bad anatomy")
assert_equal([disapproval1.id, disapproval2.id], PostDisapproval.where(user: @approver).pluck(:id))
end end
end end
end end

View File

@@ -294,9 +294,9 @@ class UserTest < ActiveSupport::TestCase
user2.save(validate: false) user2.save(validate: false)
user3.save(validate: false) user3.save(validate: false)
assert_equal([user2.id, user1.id], User.search(name: "foo*").map(&:id)) assert_search_equals([user2, user1], name: "foo*")
assert_equal([user2.id], User.search(name: "foo\*bar").map(&:id)) assert_search_equals(user2, name: "foo\*bar")
assert_equal([user3.id], User.search(name: "bar\\\*baz").map(&:id)) assert_search_equals(user3, name: "bar\\\*baz")
end end
end end

View File

@@ -20,8 +20,7 @@ class WikiPageTest < ActiveSupport::TestCase
end end
should "search other names with wildcards" do should "search other names with wildcards" do
matches = WikiPage.search(other_names_match: "fo*") assert_search_equals(@wiki_page, other_names_match: "fo*")
assert_equal([@wiki_page.id], matches.map(&:id))
end end
should "create versions" do should "create versions" do