This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
class Tag < ApplicationRecord
|
||||
class NicoTagNormalisationError < StandardError
|
||||
;
|
||||
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'
|
||||
@@ -65,6 +69,8 @@ class Tag < ApplicationRecord
|
||||
|
||||
def self.normalise_tags tag_names, with_tagme: true
|
||||
tags = tag_names.map do |name|
|
||||
raise NicoTagNormalisationError if name.start_with?('nico:')
|
||||
|
||||
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil]
|
||||
name.delete_prefix!(pf)
|
||||
find_or_create_by_tag_name!(name, category: (cat || 'general')).tap do |tag|
|
||||
|
||||
Reference in New Issue
Block a user