mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 10:20:58 -06:00
143f06f2c6
At the moment, there is no way to create a group of related watched words together. If a user needed a set of words to be created together, they'll have to create them individually one at a time. This change attempts to allow related watched words to be created as a group. The idea here is to have a list of words be tied together via a common `WatchedWordGroup` record. Given a list of words, a `WatchedWordGroup` record is created and assigned to each `WatchedWord` record. The existing WatchedWord creation behaviour remains largely unchanged. Co-authored-by: Selase Krakani <skrakani@gmail.com> Co-authored-by: Martin Brennan <martin@discourse.org>
57 lines
2.2 KiB
Ruby
57 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe WatchedWordGroup do
|
|
fab!(:watched_word_group)
|
|
fab!(:watched_word_1) { Fabricate(:watched_word, watched_word_group_id: watched_word_group.id) }
|
|
fab!(:watched_word_2) { Fabricate(:watched_word, watched_word_group_id: watched_word_group.id) }
|
|
|
|
describe "#create_or_update_members" do
|
|
it "updates watched word action" do
|
|
words = [watched_word_1.word, watched_word_2.word, "damn", "4sale"]
|
|
old_action = watched_word_group.action
|
|
watched_words_before_update = watched_word_group.watched_words
|
|
|
|
expect(old_action).to eq(WatchedWord.actions[:block])
|
|
expect(watched_words_before_update.map(&:action).uniq).to contain_exactly(old_action)
|
|
|
|
watched_word_group.create_or_update_members(words, action_key: :censor)
|
|
|
|
expect(watched_word_group.reload.errors).to be_empty
|
|
|
|
watched_words = watched_word_group.watched_words
|
|
|
|
expect(watched_words.size).to eq(4)
|
|
expect(watched_words.map(&:word)).to contain_exactly(*words)
|
|
expect(watched_words.map(&:action).uniq).to contain_exactly(WatchedWord.actions[:censor])
|
|
expect(watched_word_group.action).to eq(WatchedWord.actions[:censor])
|
|
end
|
|
|
|
it "leaves membership intact if update fails" do
|
|
words = [watched_word_1.word, watched_word_2.word, "a" * 120]
|
|
old_action = watched_word_group.action
|
|
watched_words_before_update = watched_word_group.watched_words
|
|
|
|
expect(watched_words_before_update.size).to eq(2)
|
|
expect(watched_words_before_update.map(&:word)).to contain_exactly(
|
|
watched_word_1.word,
|
|
watched_word_2.word,
|
|
)
|
|
expect(watched_words_before_update.map(&:action).uniq).to contain_exactly(old_action)
|
|
|
|
watched_word_group.create_or_update_members(
|
|
words,
|
|
action_key: WatchedWord.actions[watched_word_group.action],
|
|
)
|
|
|
|
expect(watched_word_group.reload.errors).not_to be_empty
|
|
|
|
watched_words = watched_word_group.watched_words
|
|
|
|
expect(watched_word_group.action).to eq(old_action)
|
|
expect(watched_words.size).to eq(2)
|
|
expect(watched_words.map(&:word)).to contain_exactly(watched_word_1.word, watched_word_2.word)
|
|
expect(watched_words.map(&:action).uniq).to contain_exactly(old_action)
|
|
end
|
|
end
|
|
end
|