このコミットが含まれているのは:
@@ -9,8 +9,8 @@ class Post < ApplicationRecord
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
has_many :tags, through: :active_post_tags
|
||||
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
has_many :post_similarities, dependent: :destroy
|
||||
has_many :user_post_views, dependent: :delete_all
|
||||
has_many :post_similarities, dependent: :delete_all
|
||||
|
||||
has_one_attached :thumbnail
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ class Tag < ApplicationRecord
|
||||
class_name: 'TagImplication', foreign_key: :tag_id, dependent: :destroy
|
||||
has_many :parents, through: :reversed_tag_implications, source: :parent_tag
|
||||
|
||||
has_many :tag_similarities, dependent: :delete_all
|
||||
|
||||
belongs_to :tag_name
|
||||
delegate :name, to: :tag_name, allow_nil: true
|
||||
validates :tag_name, presence: true
|
||||
@@ -72,7 +74,7 @@ class Tag < ApplicationRecord
|
||||
|
||||
tags = tag_names.map do |name|
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name = name.delete_prefix(pf)
|
||||
name = TagName.canonicalise(name.delete_prefix(pf)).first
|
||||
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag|
|
||||
if cat && tag.category != cat
|
||||
tag.update!(category: cat)
|
||||
@@ -109,6 +111,8 @@ class Tag < ApplicationRecord
|
||||
|
||||
def self.find_or_create_by_tag_name! name, category:
|
||||
tn = TagName.find_or_create_by!(name: name.to_s.strip)
|
||||
tn = tn.canonical if tn.canonical_id?
|
||||
|
||||
Tag.find_or_create_by!(tag_name_id: tn.id) do |t|
|
||||
t.category = category
|
||||
end
|
||||
@@ -127,7 +131,7 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
|
||||
def tag_name_must_be_canonical
|
||||
if tag_name&.canonical_id
|
||||
if tag_name&.canonical_id?
|
||||
errors.add :tag_name, 'tag_names へは実体を示す必要があります.'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -9,6 +9,16 @@ class TagName < ApplicationRecord
|
||||
|
||||
validate :canonical_must_be_canonical
|
||||
validate :alias_name_must_not_have_prefix
|
||||
validate :canonical_must_not_be_present_with_tag_or_wiki_page
|
||||
|
||||
def self.canonicalise names
|
||||
names = Array(names).map { |n| n.to_s.strip }.reject(&:blank?)
|
||||
return [] if names.blank?
|
||||
|
||||
tns = TagName.includes(:canonical).where(name: names).index_by(&:name)
|
||||
|
||||
names.map { |name| tns[name]&.canonical&.name || name }.uniq
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -23,4 +33,10 @@ class TagName < ApplicationRecord
|
||||
errors.add :name, 'エーリアス名にプレフィクスを含むことはできません.'
|
||||
end
|
||||
end
|
||||
|
||||
def canonical_must_not_be_present_with_tag_or_wiki_page
|
||||
if canonical_id? && (tag || wiki_page)
|
||||
errors.add :canonical, 'タグもしくは Wiki の参照がある名前はエーリアスになれません.'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする