searchable: don't automatically include id, created_at, updated_at.

Don't make search methods on models call super in order to search
certain default attributes (id, created_at, updated_at). Simplifies some
magic.
This commit is contained in:
evazion
2020-12-16 22:00:22 -06:00
parent b0659eb76c
commit e771c0fca8
45 changed files with 51 additions and 121 deletions

View File

@@ -155,6 +155,7 @@ module Searchable
indifferent_params = params.try(:with_indifferent_access) || params.try(:to_unsafe_h)
raise ArgumentError, "unable to process params" if indifferent_params.nil?
attributes += searchable_includes
attributes.reduce(all) do |relation, attribute|
relation.search_attribute(attribute, indifferent_params, CurrentUser.user)
end
@@ -406,14 +407,6 @@ module Searchable
where(id: ids).order(Arel.sql(order_clause.join(', ')))
end
def search(params = {})
params ||= {}
default_attributes = (attribute_names.map(&:to_sym) & %i[id created_at updated_at])
all_attributes = default_attributes + searchable_includes
search_attributes(params, *all_attributes)
end
private
def qualified_column_for(attr)

View File

@@ -250,9 +250,7 @@ class Artist < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_deleted, :is_banned, :name, :group_name, :other_names)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :other_names)
if params[:any_other_name_like]
q = q.any_other_name_like(params[:any_other_name_like])

View File

@@ -31,9 +31,7 @@ class ArtistCommentary < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :original_title, :original_description, :translated_title, :translated_description)
q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description)
if params[:text_matches].present?
q = q.text_matches(params[:text_matches])

View File

@@ -12,8 +12,7 @@ class ArtistCommentaryVersion < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :original_title, :original_description, :translated_title, :translated_description)
q = search_attributes(params, :id, :created_at, :updated_at, :original_title, :original_description, :translated_title, :translated_description)
if params[:text_matches].present?
q = q.text_matches(params[:text_matches])

View File

@@ -40,9 +40,7 @@ class ArtistUrl < ApplicationRecord
end
def self.search(params = {})
q = super
q = q.search_attributes(params, :url, :normalized_url, :is_active)
q = search_attributes(params, :id, :created_at, :updated_at, :url, :normalized_url, :is_active)
q = q.url_matches(params[:url_matches])
q = q.normalized_url_matches(params[:normalized_url_matches])

View File

@@ -7,9 +7,7 @@ class ArtistVersion < ApplicationRecord
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names)
q = q.text_attribute_matches(:name, params[:name_matches])
q = q.text_attribute_matches(:group_name, params[:group_name_matches])

View File

@@ -20,9 +20,7 @@ class Ban < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :expires_at, :reason)
q = search_attributes(params, :id, :created_at, :updated_at, :expires_at, :reason)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q = q.expired if params[:expired].to_s.truthy?

View File

@@ -31,9 +31,7 @@ class BulkUpdateRequest < ApplicationRecord
end
def search(params = {})
q = super
q = q.search_attributes(params, :script, :tags)
q = search_attributes(params, :id, :created_at, :updated_at, :script, :tags)
q = q.text_attribute_matches(:script, params[:script_matches])
if params[:status].present?

View File

@@ -26,9 +26,7 @@ class Comment < ApplicationRecord
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index)
case params[:order]

View File

@@ -19,8 +19,7 @@ class CommentVote < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :score)
q = search_attributes(params, :id, :created_at, :updated_at, :score)
q.apply_default_order(params)
end

View File

@@ -98,9 +98,7 @@ class Dmail < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_read, :is_deleted, :title, :body)
q = search_attributes(params, :id, :created_at, :updated_at, :is_read, :is_deleted, :title, :body)
q = q.text_attribute_matches(:title, params[:title_matches])
q = q.text_attribute_matches(:body, params[:message_matches], index_column: :message_index)

View File

@@ -30,8 +30,7 @@ class DtextLink < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :link_type, :link_target)
q = search_attributes(params, :id, :created_at, :updated_at, :link_type, :link_target)
q.apply_default_order(params)
end

View File

@@ -38,9 +38,7 @@ class EmailAddress < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :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)
q = q.apply_default_order(params)
q

View File

