feat: 投稿にニコタグ追加できてしまふバグ修正(#125) (#236)
#125 #125 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #236
This commit was merged in pull request #236.
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
class Tag < ApplicationRecord
|
||||
class NicoTagNormalisationError < ArgumentError
|
||||
;
|
||||
end
|
||||
|
||||
has_many :post_tags, dependent: :delete_all, 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'
|
||||
@@ -63,10 +67,14 @@ class Tag < ApplicationRecord
|
||||
@bot ||= find_or_create_by_tag_name!('bot操作', category: 'meta')
|
||||
end
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true
|
||||
def self.normalise_tags tag_names, with_tagme: true, deny_nico: true
|
||||
if deny_nico && tag_names.any? { |n| n.start_with?('nico:') }
|
||||
raise NicoTagNormalisationError
|
||||
end
|
||||
|
||||
tags = tag_names.map do |name|
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name.delete_prefix!(pf)
|
||||
name = name.delete_prefix(pf)
|
||||
find_or_create_by_tag_name!(name, category: (cat || 'general')).tap do |tag|
|
||||
if cat && tag.category != cat
|
||||
tag.update!(category: cat)
|
||||
@@ -101,11 +109,13 @@ class Tag < ApplicationRecord
|
||||
(result + tags).uniq { |t| t.id }
|
||||
end
|
||||
|
||||
def self.find_or_create_by_tag_name!(name, category:)
|
||||
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
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
retry
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
Reference in New Issue
Block a user