#281 テストまだ通ってないので要確認
このコミットが含まれているのは:
@@ -160,9 +160,15 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
tag_name = st.tag_name
|
||||
nico_flg = st.nico?
|
||||
st.destroy!
|
||||
tag_name.reload
|
||||
tag_name.update!(canonical: target_tag.tag_name)
|
||||
|
||||
if nico_flg
|
||||
tag_name.destroy!
|
||||
else
|
||||
tag_name.update!(canonical: target_tag.tag_name)
|
||||
end
|
||||
end
|
||||
|
||||
# 投稿件数を再集計
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
class TagName < ApplicationRecord
|
||||
before_validation :sanitise_name
|
||||
|
||||
has_one :tag
|
||||
has_one :wiki_page
|
||||
|
||||
@@ -22,6 +24,10 @@ class TagName < ApplicationRecord
|
||||
|
||||
private
|
||||
|
||||
def sanitise_name
|
||||
self.name = TagNameSanitisationRule.sanitise(name)
|
||||
end
|
||||
|
||||
def canonical_must_be_canonical
|
||||
if canonical&.canonical_id?
|
||||
errors.add :canonical, 'canonical は実体を示す必要があります.'
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
class TagNameSanitisationRule < ApplicationRecord
|
||||
include Discard::Model
|
||||
|
||||
self.primary_key = :priority
|
||||
|
||||
default_scope -> { kept }
|
||||
|
||||
validates :source_pattern, presence: true, uniqueness: true
|
||||
|
||||
validate :source_pattern_must_be_regexp
|
||||
|
||||
class << self
|
||||
def sanitise(name) =
|
||||
rules.reduce(name.dup) { |name, (pattern, replacement)| name.gsub(pattern, replacement) }
|
||||
|
||||
def apply!
|
||||
TagName.find_each do |tn|
|
||||
name = sanitise(tn.name)
|
||||
next if name == tn.name
|
||||
|
||||
TagName.transaction do
|
||||
existing_tn = TagName.find_by(name:)
|
||||
if existing_tn
|
||||
existing_tn = existing_tn.canonical || existing_tn
|
||||
next if existing_tn.id == tn.id
|
||||
|
||||
if existing_tn.tag
|
||||
Tag.merge_tags!(existing_tn.tag, tn.tag) if tn.tag
|
||||
elsif tn.tag
|
||||
tn.tag.update_columns(tag_name_id: existing_tn.id, updated_at: Time.current)
|
||||
end
|
||||
tn.destroy!
|
||||
|
||||
next
|
||||
end
|
||||
|
||||
# TagName 側の自動サニタイズを回避
|
||||
tn.update_columns(name:, updated_at: Time.current)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def rules = kept.order(:priority).map { |r| [Regexp.new(r.source_pattern), r.replacement] }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def source_pattern_must_be_regexp
|
||||
Regexp.new(source_pattern)
|
||||
rescue RegexpError
|
||||
errors.add :source_pattern, '変な正規表現だね〜(笑)'
|
||||
end
|
||||
end
|
||||
新しい課題から参照
ユーザをブロックする