search: refactor PostQueryBuilder class methods into instance methods.

* Make scan_query, parse_query, normalize_query into instance methods
  instead of class methods. This is to a) clean up the API and b)
  prepare for moving certain tag utility methods into PostQueryBuilder.

* Fix a few cases where a caller used scan_query when they should have
  used split_query or parse_tag_edit.
This commit is contained in:
evazion
2020-04-22 19:38:17 -05:00
parent d355c0e221
commit 3dab648d0e
10 changed files with 534 additions and 532 deletions

View File

@@ -596,7 +596,7 @@ class Post < ApplicationRecord
# If someone else committed changes to this post before we did,
# then try to merge the tag changes together.
current_tags = tag_string_was.split
new_tags = PostQueryBuilder.parse_tag_edit(tag_string)
new_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
old_tags = old_tag_string.split
kept_tags = current_tags & new_tags
@@ -634,7 +634,7 @@ class Post < ApplicationRecord
end
def normalize_tags
normalized_tags = PostQueryBuilder.split_query(tag_string)
normalized_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map(&:downcase)
normalized_tags = filter_metatags(normalized_tags)
@@ -1070,7 +1070,7 @@ class Post < ApplicationRecord
tags = tags.to_s
tags += " rating:s" if CurrentUser.safe_mode?
tags += " -status:deleted" if CurrentUser.hide_deleted_posts? && !Tag.has_metatag?(tags, "status", "-status")
tags = PostQueryBuilder.normalize_query(tags)
tags = PostQueryBuilder.new(tags).normalize_query
# Optimize some cases. these are just estimates but at these
# quantities being off by a few hundred doesn't matter much

View File

@@ -136,17 +136,17 @@ class SavedSearch < ApplicationRecord
def queries_for(user_id, label: nil, options: {})
searches = SavedSearch.where(user_id: user_id)
searches = searches.labeled(label) if label.present?
queries = searches.pluck(:query).map { |query| PostQueryBuilder.normalize_query(query, sort: true) }
queries = searches.pluck(:query).map { |query| PostQueryBuilder.new(query).normalize_query(sort: true) }
queries.sort.uniq
end
end
def normalized_query
PostQueryBuilder.normalize_query(query, sort: true)
PostQueryBuilder.new(query).normalize_query(sort: true)
end
def normalize_query
self.query = PostQueryBuilder.normalize_query(query, sort: false)
self.query = PostQueryBuilder.new(query).normalize_query(sort: false)
end
end

View File

@@ -234,7 +234,7 @@ class Tag < ApplicationRecord
end
def is_single_tag?(query)
PostQueryBuilder.scan_query(query).size == 1
PostQueryBuilder.new(query).split_query.size == 1
end
def is_metatag?(tag)
@@ -256,7 +256,7 @@ class Tag < ApplicationRecord
def has_metatag?(tags, *metatags)
return nil if tags.blank?
tags = PostQueryBuilder.split_query(tags.to_str) if tags.respond_to?(:to_str)
tags = PostQueryBuilder.new(tags.to_str).split_query if tags.respond_to?(:to_str)
tags.grep(/\A(?:#{metatags.map(&:to_s).join("|")}):(.+)\z/i) { $1 }.first
end
end