Model#search: factor out post_tags_match.

This commit is contained in:
evazion
2019-08-29 16:44:19 -05:00
parent c3ad7f6112
commit 6fc4b63fa8
9 changed files with 27 additions and 104 deletions

View File

@@ -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])

View File

@@ -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"

View File

@@ -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])

View File

@@ -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

View File

@@ -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?

View File

@@ -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

View File

@@ -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]

View File

@@ -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

View File

@@ -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