タグの合併処理追加(#282) #284

マージ済み
みてるぞ が 6 個のコミットを feature/282 から main へマージ 2026-03-08 15:46:06 +09:00
2個のファイルの変更10行の追加6行の削除
コミット 7c2c94bc48 の変更だけを表示してゐます - すべてのコミットを表示
+5 -1
ファイルの表示
@@ -23,6 +23,8 @@ class Tag < ApplicationRecord
has_many :parents, through: :reversed_tag_implications, source: :parent_tag has_many :parents, through: :reversed_tag_implications, source: :parent_tag
has_many :tag_similarities, dependent: :delete_all has_many :tag_similarities, dependent: :delete_all
has_many :tag_similarities_as_target,
class_name: 'TagSimilarity', foreign_key: :target_tag_id, dependent: :delete_all
has_many :deerjikists, dependent: :delete_all has_many :deerjikists, dependent: :delete_all
@@ -140,7 +142,7 @@ class Tag < ApplicationRecord
retry retry
end end
def self.merge_tags target_tag, source_tags def self.merge_tags! target_tag, source_tags
target_tag => Tag target_tag => Tag
Tag.transaction do Tag.transaction do
@@ -166,6 +168,8 @@ class Tag < ApplicationRecord
end end
end end
target_tag.update!(post_count: PostTag.kept.where(tag: target_tag).count)
target_tag target_tag
end end
+5 -5
ファイルの表示
@@ -1,7 +1,7 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Tag, type: :model do RSpec.describe Tag, type: :model do
describe '.merge_tags' do describe '.merge_tags!' do
let!(:target_tag) { create(:tag) } let!(:target_tag) { create(:tag) }
let!(:source_tag) { create(:tag) } let!(:source_tag) { create(:tag) }
@@ -11,7 +11,7 @@ RSpec.describe Tag, type: :model do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } 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 it 'moves the post_tag, deletes the source tag, and aliases the source tag_name' do
described_class.merge_tags(target_tag, [source_tag]) described_class.merge_tags!(target_tag, [source_tag])
expect(source_post_tag.reload.tag_id).to eq(target_tag.id) expect(source_post_tag.reload.tag_id).to eq(target_tag.id)
expect(Tag.exists?(source_tag.id)).to be(false) expect(Tag.exists?(source_tag.id)).to be(false)
@@ -24,7 +24,7 @@ RSpec.describe Tag, type: :model do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_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 it 'discards the duplicate source post_tag and keeps one active target post_tag' do
described_class.merge_tags(target_tag, [source_tag]) described_class.merge_tags!(target_tag, [source_tag])
active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id) active = PostTag.kept.where(post_id: post_record.id, tag_id: target_tag.id)
discarded_source = PostTag.with_discarded.find(source_post_tag.id) discarded_source = PostTag.with_discarded.find(source_post_tag.id)
@@ -40,7 +40,7 @@ RSpec.describe Tag, type: :model do
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) } let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
it 'ignores the target tag in source_tags' do it 'ignores the target tag in source_tags' do
described_class.merge_tags(target_tag, [source_tag, target_tag]) described_class.merge_tags!(target_tag, [source_tag, target_tag])
expect(Tag.exists?(target_tag.id)).to be(true) expect(Tag.exists?(target_tag.id)).to be(true)
expect(Tag.exists?(source_tag.id)).to be(false) expect(Tag.exists?(source_tag.id)).to be(false)
@@ -59,7 +59,7 @@ RSpec.describe Tag, type: :model do
it 'rolls back the transaction' do it 'rolls back the transaction' do
expect { expect {
described_class.merge_tags(target_tag, [source_tag]) described_class.merge_tags!(target_tag, [source_tag])
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
expect(Tag.exists?(source_tag.id)).to be(true) expect(Tag.exists?(source_tag.id)).to be(true)