DEV: allow to have duplicate topic titles if categegory is different (#10034)

Co-authored-by: Robin Ward <robin.ward@gmail.com>

Co-authored-by: Robin Ward <robin.ward@gmail.com>
This commit is contained in:
Bernhard Suttner 2020-06-18 17:19:47 +02:00 committed by GitHub
parent ec794c7f29
commit e31471585a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 6 deletions

View File

@ -155,7 +155,12 @@ class Topic < ActiveRecord::Base
message: :has_already_been_used, message: :has_already_been_used,
allow_blank: true, allow_blank: true,
case_sensitive: false, case_sensitive: false,
collection: Proc.new { Topic.listable_topics } } collection: Proc.new { |t|
SiteSetting.allow_duplicate_topic_titles_category? ?
Topic.listable_topics.where("category_id = ?", t.category_id) :
Topic.listable_topics
}
}
validates :category_id, validates :category_id,
presence: true, presence: true,

View File

@ -1449,6 +1449,7 @@ en:
category_search_priority_very_high_weight: "Weight applied to ranking for very high category search priority." category_search_priority_very_high_weight: "Weight applied to ranking for very high category search priority."
allow_uncategorized_topics: "Allow topics to be created without a category. WARNING: If there are any uncategorized topics, you must recategorize them before turning this off." allow_uncategorized_topics: "Allow topics to be created without a category. WARNING: If there are any uncategorized topics, you must recategorize them before turning this off."
allow_duplicate_topic_titles: "Allow topics with identical, duplicate titles." allow_duplicate_topic_titles: "Allow topics with identical, duplicate titles."
allow_duplicate_topic_titles_category: "Allow topics with identical, duplicate titles if the category is different. allow_duplicate_topic_titles must be false."
unique_posts_mins: "How many minutes before a user can make a post with the same content again" unique_posts_mins: "How many minutes before a user can make a post with the same content again"
educate_until_posts: "When the user starts typing their first (n) new posts, show the pop-up new user education panel in the composer." educate_until_posts: "When the user starts typing their first (n) new posts, show the pop-up new user education panel in the composer."
title: "The name of this site, as used in the title tag." title: "The name of this site, as used in the title tag."

View File

@ -716,6 +716,7 @@ posting:
default: true default: true
refresh: true refresh: true
allow_duplicate_topic_titles: false allow_duplicate_topic_titles: false
allow_duplicate_topic_titles_category: false
min_title_similar_length: min_title_similar_length:
client: true client: true
default: 10 default: 10

View File

@ -12,7 +12,7 @@ class UniqueAmongValidator < ActiveRecord::Validations::UniquenessValidator
# do nothing further unless there were some duplicates. # do nothing further unless there were some duplicates.
unless new_errors == 0 unless new_errors == 0
# now look only in the collection we care about. # now look only in the collection we care about.
dupes = options[:collection].call.where("lower(#{attribute}) = ?", value.downcase) dupes = options[:collection].call(record).where("lower(#{attribute}) = ?", value.downcase)
dupes = dupes.where("id != ?", record.id) if record.persisted? dupes = dupes.where("id != ?", record.id) if record.persisted?
# pop off the error, if it was a false positive # pop off the error, if it was a false positive

View File

@ -77,6 +77,7 @@ class ImportScripts::Base
min_personal_message_post_length: 1, min_personal_message_post_length: 1,
min_personal_message_title_length: 1, min_personal_message_title_length: 1,
allow_duplicate_topic_titles: true, allow_duplicate_topic_titles: true,
allow_duplicate_topic_titles_category: false,
disable_emails: 'yes', disable_emails: 'yes',
max_attachment_size_kb: 102400, max_attachment_size_kb: 102400,
max_image_size_kb: 102400, max_image_size_kb: 102400,

View File

@ -255,19 +255,27 @@ describe Topic do
end end
context 'topic title uniqueness' do context 'topic title uniqueness' do
let!(:category1) { Fabricate(:category) }
let!(:category2) { Fabricate(:category) }
let!(:topic) { Fabricate(:topic) } let!(:topic) { Fabricate(:topic, category: category1) }
let(:new_topic) { Fabricate.build(:topic, title: topic.title) } let(:new_topic) { Fabricate.build(:topic, title: topic.title, category: category1) }
let(:new_topic_different_cat) { Fabricate.build(:topic, title: topic.title, category: category2) }
context "when duplicates aren't allowed" do context "when duplicates aren't allowed" do
before do before do
SiteSetting.expects(:allow_duplicate_topic_titles?).returns(false) SiteSetting.allow_duplicate_topic_titles = false
SiteSetting.allow_duplicate_topic_titles_category = false
end end
it "won't allow another topic to be created with the same name" do it "won't allow another topic to be created with the same name" do
expect(new_topic).not_to be_valid expect(new_topic).not_to be_valid
end end
it "won't even allow another topic to be created with the same name but different category" do
expect(new_topic_different_cat).not_to be_valid
end
it "won't allow another topic with an upper case title to be created" do it "won't allow another topic with an upper case title to be created" do
new_topic.title = new_topic.title.upcase new_topic.title = new_topic.title.upcase
expect(new_topic).not_to be_valid expect(new_topic).not_to be_valid
@ -286,7 +294,8 @@ describe Topic do
context "when duplicates are allowed" do context "when duplicates are allowed" do
before do before do
SiteSetting.expects(:allow_duplicate_topic_titles?).returns(true) SiteSetting.allow_duplicate_topic_titles = true
SiteSetting.allow_duplicate_topic_titles_category = false
end end
it "will allow another topic to be created with the same name" do it "will allow another topic to be created with the same name" do
@ -294,6 +303,21 @@ describe Topic do
end end
end end
context "when duplicates are allowed if the category is different" do
before do
SiteSetting.allow_duplicate_topic_titles = false
SiteSetting.allow_duplicate_topic_titles_category = true
end
it "will allow another topic to be created with the same name but different category" do
expect(new_topic_different_cat).to be_valid
end
it "won't allow another topic to be created with the same name in same category" do
expect(new_topic).not_to be_valid
end
end
end end
context 'html in title' do context 'html in title' do