search: refactor to pass in the current user explicitly.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -296,7 +296,7 @@ class AutocompleteService
|
||||
# @param string [String] the name of the pool
|
||||
# @return [Array<Hash>] 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<Hash>] 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<Hash>] 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<Hash>] 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<Hash>] 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 }
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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(","))
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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]))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:]]+/))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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])
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user