|
|
|
@@ -46,12 +46,16 @@ class Tag < ApplicationRecord |
|
|
|
scope :nico_tags, -> { where(category: :nico) } |
|
|
|
|
|
|
|
CATEGORY_PREFIXES = { |
|
|
|
'gen:' => :general, |
|
|
|
'djk:' => :deerjikist, |
|
|
|
'meme:' => :meme, |
|
|
|
'chr:' => :character, |
|
|
|
'mtr:' => :material, |
|
|
|
'meta:' => :meta }.freeze |
|
|
|
'general:' => :general, |
|
|
|
'gen:' => :general, |
|
|
|
'deerjikist:' => :deerjikist, |
|
|
|
'djk:' => :deerjikist, |
|
|
|
'meme:' => :meme, |
|
|
|
'character:' => :character, |
|
|
|
'chr:' => :character, |
|
|
|
'material:' => :material, |
|
|
|
'mtr:' => :material, |
|
|
|
'meta:' => :meta }.freeze |
|
|
|
|
|
|
|
def name= val |
|
|
|
(self.tag_name ||= build_tag_name).name = val |
|
|
|
@@ -78,13 +82,13 @@ class Tag < ApplicationRecord |
|
|
|
end |
|
|
|
|
|
|
|
def self.normalise_tags tag_names, with_tagme: true, deny_nico: true |
|
|
|
if deny_nico && tag_names.any? { |n| n.start_with?('nico:') } |
|
|
|
if deny_nico && tag_names.any? { |n| n.downcase.start_with?('nico:') } |
|
|
|
raise NicoTagNormalisationError |
|
|
|
end |
|
|
|
|
|
|
|
tags = tag_names.map do |name| |
|
|
|
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.start_with?(p) } || ['', nil] |
|
|
|
name = TagName.canonicalise(name.delete_prefix(pf)).first |
|
|
|
pf, cat = CATEGORY_PREFIXES.find { |p, _| name.downcase.start_with?(p) } || ['', nil] |
|
|
|
name = TagName.canonicalise(name.sub(/\A#{ pf }/i, '')).first |
|
|
|
find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag| |
|
|
|
if cat && tag.category != cat |
|
|
|
tag.update!(category: cat) |
|
|
|
@@ -134,8 +138,8 @@ class Tag < ApplicationRecord |
|
|
|
|
|
|
|
def nico_tag_name_must_start_with_nico |
|
|
|
n = name.to_s |
|
|
|
if ((category == 'nico' && !(n.start_with?('nico:'))) || |
|
|
|
(category != 'nico' && n.start_with?('nico:'))) |
|
|
|
if ((category == 'nico' && !(n.downcase.start_with?('nico:'))) || |
|
|
|
(category != 'nico' && n.downcase.start_with?('nico:'))) |
|
|
|
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.' |
|
|
|
end |
|
|
|
end |
|
|
|
|