| @@ -46,10 +46,14 @@ class Tag < ApplicationRecord | |||||
| scope :nico_tags, -> { where(category: :nico) } | scope :nico_tags, -> { where(category: :nico) } | ||||
| CATEGORY_PREFIXES = { | CATEGORY_PREFIXES = { | ||||
| 'general:' => :general, | |||||
| 'gen:' => :general, | 'gen:' => :general, | ||||
| 'deerjikist:' => :deerjikist, | |||||
| 'djk:' => :deerjikist, | 'djk:' => :deerjikist, | ||||
| 'meme:' => :meme, | 'meme:' => :meme, | ||||
| 'character:' => :character, | |||||
| 'chr:' => :character, | 'chr:' => :character, | ||||
| 'material:' => :material, | |||||
| 'mtr:' => :material, | 'mtr:' => :material, | ||||
| 'meta:' => :meta }.freeze | 'meta:' => :meta }.freeze | ||||
| @@ -78,13 +82,13 @@ class Tag < ApplicationRecord | |||||
| end | end | ||||
| def self.normalise_tags tag_names, with_tagme: true, deny_nico: true | 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 | raise NicoTagNormalisationError | ||||
| end | end | ||||
| tags = tag_names.map do |name| | 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| | find_or_create_by_tag_name!(name, category: (cat || :general)).tap do |tag| | ||||
| if cat && tag.category != cat | if cat && tag.category != cat | ||||
| tag.update!(category: cat) | tag.update!(category: cat) | ||||
| @@ -134,8 +138,8 @@ class Tag < ApplicationRecord | |||||
| def nico_tag_name_must_start_with_nico | def nico_tag_name_must_start_with_nico | ||||
| n = name.to_s | 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, 'ニコニコ・タグの命名規則に反してゐます.' | errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.' | ||||
| end | end | ||||
| end | end | ||||