tags の論理削除対応(#287) (#288)
#287 #287 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #288
This commit was merged in pull request #288.
This commit is contained in:
+16
-14
@@ -1,8 +1,12 @@
|
||||
class Tag < ApplicationRecord
|
||||
include Discard::Model
|
||||
|
||||
class NicoTagNormalisationError < ArgumentError
|
||||
;
|
||||
end
|
||||
|
||||
default_scope -> { kept }
|
||||
|
||||
has_many :post_tags, inverse_of: :tag
|
||||
has_many :active_post_tags, -> { kept }, class_name: 'PostTag', inverse_of: :tag
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
@@ -144,25 +148,23 @@ class Tag < ApplicationRecord
|
||||
target_tag => Tag
|
||||
|
||||
Tag.transaction do
|
||||
Array(source_tags).compact.uniq.each do |st|
|
||||
st => Tag
|
||||
Array(source_tags).compact.uniq.each do |source_tag|
|
||||
source_tag => Tag
|
||||
|
||||
next if st == target_tag
|
||||
next if source_tag == target_tag
|
||||
|
||||
st.post_tags.find_each do |pt|
|
||||
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id)
|
||||
pt.discard_by!(nil)
|
||||
# discard 後の update! は禁止なので DB を直に更新
|
||||
pt.update_columns(tag_id: target_tag.id, updated_at: Time.current)
|
||||
else
|
||||
pt.update!(tag: target_tag)
|
||||
source_tag.post_tags.kept.find_each do |source_pt|
|
||||
post_id = source_pt.post_id
|
||||
source_pt.discard_by!(nil)
|
||||
unless PostTag.kept.exists?(post_id:, tag: target_tag)
|
||||
PostTag.create!(post_id:, tag: target_tag)
|
||||
end
|
||||
end
|
||||
|
||||
tag_name = st.tag_name
|
||||
st.destroy!
|
||||
tag_name.reload
|
||||
tag_name.update!(canonical: target_tag.tag_name)
|
||||
source_tag_name = source_tag.tag_name
|
||||
source_tag.discard!
|
||||
source_tag_name.reload
|
||||
source_tag_name.update!(canonical: target_tag.tag_name)
|
||||
end
|
||||
|
||||
# 投稿件数を再集計
|
||||
|
||||
Reference in New Issue
Block a user