@@ -12,8 +12,7 @@ class Favorite < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :post)
q = search_attributes(params, :id, :post)
if params[:user_id].present?
q = q.for_user(params[:user_id])

View File

@@ -26,8 +26,7 @@ class FavoriteGroup < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :name, :is_public, :post_ids)
q = search_attributes(params, :id, :created_at, :updated_at, :name, :is_public, :post_ids)
if params[:name_matches].present?
q = q.name_matches(params[:name_matches])

View File

@@ -41,8 +41,7 @@ class ForumPost < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_deleted, :body)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :body)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :text_index)
if params[:linked_to].present?

View File

@@ -19,8 +19,7 @@ class ForumPostVote < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :score)
q = search_attributes(params, :id, :created_at, :updated_at, :score)
q = q.forum_post_matches(params[:forum_post])
q.apply_default_order(params)
end

View File

@@ -86,8 +86,7 @@ class ForumTopic < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_sticky, :is_locked, :is_deleted, :category_id, :title, :response_count)
q = search_attributes(params, :id, :created_at, :updated_at, :is_sticky, :is_locked, :is_deleted, :category_id, :title, :response_count)
q = q.text_attribute_matches(:title, params[:title_matches], index_column: :text_index)
if params[:is_private].to_s.truthy?

View File

@@ -7,8 +7,7 @@ class ForumTopicVisit < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :user, :forum_topic_id, :last_read_at)
q = search_attributes(params, :id, :created_at, :updated_at, :user, :forum_topic_id, :last_read_at)
q.apply_default_order(params)
end
end

View File

@@ -12,8 +12,7 @@ class IpAddress < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :ip_addr)
q = search_attributes(params, :ip_addr)
q.order(created_at: :desc)
end

View File

@@ -25,8 +25,7 @@ class IpBan < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :reason)
q = search_attributes(params, :id, :created_at, :updated_at, :reason)
q = q.text_attribute_matches(:reason, params[:reason_matches])
if params[:ip_addr].present?

View File

@@ -61,9 +61,7 @@ class ModAction < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :category, :description)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :description)
q = q.text_attribute_matches(:description, params[:description_matches])
q.apply_default_order(params)

View File

@@ -82,8 +82,7 @@ class ModerationReport < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :reason)
q = search_attributes(params, :id, :created_at, :updated_at, :reason)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q.apply_default_order(params)

View File

@@ -14,9 +14,7 @@ class Note < ApplicationRecord
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :is_active, :x, :y, :width, :height, :body, :version)
q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index)
q.apply_default_order(params)

View File

@@ -4,9 +4,7 @@ class NoteVersion < ApplicationRecord
belongs_to_updater :counter_cache => "note_update_count"
def self.search(params)
q = super
q = q.search_attributes(params, :is_active, :x, :y, :width, :height, :body, :version)
q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)

View File

@@ -13,8 +13,7 @@ class PixivUgoiraFrameData < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :data, :content_type)
q = search_attributes(params, :id, :data, :content_type)
q.apply_default_order(params)
end

View File

@@ -36,9 +36,7 @@ class Pool < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_deleted, :name, :description, :post_ids)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids)
q = q.text_attribute_matches(:description, params[:description_matches])
if params[:post_tags_match]

View File

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

View File

@@ -1289,14 +1289,14 @@ class Post < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(
q = search_attributes(
params,
: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_note_locked, :is_rating_locked, :is_status_locked, :is_pending, :is_flagged, :is_deleted,
:is_banned, :last_comment_bumped_at, :last_commented_at, :last_noted_at
: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_note_locked, :is_rating_locked, :is_status_locked, :is_pending,
:is_flagged, :is_deleted, :is_banned, :last_comment_bumped_at,
:last_commented_at, :last_noted_at, :uploader_ip_addr
)
if params[:tags].present?

View File

@@ -17,8 +17,7 @@ class PostAppeal < ApplicationRecord
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :reason, :status)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q.apply_default_order(params)

View File

@@ -38,7 +38,7 @@ class PostApproval < ApplicationRecord
end
def self.search(params)
q = super
q = search_attributes(params, :id, :created_at, :updated_at, :post, :user)
q.apply_default_order(params)
end

