Fix #3412: Mass updates incorrectly move saved searches.

This commit is contained in:
evazion
2017-12-06 14:46:12 -06:00
parent d6d73404a9
commit 5819afced7
2 changed files with 20 additions and 6 deletions

View File

@@ -18,11 +18,12 @@ module Moderator
post.update_attributes(:tag_string => tags)
end
tags = Tag.scan_tags(antecedent, :strip_metatags => true)
conds = tags.map {|x| "query like ?"}.join(" AND ")
conds = [conds, *tags.map {|x| "%#{x.to_escaped_for_sql_like}%"}]
if SavedSearch.enabled?
SavedSearch.where(*conds).find_each do |ss|
tags = Tag.scan_tags(antecedent, :strip_metatags => true)
# https://www.postgresql.org/docs/current/static/functions-array.html
saved_searches = SavedSearch.where("string_to_array(query, ' ') @> ARRAY[?]", tags)
saved_searches.find_each do |ss|
ss.query = (ss.query.split - tags + [consequent]).uniq.join(" ")
ss.save
end

View File

@@ -34,8 +34,21 @@ module Moderator
ss = FactoryGirl.create(:saved_search, :user => @user, :query => "123 ... 456")
tag_batch_change = TagBatchChange.new("...", "bbb", @user.id, "127.0.0.1")
tag_batch_change.perform
ss.reload
assert_equal(%w(123 456 bbb), ss.query.scan(/\S+/).sort)
assert_equal("123 456 bbb", ss.reload.normalized_query)
end
should "move only saved searches that match the mass update exactly" do
ss = FactoryGirl.create(:saved_search, :user => @user, :query => "123 ... 456")
tag_batch_change = TagBatchChange.new("1", "bbb", @user.id, "127.0.0.1")
tag_batch_change.perform
assert_equal("... 123 456", ss.reload.normalized_query, "expected '123' to remain unchanged")
tag_batch_change = TagBatchChange.new("123 456", "789", @user.id, "127.0.0.1")
tag_batch_change.perform
assert_equal("... 789", ss.reload.normalized_query, "expected '123 456' to be changed to '789'")
end
should "raise an error if there is no predicate" do