aliases: refactor tag moving code.

* Factor out the code for moving tags from tag aliases to a separate
  TagMover class.

* When aliasing two tags that have conflicting wikis, merge the old wiki
  into the new one instead of failing with an error. Merge the other names
  fields, replace the old wiki body with a message linking to the new
  wiki, and mark the old wiki as deleted.

* When aliasing two tags that have conflicting artist entries, merge the
  old artist into the new one instead of silently ignore the conflict.
  Merge the group name, other names, and urls fields, and mark the old
  artist as deleted.

* When two tags have conflicting wikis or artist entries, but the old
  wiki or artist entry is deleted, then just ignore the old wiki or
  artist and don't try to merge it.

* Fix it so that when saved searches are rewritten, we rewrite negated
  searches too.
This commit is contained in:
evazion
2020-08-26 13:54:27 -05:00
parent 44402299ec
commit f0299a8945
5 changed files with 244 additions and 84 deletions

View File

@@ -137,6 +137,14 @@ class SavedSearch < ApplicationRecord
queries = searches.map(&:normalized_query)
queries.sort.uniq
end
def rewrite_queries!(old_name, new_name)
has_tag(old_name).find_each do |ss|
ss.lock!
ss.rewrite_query(old_name, new_name)
ss.save!
end
end
end
def normalized_query
@@ -146,6 +154,11 @@ class SavedSearch < ApplicationRecord
def normalize_query
self.query = PostQueryBuilder.new(query).normalized_query(sort: false).to_s
end
def rewrite_query(old_name, new_name)
self.query.gsub!(/(?:\A| )([-~])?#{Regexp.escape(old_name)}(?: |\z)/i) { " #{$1}#{new_name} " }
self.query.strip!
end
end
attr_reader :disable_labels
@@ -155,6 +168,7 @@ class SavedSearch < ApplicationRecord
before_validation :normalize_query
before_validation :normalize_labels
scope :labeled, ->(label) { where_array_includes_any_lower(:labels, [normalize_label(label)]) }
scope :has_tag, ->(name) { where_regex(:query, "(^| )[~-]?#{Regexp.escape(name)}( |$)", flags: "i") }
def validate_count
if user.saved_searches.count + 1 > user.max_saved_searches