This commit is contained in:
@@ -26,6 +26,14 @@ class Tag < ApplicationRecord
|
||||
|
||||
scope :nico_tags, -> { where(category: :nico) }
|
||||
|
||||
CATEGORY_PREFIXES = {
|
||||
'gen:' => 'general',
|
||||
'djk:' => 'deerjikist',
|
||||
'meme:' => 'meme',
|
||||
'chr:' => 'character',
|
||||
'mtr:' => 'material',
|
||||
'meta:' => 'meta' }.freeze
|
||||
|
||||
def self.tagme
|
||||
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
|
||||
tag.category = 'meta'
|
||||
@@ -38,11 +46,26 @@ class Tag < ApplicationRecord
|
||||
end
|
||||
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|
|
||||
if cat && tag.category != cat
|
||||
tag.category = cat
|
||||
tag.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
tags << Tag.tagme if with_tagme && tags.size < 20 && tags.none?(Tag.tagme)
|
||||
tags.uniq
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def nico_tag_name_must_start_with_nico
|
||||
if ((category == 'nico' && name&.[](0, 5) != 'nico:') ||
|
||||
(category != 'nico' && name&.[](0, 5) == 'nico:'))
|
||||
if ((category == 'nico' && !(name.start_with?('nico:'))) ||
|
||||
(category != 'nico' && name.start_with?('nico:')))
|
||||
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user