From 6fc4b63fa88dc9b39f80aa375745dfef1b84461f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 29 Aug 2019 16:44:19 -0500 Subject: [PATCH] Model#search: factor out post_tags_match. --- app/models/application_record.rb | 14 ++++++++++++++ app/models/artist_commentary.rb | 14 ++------------ app/models/comment.rb | 14 +------------- app/models/note.rb | 13 +------------ app/models/post_appeal.rb | 14 +------------- app/models/post_approval.rb | 22 +++++----------------- app/models/post_flag.rb | 13 +------------ app/models/post_replacement.rb | 14 +------------- app/models/upload.rb | 13 +------------ 9 files changed, 27 insertions(+), 104 deletions(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index dfd4aeb49..b4263da45 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -121,6 +121,20 @@ class ApplicationRecord < ActiveRecord::Base end end + def search_post_id_attribute(params) + relation = self + + if params[:post_id].present? + relation = relation.attribute_matches(:post_id, params[:post_id]) + end + + if params[:post_tags_match].present? + relation = relation.where(post_id: Post.tag_match(params[:post_tags_match]).reorder(nil)) + end + + relation + end + def apply_default_order(params) if params[:order] == "custom" parse_ids = Tag.parse_helper(params[:id]) diff --git a/app/models/artist_commentary.rb b/app/models/artist_commentary.rb index 7742b9ffd..9008372ad 100644 --- a/app/models/artist_commentary.rb +++ b/app/models/artist_commentary.rb @@ -18,10 +18,6 @@ class ArtistCommentary < ApplicationRecord where("original_title ILIKE ? ESCAPE E'\\\\' OR original_description ILIKE ? ESCAPE E'\\\\' OR translated_title ILIKE ? ESCAPE E'\\\\' OR translated_description ILIKE ? ESCAPE E'\\\\'", escaped_query, escaped_query, escaped_query, escaped_query) end - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def deleted where(original_title: "", original_description: "", translated_title: "", translated_description: "") end @@ -33,14 +29,12 @@ class ArtistCommentary < ApplicationRecord def search(params) q = super + q = q.search_post_id_attribute(params) + if params[:text_matches].present? q = q.text_matches(params[:text_matches]) end - if params[:post_id].present? - q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) - end - if params[:original_present].to_s.truthy? q = q.where("(original_title != '') or (original_description != '')") elsif params[:original_present].to_s.falsy? @@ -53,10 +47,6 @@ class ArtistCommentary < ApplicationRecord q = q.where("(translated_title = '') and (translated_description = '')") end - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - q = q.deleted if params[:is_deleted] == "yes" q = q.undeleted if params[:is_deleted] == "no" diff --git a/app/models/comment.rb b/app/models/comment.rb index 29097d414..0c69a0744 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -31,23 +31,11 @@ class Comment < ApplicationRecord where("comments.is_deleted = false") end - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def search(params) q = super q = q.attribute_matches(:body, params[:body_matches], index_column: :body_index) - - if params[:post_id].present? - q = q.where("post_id in (?)", params[:post_id].split(",").map(&:to_i)) - end - - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - + q = q.search_post_id_attribute(params) q = q.search_user_attribute(:creator, params) q = q.attribute_matches(:is_deleted, params[:is_deleted]) q = q.attribute_matches(:is_sticky, params[:is_sticky]) diff --git a/app/models/note.rb b/app/models/note.rb index ddf05457e..778e0b78c 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -16,24 +16,13 @@ class Note < ApplicationRecord where("is_active = TRUE") end - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def search(params) q = super q = q.attribute_matches(:body, params[:body_matches], index_column: :body_index) q = q.attribute_matches(:is_active, params[:is_active]) q = q.search_user_attribute(:creator, params) - - if params[:post_id].present? - q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) - end - - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end + q = q.search_post_id_attribute(params) q.apply_default_order(params) end diff --git a/app/models/post_appeal.rb b/app/models/post_appeal.rb index 6a1ac0dd8..2ca9b04c0 100644 --- a/app/models/post_appeal.rb +++ b/app/models/post_appeal.rb @@ -10,10 +10,6 @@ class PostAppeal < ApplicationRecord validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post" module SearchMethods - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def resolved joins(:post).where("posts.is_deleted = false and posts.is_flagged = false") end @@ -31,15 +27,7 @@ class PostAppeal < ApplicationRecord q = q.attribute_matches(:reason, params[:reason_matches]) q = q.search_user_attribute(:creator, params) - - if params[:post_id].present? - q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) - end - - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - + q = q.search_post_id_attribute(params) q = q.resolved if params[:is_resolved].to_s.truthy? q = q.unresolved if params[:is_resolved].to_s.falsy? diff --git a/app/models/post_approval.rb b/app/models/post_approval.rb index 8f5327fdd..e36ad3508 100644 --- a/app/models/post_approval.rb +++ b/app/models/post_approval.rb @@ -32,24 +32,12 @@ class PostApproval < ApplicationRecord post.update(approver: user, is_flagged: false, is_pending: false, is_deleted: false) end - concerning :SearchMethods do - class_methods do - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end + def self.search(params) + q = super - def search(params) - q = super + q = q.search_user_attribute(:user, params) + q = q.search_post_id_attribute(params) - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - - q = q.search_user_attribute(:user, params) - q = q.attribute_matches(:post_id, params[:post_id]) - - q.apply_default_order(params) - end - end + q.apply_default_order(params) end end diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index 34f40077d..72c3dffaf 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -32,10 +32,6 @@ class PostFlag < ApplicationRecord where("to_tsvector('english', post_flags.reason) @@ to_tsquery('!dup & !duplicate & !sample & !smaller')") end - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def resolved where("is_resolved = ?", true) end @@ -78,14 +74,7 @@ class PostFlag < ApplicationRecord end end - if params[:post_id].present? - q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) - end - - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - + q = q.search_post_id_attribute(params) q = q.attribute_matches(:is_resolved, params[:is_resolved]) case params[:category] diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index eaba5cc18..7fb62c9c3 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -20,10 +20,6 @@ class PostReplacement < ApplicationRecord concerning :Search do class_methods do - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def search(params = {}) q = super @@ -34,15 +30,7 @@ class PostReplacement < ApplicationRecord q = q.attribute_matches(:md5_was, params[:md5_was]) q = q.attribute_matches(:md5, params[:md5]) q = q.search_user_attribute(:creator, params) - - if params[:post_id].present? - q = q.where(post_id: params[:post_id].split(",").map(&:to_i)) - end - - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - + q = q.search_post_id_attribute(params) q.apply_default_order(params) end end diff --git a/app/models/upload.rb b/app/models/upload.rb index 4c2c9c1a8..69f13a181 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -174,14 +174,11 @@ class Upload < ApplicationRecord where(:status => "pending") end - def post_tags_match(query) - where(post_id: PostQueryBuilder.new(query).build.reorder("")) - end - def search(params) q = super q = q.search_user_attribute(:uploader, params) + q = q.search_post_id_attribute(params) if params[:source].present? q = q.where(source: params[:source]) @@ -199,20 +196,12 @@ class Upload < ApplicationRecord q = q.attribute_matches(:rating, params[:parent_id]) end - if params[:post_id].present? - q = q.attribute_matches(:post_id, params[:post_id]) - end - if params[:has_post].to_s.truthy? q = q.where.not(post_id: nil) elsif params[:has_post].to_s.falsy? q = q.where(post_id: nil) end - if params[:post_tags_match].present? - q = q.post_tags_match(params[:post_tags_match]) - end - if params[:status].present? q = q.where("uploads.status LIKE ? ESCAPE E'\\\\'", params[:status].to_escaped_for_sql_like) end