smarter updates on saved searches on tag batch changes #2674

This commit is contained in:
Albert Yi
2016-12-05 17:19:21 -08:00
parent bfa1ac63a4
commit deb62e0cdb
3 changed files with 15 additions and 5 deletions

View File

@@ -20,9 +20,11 @@ module Moderator
post.update_attributes(:tag_string => tags) post.update_attributes(:tag_string => tags)
end end
escaped = Regexp.escape(antecedent) tags = Tag.scan_tags(antecedent, :strip_metatags => true)
SavedSearch.where("tag_query like ?", "%#{antecedent}%").find_each do |ss| conds = tags.map {|x| "tag_query like ?"}.join(" AND ")
ss.tag_query = ss.tag_query.sub(/(?:^| )#{escaped}(?:$| )/, " #{consequent} ").strip.gsub(/ /, " ") conds = [conds, *tags.map {|x| "%#{x}%"}]
SavedSearch.where(*conds).find_each do |ss|
ss.tag_query = (ss.tag_query_array - tags + antecedent).uniq.join(" ")
ss.save ss.save
end end

View File

@@ -152,4 +152,8 @@ class SavedSearch < ActiveRecord::Base
user.update_attribute(:has_saved_searches, false) user.update_attribute(:has_saved_searches, false)
end end
end end
def tag_query_array
Tag.scan_tags(tag_query)
end
end end

View File

@@ -236,10 +236,14 @@ class Tag < ActiveRecord::Base
list + tagstr.gsub(/-?source:".*?"/, "").scan(/\S+/).uniq list + tagstr.gsub(/-?source:".*?"/, "").scan(/\S+/).uniq
end end
def scan_tags(tags) def scan_tags(tags, options = {})
tagstr = normalize(tags) tagstr = normalize(tags)
list = tagstr.scan(/source:".*?"/) || [] list = tagstr.scan(/source:".*?"/) || []
list + tagstr.gsub(/source:".*?"/, "").gsub(/[%,]/, "").scan(/\S+/).uniq list += tagstr.gsub(/source:".*?"/, "").gsub(/[%,]/, "").scan(/\S+/).uniq
if options[:strip_metatags]
list = list.map {|x| x.sub(/^[-~]/, "")}
end
list
end end
def parse_cast(object, type) def parse_cast(object, type)