View File

@@ -21,9 +21,7 @@ class PostDisapproval < ApplicationRecord
concerning :SearchMethods do
class_methods do
def search(params)
q = super
q = q.search_attributes(params, :message, :reason)
q = search_attributes(params, :id, :created_at, :updated_at, :message, :reason)
q = q.text_attribute_matches(:message, params[:message_matches])
q = q.with_message if params[:has_message].to_s.truthy?

View File

@@ -56,9 +56,7 @@ class PostFlag < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :reason, :status)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status)
q = q.text_attribute_matches(:reason, params[:reason_matches])
if params[:creator_id].present?

View File

@@ -21,8 +21,7 @@ class PostReplacement < ApplicationRecord
concerning :Search do
class_methods do
def search(params = {})
q = super
q = q.search_attributes(params, :md5, :md5_was, :file_ext, :file_ext_was, :original_url, :replacement_url)
q = search_attributes(params, :id, :created_at, :updated_at, :md5, :md5_was, :file_ext, :file_ext_was, :original_url, :replacement_url)
q.apply_default_order(params)
end
end

View File

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

View File

@@ -19,8 +19,7 @@ class PostVote < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :score)
q = search_attributes(params, :id, :created_at, :updated_at, :score)
q.apply_default_order(params)
end

View File

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

View File

@@ -271,9 +271,7 @@ class Tag < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :is_locked, :category, :post_count, :name)
q = search_attributes(params, :id, :created_at, :updated_at, :is_locked, :category, :post_count, :name)
if params[:fuzzy_name_matches].present?
q = q.fuzzy_name_matches(params[:fuzzy_name_matches])

View File

@@ -66,8 +66,7 @@ class TagRelationship < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :antecedent_name, :consequent_name)
q = search_attributes(params, :id, :created_at, :updated_at, :antecedent_name, :consequent_name)
if params[:name_matches].present?
q = q.name_matches(params[:name_matches])

View File

@@ -182,9 +182,7 @@ class Upload < ApplicationRecord
end
def self.search(params)
q = super
q = q.search_attributes(params, :source, :rating, :parent_id, :server, :md5, :server, :file_ext, :file_size, :image_width, :image_height, :referer_url)
q = search_attributes(params, :id, :created_at, :updated_at, :source, :rating, :parent_id, :server, :md5, :server, :file_ext, :file_size, :image_width, :image_height, :referer_url)
if params[:source_matches].present?
q = q.where_like(:source, params[:source_matches])

View File

@@ -560,12 +560,10 @@ class User < ApplicationRecord
module SearchMethods
def search(params)
q = super
params = params.dup
params[:name_matches] = params.delete(:name) if params[:name].present?
q = q.search_attributes(params, :name, :level, :post_upload_count, :post_update_count, :note_update_count, :favorite_count)
q = search_attributes(params, :id, :created_at, :updated_at, :name, :level, :post_upload_count, :post_update_count, :note_update_count, :favorite_count)
if params[:name_matches].present?
q = q.where_ilike(:name, normalize_name(params[:name_matches]))

View File

@@ -30,9 +30,7 @@ class UserFeedback < ApplicationRecord
end
def search(params)
q = super
q = q.search_attributes(params, :category, :body, :is_deleted)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :body, :is_deleted)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)

View File

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

View File

@@ -65,9 +65,7 @@ class WikiPage < ApplicationRecord
end
def search(params = {})
q = super
q = q.search_attributes(params, :is_locked, :is_deleted, :body, :title, :other_names)
q = search_attributes(params, :id, :created_at, :updated_at, :is_locked, :is_deleted, :body, :title, :other_names)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index, ts_config: "danbooru")
if params[:title_normalize].present?

View File

@@ -7,9 +7,7 @@ class WikiPageVersion < ApplicationRecord
module SearchMethods
def search(params)
q = super
q = q.search_attributes(params, :title, :body, :other_names, :is_locked, :is_deleted)
q = search_attributes(params, :id, :created_at, :updated_at, :title, :body, :other_names, :is_locked, :is_deleted)
q = q.text_attribute_matches(:title, params[:title_matches])
q = q.text_attribute_matches(:body, params[:body_matches])