feat: タグ名を別管理に変更(#215) (#219)

Merge branch 'main' into feature/215

#215 ニコニコ同期テスト

#215 テスト・ケース追加

#215 テスト・ケース追加

#215 テスト・ケース追加

#215 テスト・ケース追加

Merge remote-tracking branch 'origin/main' into feature/215

Merge branch 'main' into feature/215

#215

#215

Merge remote-tracking branch 'origin/main' into feature/215

#215

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #219
This commit was merged in pull request #219.
This commit is contained in:
2026-01-15 12:40:41 +09:00
parent 74141f2a84
commit fa2030f9a5
34 changed files with 1268 additions and 119 deletions
+4
View File
@@ -1,6 +1,10 @@
class PostTag < ApplicationRecord
include Discard::Model
before_destroy do
raise ActiveRecord::ReadOnlyRecord, '消さないでください.'
end
belongs_to :post
belongs_to :tag, counter_cache: :post_count
belongs_to :created_user, class_name: 'User', optional: true
+28 -13
View File
@@ -21,6 +21,10 @@ class Tag < ApplicationRecord
dependent: :destroy
has_many :parents, through: :reversed_tag_implications, source: :parent_tag
belongs_to :tag_name
delegate :name, to: :tag_name, allow_nil: true
validates :tag_name, presence: true
enum :category, { deerjikist: 'deerjikist',
meme: 'meme',
character: 'character',
@@ -29,7 +33,6 @@ class Tag < ApplicationRecord
nico: 'nico',
meta: 'meta' }
validates :name, presence: true, length: { maximum: 255 }
validates :category, presence: true, inclusion: { in: Tag.categories.keys }
validate :nico_tag_name_must_start_with_nico
@@ -44,31 +47,35 @@ class Tag < ApplicationRecord
'mtr:' => 'material',
'meta:' => 'meta' }.freeze
def name= val
(self.tag_name ||= build_tag_name).name = val
end
def has_wiki
tag_name&.wiki_page.present?
end
def self.tagme
@tagme ||= Tag.find_or_create_by!(name: 'タグ希望') do |tag|
tag.category = 'meta'
end
@tagme ||= find_or_create_by_tag_name!('タグ希望', category: 'meta')
end
def self.bot
@bot ||= Tag.find_or_create_by!(name: 'bot操作') do |tag|
tag.category = 'meta'
end
@bot ||= find_or_create_by_tag_name!('bot操作', category: 'meta')
end
def self.normalise_tags tag_names, with_tagme: true
tags = tag_names.map do |name|
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
name.delete_prefix!(pf)
Tag.find_or_initialize_by(name:).tap do |tag|
find_or_create_by_tag_name!(name, category: (cat || 'general')).tap do |tag|
if cat && tag.category != cat
tag.category = cat
tag.save!
tag.update!(category: cat)
end
end
end
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme)
tags.uniq
tags.uniq(&:id)
end
def self.expand_parent_tags tags
@@ -94,11 +101,19 @@ class Tag < ApplicationRecord
(result + tags).uniq { |t| t.id }
end
def self.find_or_create_by_tag_name!(name, category:)
tn = TagName.find_or_create_by!(name: name.to_s.strip)
Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
t.category = category
end
end
private
def nico_tag_name_must_start_with_nico
if ((category == 'nico' && !(name.start_with?('nico:'))) ||
(category != 'nico' && name.start_with?('nico:')))
n = name.to_s
if ((category == 'nico' && !(n.start_with?('nico:'))) ||
(category != 'nico' && n.start_with?('nico:')))
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
end
end
+9
View File
@@ -0,0 +1,9 @@
class TagName < ApplicationRecord
has_one :tag
has_one :wiki_page
belongs_to :canonical, class_name: 'TagName', optional: true
has_many :aliases, class_name: 'TagName', foreign_key: :canonical_id
validates :name, presence: true, length: { maximum: 255 }, uniqueness: true
end
+10 -1
View File
@@ -11,7 +11,16 @@ class WikiPage < ApplicationRecord
foreign_key: :redirect_page_id,
dependent: :nullify
validates :title, presence: true, length: { maximum: 255 }, uniqueness: true
belongs_to :tag_name
validates :tag_name, presence: true
def title
tag_name.name
end
def title= val
(self.tag_name ||= build_tag_name).name = val
end
def current_revision
wiki_revisions.order(id: :desc).first