タグの合併処理追加(#282) #284
@@ -150,15 +150,18 @@ class Tag < ApplicationRecord
|
|||||||
next if st == target_tag
|
next if st == target_tag
|
||||||
|
|
||||||
st.post_tags.find_each do |pt|
|
st.post_tags.find_each do |pt|
|
||||||
begin
|
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id)
|
||||||
pt.update!(tag: target_tag)
|
|
||||||
rescue ActiveRecord::RecordNotUnique
|
|
||||||
pt.discard_by!(nil)
|
pt.discard_by!(nil)
|
||||||
|
# discard 後の update! は禁止なので DB を直に更新
|
||||||
|
pt.update_columns(tag_id: target_tag.id)
|
||||||
|
else
|
||||||
|
pt.update!(tag: target_tag)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
tag_name = st.tag_name
|
tag_name = st.tag_name
|
||||||
st.destroy!
|
st.destroy!
|
||||||
|
tag_name.reload
|
||||||
tag_name.update!(canonical: target_tag.tag_name)
|
tag_name.update!(canonical: target_tag.tag_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Tag, type: :model do
|
||||||
|
describe '.merge_tags' do
|
||||||
|
let!(:target_tag) { create(:tag) }
|
||||||
|
let!(:source_tag) { create(:tag) }
|
||||||
|
|
||||||
|
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') }
|
||||||
|
|
||||||
|
context 'when merging a simple source tag' do
|
||||||
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
|
||||||
|
it 'moves the post_tag, deletes the source tag, and aliases the source tag_name' do
|
||||||
|
described_class.merge_tags(target_tag, [source_tag])
|
||||||
|
|
||||||
|
expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
||||||
|
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||||
|
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the target already has the same post_tag' 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 duplicate source post_tag and keeps one active target post_tag' do
|
||||||
|
described_class.merge_tags(target_tag, [source_tag])
|
||||||
|
|
||||||
|
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id)
|
||||||
|
discarded_source = PostTag.with_discarded.find(source_post_tag.id)
|
||||||
|
|
||||||
|
expect(active.count).to eq(1)
|
||||||
|
expect(discarded_source.discarded_at).to be_present
|
||||||
|
expect(Tag.exists?(source_tag.id)).to be(false)
|
||||||
|
expect(source_tag.tag_name.reload.canonical_id).to eq(target_tag.tag_name_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when source_tags includes the target itself' do
|
||||||
|
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||||
|
|
||||||
|
it 'ignores the target tag in source_tags' 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(source_post_tag.reload.tag_id).to eq(target_tag.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when aliasing the source tag_name is invalid' 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,
|
||||||
|
created_user: create_admin_user!,
|
||||||
|
updated_user: create_admin_user!)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rolls back the transaction' do
|
||||||
|
expect {
|
||||||
|
described_class.merge_tags(target_tag, [source_tag])
|
||||||
|
}.to raise_error(ActiveRecord::RecordInvalid)
|
||||||
|
|
||||||
|
expect(Tag.exists?(source_tag.id)).to be(true)
|
||||||
|
expect(source_post_tag.reload.tag_id).to eq(source_tag.id)
|
||||||
|
expect(source_tag.tag_name.reload.canonical_id).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
# spec/requests/tag_children_spec.rb
|
|
||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
RSpec.describe "TagChildren", type: :request do
|
RSpec.describe "TagChildren", type: :request do
|
||||||
|
|||||||
新しい課題から参照
ユーザをブロックする