#281 テストまだ通ってないので要確認
このコミットが含まれているのは:
@@ -160,10 +160,16 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
tag_name = st.tag_name
|
||||
nico_flg = st.nico?
|
||||
st.destroy!
|
||||
tag_name.reload
|
||||
|
||||
if nico_flg
|
||||
tag_name.destroy!
|
||||
else
|
||||
tag_name.update!(canonical: target_tag.tag_name)
|
||||
end
|
||||
end
|
||||
|
||||
# 投稿件数を再集計
|
||||
target_tag.update_columns(post_count: PostTag.kept.where(tag: target_tag).count)
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1,31 @@
|
||||
class CreateTagNameSanitisationRules < ActiveRecord::Migration[8.0]
|
||||
def up
|
||||
create_table :tag_name_sanitisation_rules, id: :integer, primary_key: :priority do |t|
|
||||
t.string :source_pattern, null: false
|
||||
t.string :replacement, null: false
|
||||
t.timestamps
|
||||
t.datetime :discarded_at
|
||||
t.index :source_pattern, unique: true
|
||||
t.index :discarded_at
|
||||
end
|
||||
|
||||
now = ActiveRecord::Base.connection.quote(Time.current)
|
||||
execute <<~SQL
|
||||
INSERT INTO
|
||||
tag_name_sanitisation_rules(priority, source_pattern, replacement, created_at, updated_at)
|
||||
VALUES
|
||||
(10, '\\\\*', '_', #{ now }, #{ now })
|
||||
, (20, '\\\\?', '_', #{ now }, #{ now })
|
||||
, (25, '\\\\/', '_', #{ now }, #{ now })
|
||||
, (30, '_+', '_', #{ now }, #{ now })
|
||||
, (40, '_$', '', #{ now }, #{ now })
|
||||
, (45, '^([^:]+\\\\:)?_', '\\\\1', #{ now }, #{ now })
|
||||
, (50, '^([^:]+\\\\:)?$', '\\\\1null', #{ now }, #{ now })
|
||||
;
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
drop_table :tag_name_sanitisation_rules
|
||||
end
|
||||
end
|
||||
生成ファイル
+11
-1
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2026_03_09_123200) do
|
||||
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@@ -127,6 +127,16 @@ ActiveRecord::Schema[8.0].define(version: 2026_03_03_122700) do
|
||||
t.index ["tag_id"], name: "index_tag_implications_on_tag_id"
|
||||
end
|
||||
|
||||
create_table "tag_name_sanitisation_rules", primary_key: "priority", id: :integer, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "source_pattern", null: false
|
||||
t.string "replacement", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "discarded_at"
|
||||
t.index ["discarded_at"], name: "index_tag_name_sanitisation_rules_on_discarded_at"
|
||||
t.index ["source_pattern"], name: "index_tag_name_sanitisation_rules_on_source_pattern", unique: true
|
||||
end
|
||||
|
||||
create_table "tag_names", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.bigint "canonical_id"
|
||||
|
||||
@@ -108,7 +108,7 @@ namespace :nico do
|
||||
desired_non_nico_tag_ids = []
|
||||
|
||||
datum['tags'].each do |raw|
|
||||
name = "nico:#{ raw }"
|
||||
name = TagNameSanitisationRule.sanitise("nico:#{ raw }")
|
||||
tag = Tag.find_or_create_by_tag_name!(name, category: :nico)
|
||||
desired_nico_tag_based_ids << tag.id
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TagNameSanitisationRule, type: :model do
|
||||
describe '.sanitise' do
|
||||
before do
|
||||
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
|
||||
described_class.create!(priority: 20, source_pattern: 'ABC', replacement: 'abc')
|
||||
end
|
||||
|
||||
it 'applies rules in priority order' do
|
||||
expect(described_class.sanitise('A_B_C')).to eq('abc')
|
||||
end
|
||||
|
||||
it 'does not fail when a rule does not match' do
|
||||
expect { described_class.sanitise('xyz') }.not_to raise_error
|
||||
expect(described_class.sanitise('xyz')).to eq('xyz')
|
||||
end
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
it 'is invalid when source_pattern is not a valid regexp' do
|
||||
rule = described_class.new(priority: 10, source_pattern: '[', replacement: '')
|
||||
expect(rule).to be_invalid
|
||||
expect(rule.errors[:source_pattern]).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
describe '.apply!' do
|
||||
before do
|
||||
described_class.create!(priority: 10, source_pattern: '_', replacement: '')
|
||||
end
|
||||
|
||||
context 'when no conflicting tag_name exists' do
|
||||
let!(:tag_name) { TagName.create!(name: 'foo_bar') }
|
||||
|
||||
it 'renames the tag_name' do
|
||||
described_class.apply!
|
||||
expect(tag_name.reload.name).to eq('foobar')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when a conflicting canonical tag_name exists' do
|
||||
let!(:existing) { TagName.create!(name: 'foobar') }
|
||||
let!(:source) { TagName.create!(name: 'foo_bar') }
|
||||
|
||||
it 'deletes the source tag_name' do
|
||||
described_class.apply!
|
||||
expect(TagName.exists?(source.id)).to be(false)
|
||||
expect(existing.reload.name).to eq('foobar')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the source tag_name has a tag and the existing one has no tag' do
|
||||
let!(:existing) { TagName.create!(name: 'foobar') }
|
||||
let!(:source_tag) { create(:tag, name: 'foo_bar', category: :general) }
|
||||
|
||||
it 'moves the tag to the existing tag_name' do
|
||||
described_class.apply!
|
||||
expect(source_tag.reload.tag_name_id).to eq(existing.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,28 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe TagName, type: :model do
|
||||
describe 'before_validation :sanitise_name' do
|
||||
before do
|
||||
TagNameSanitisationRule.create!(priority: 10, source_pattern: '_', replacement: '')
|
||||
end
|
||||
|
||||
it 'sanitises name on create' do
|
||||
tag_name = TagName.create!(name: 'foo_bar')
|
||||
expect(tag_name.reload.name).to eq('foobar')
|
||||
end
|
||||
|
||||
it 'sanitises name on update' do
|
||||
tag_name = TagName.create!(name: 'foobar')
|
||||
tag_name.update!(name: 'foo_bar')
|
||||
expect(tag_name.reload.name).to eq('foobar')
|
||||
end
|
||||
|
||||
it 'becomes invalid when sanitised name conflicts with another tag_name' do
|
||||
TagName.create!(name: 'foobar')
|
||||
tag_name = TagName.new(name: 'foo_bar')
|
||||
|
||||
expect(tag_name).to be_invalid
|
||||
expect(tag_name.errors[:name]).to be_present
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,10 +2,13 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe Tag, type: :model do
|
||||
describe '.merge_tags!' do
|
||||
let!(:target_tag) { create(:tag) }
|
||||
let!(:source_tag) { create(:tag) }
|
||||
let!(:target_tag) { create(:tag, category: :general) }
|
||||
let!(:source_tag) { create(:tag, category: :general) }
|
||||
let!(:source_tag_name) { source_tag.tag_name }
|
||||
|
||||
let!(:post_record) { Post.create!(url: 'https://example.com/posts/1', title: 'test post') }
|
||||
let!(:post_record) do
|
||||
Post.create!(url: 'https://example.com/posts/1', title: 'test post')
|
||||
end
|
||||
|
||||
context 'when merging a simple source tag' do
|
||||
let!(:source_post_tag) { PostTag.create!(post: post_record, tag: source_tag) }
|
||||
@@ -15,7 +18,8 @@ RSpec.describe Tag, type: :model do
|
||||
|
||||
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)
|
||||
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
|
||||
|
||||
@@ -31,8 +35,10 @@ RSpec.describe Tag, type: :model do
|
||||
|
||||
expect(active.count).to eq(1)
|
||||
expect(discarded_source.discarded_at).to be_present
|
||||
expect(discarded_source.tag_id).to eq(source_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)
|
||||
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
|
||||
|
||||
@@ -45,6 +51,7 @@ RSpec.describe Tag, type: :model do
|
||||
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)
|
||||
expect(target_tag.reload.post_count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -52,9 +59,10 @@ RSpec.describe Tag, type: :model 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!)
|
||||
updated_user: create_admin_user!
|
||||
)
|
||||
end
|
||||
|
||||
it 'rolls back the transaction' do
|
||||
@@ -64,7 +72,20 @@ RSpec.describe Tag, type: :model do
|
||||
|
||||
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
|
||||
expect(source_tag_name.reload.canonical_id).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when merging a nico source tag' do
|
||||
let!(:target_tag) { create(:tag, category: :nico, name: 'nico:foo') }
|
||||
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
|
||||
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)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする