このコミットが含まれているのは:
2026-03-12 00:46:21 +09:00
コミット 2e2b99f74b
9個のファイルの変更105行の追加44行の削除
+2 -1
ファイルの表示
@@ -69,7 +69,8 @@ RSpec.describe TagNameSanitisationRule, type: :model do
it 'moves the tag to the existing tag_name' do
described_class.apply!
expect(source_tag.reload.tag_name_id).to eq(existing.id)
expected_tag_name_id = existing.canonical_id || existing.id
expect(source_tag.reload.tag_name_id).to eq(expected_tag_name_id)
expect(TagName.exists?(source_tag_name_id)).to be(false)
end
end
+60 -31
ファイルの表示
@@ -13,14 +13,9 @@ RSpec.describe Tag, type: :model do
context 'when merging a simple source tag' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
it 'discards the source post_tag, creates an active target post_tag, ' +
'discards the source tag, and aliases the source tag_name' do
it 'discards the source post_tag, creates an active target post_tag, discards the source tag, and aliases the source tag_name' do
described_class.merge_tags!(target_tag, [source_tag])
expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
source_pt = PostTag.with_discarded.find(source_post_tag.id)
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
@@ -29,7 +24,9 @@ RSpec.describe Tag, type: :model do
expect(active_target).to be_present
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end
end
@@ -37,8 +34,7 @@ RSpec.describe Tag, type: :model do
let!(:target_post_tag) { PostTag.create!(post: post_record, tag: target_tag) }
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
it 'discards the source post_tag, keeps one active target post_tag, ' +
'and aliases the source tag_name' do
it 'discards the source post_tag, keeps one active target post_tag, discards the source tag, and aliases the source tag_name' do
described_class.merge_tags!(target_tag, [source_tag])
source_pt = PostTag.with_discarded.find(source_post_tag.id)
@@ -47,17 +43,12 @@ RSpec.describe Tag, type: :model do
expect(source_pt.discarded_at).to be_present
expect(source_pt.tag_id).to eq(source_tag.id)
expect(active.count).to eq(1)
expect(source_pt.discarded_at).to be_present
expect(source_pt.tag_id).to eq(source_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false)
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
expect(active.first.id).to eq(target_post_tag.id)
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end
end
@@ -67,10 +58,6 @@ RSpec.describe Tag, type: :model do
it 'ignores the target in source_tags while still merging the source tag' do
described_class.merge_tags!(target_tag, [source_tag, target_tag])
expect(Tag.exists?(target_tag.id)).to be(true)
expect(Tag.exists?(source_tag.id)).to be(false)
expect(target_tag.reload.post_count).to eq(1)
source_pt = PostTag.with_discarded.find(source_post_tag.id)
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
@@ -79,23 +66,65 @@ RSpec.describe Tag, type: :model do
expect(source_pt.discarded_at).to be_present
expect(source_pt.tag_id).to eq(source_tag.id)
expect(active_target).to be_present
expect(source_tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
expect(target_tag.reload.post_count).to eq(1)
end
end
context 'when aliasing the source tag_name would be invalid under normal validation' do
context 'when the source tag_name is invalid under sanitisation rules' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
let!(:sanitisation_rule) do
TagNameSanitisationRule.create!(
priority: 99_999,
source_pattern: 'INVALIDTOKEN',
replacement: ''
)
end
before do
source_tag_name.update_columns(
name: "#{ source_tag_name.name }INVALIDTOKEN",
updated_at: Time.current
)
end
it 'still merges, but discards the source tag_name instead of aliasing it' do
described_class.merge_tags!(target_tag, [source_tag])
source_pt = PostTag.with_discarded.find(source_post_tag.id)
active_target = PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)
discarded_source_tag_name = TagName.with_discarded.find(source_tag_name.id)
expect(source_pt.discarded_at).to be_present
expect(source_pt.tag_id).to eq(source_tag.id)
expect(active_target).to be_present
expect(Tag.with_discarded.find(source_tag.id)).to be_discarded
expect(target_tag.reload.post_count).to eq(1)
end
end
context 'when the source tag_name has a wiki_page' do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
let!(:wiki_page) do
WikiPage.create!(
tag_name: source_tag.tag_name,
tag_name: source_tag_name,
created_user: create_admin_user!,
updated_user: create_admin_user!
)
end
it 'still merges by bypassing validations' do
it 'rolls back the transaction' do
expect {
described_class.merge_tags!(target_tag, [source_tag])
}.to raise_error(ActiveRecord::RecordInvalid)
expect(Tag.with_discarded.find(source_tag.id)).not_to be_discarded
expect(TagName.with_discarded.find(source_tag_name.id)).not_to be_discarded
expect(PostTag.kept.find(source_post_tag.id).tag_id).to eq(source_tag.id)
expect(PostTag.kept.find_by(post_id: post_record.id, tag_id: target_tag.id)).to be_nil
expect(source_tag_name.reload.canonical_id).to be_nil
expect(target_tag.reload.post_count).to eq(0)
end
end
@@ -104,16 +133,16 @@ RSpec.describe Tag, type: :model do
let!(:source_tag) { create(:tag, category: :nico, name: 'nico:bar') }
let!(:source_tag_name_id) { source_tag.tag_name_id }
it 'deletes the source tag_name instead of aliasing it' do
it 'discards the source tag_name instead of aliasing it' do
described_class.merge_tags!(target_tag, [source_tag])
expect(Tag.exists?(source_tag.id)).to be(false)
expect(TagName.exists?(source_tag_name_id)).to be(false)
expect(target_tag.reload.post_count).to eq(0)
discarded_source_tag = Tag.with_discarded.find(source_tag.id)
discarded_source_tag_name = TagName.with_discarded.find(source_tag_name_id)
expect(Tag.with_discarded.find(source_tag.id)).not_to be_discarded
expect(PostTag.kept.find(source_post_tag.id).tag_id).to eq(source_tag.id)
expect(source_tag.tag_name.reload.canonical_id).to be_nil
expect(discarded_source_tag).to be_discarded
expect(discarded_source_tag_name).to be_discarded
expect(discarded_source_tag_name.canonical_id).to be_nil
expect(target_tag.reload.post_count).to eq(0)
end
end
end