|
|
|
@@ -23,6 +23,8 @@ class Tag < ApplicationRecord |
|
|
|
has_many :parents, through: :reversed_tag_implications, source: :parent_tag |
|
|
|
|
|
|
|
has_many :tag_similarities, dependent: :delete_all |
|
|
|
has_many :tag_similarities_as_target, |
|
|
|
class_name: 'TagSimilarity', foreign_key: :target_tag_id, dependent: :delete_all |
|
|
|
|
|
|
|
has_many :deerjikists, dependent: :delete_all |
|
|
|
|
|
|
|
@@ -46,7 +48,7 @@ class Tag < ApplicationRecord |
|
|
|
validate :tag_name_must_be_canonical |
|
|
|
validate :category_must_be_deerjikist_with_deerjikists |
|
|
|
|
|
|
|
scope :nico_tags, -> { where(category: :nico) } |
|
|
|
scope :nico_tags, -> { nico } |
|
|
|
|
|
|
|
CATEGORY_PREFIXES = { |
|
|
|
'general:' => :general, |
|
|
|
@@ -64,9 +66,7 @@ class Tag < ApplicationRecord |
|
|
|
(self.tag_name ||= build_tag_name).name = val |
|
|
|
end |
|
|
|
|
|
|
|
def has_wiki |
|
|
|
wiki_page.present? |
|
|
|
end |
|
|
|
def has_wiki = wiki_page.present? |
|
|
|
|
|
|
|
def self.tagme |
|
|
|
@tagme ||= find_or_create_by_tag_name!('タグ希望', category: :meta) |
|
|
|
@@ -97,14 +97,12 @@ class Tag < ApplicationRecord |
|
|
|
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) |
|
|
|
end |
|
|
|
tag.update!(category: cat) if cat && tag.category != cat |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
tags << Tag.tagme if with_tagme && tags.size < 10 && tags.none?(Tag.tagme) |
|
|
|
tags << Tag.no_deerjikist if tags.all? { |t| t.category != 'deerjikist' } |
|
|
|
tags << Tag.no_deerjikist if tags.all? { |t| !(t.deerjikist?) } |
|
|
|
tags.uniq(&:id) |
|
|
|
end |
|
|
|
|
|
|
|
@@ -142,12 +140,44 @@ class Tag < ApplicationRecord |
|
|
|
retry |
|
|
|
end |
|
|
|
|
|
|
|
def self.merge_tags! target_tag, source_tags |
|
|
|
target_tag => Tag |
|
|
|
|
|
|
|
Tag.transaction do |
|
|
|
Array(source_tags).compact.uniq.each do |st| |
|
|
|
st => Tag |
|
|
|
|
|
|
|
next if st == target_tag |
|
|
|
|
|
|
|
st.post_tags.find_each do |pt| |
|
|
|
if PostTag.kept.exists?(post_id: pt.post_id, tag_id: target_tag.id) |
|
|
|
pt.discard_by!(nil) |
|
|
|
# discard 後の update! は禁止なので DB を直に更新 |
|
|
|
pt.update_columns(tag_id: target_tag.id, updated_at: Time.current) |
|
|
|
else |
|
|
|
pt.update!(tag: target_tag) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
tag_name = st.tag_name |
|
|
|
st.destroy! |
|
|
|
tag_name.reload |
|
|
|
tag_name.update!(canonical: target_tag.tag_name) |
|
|
|
end |
|
|
|
|
|
|
|
# 投稿件数を再集計 |
|
|
|
target_tag.update_columns(post_count: PostTag.kept.where(tag: target_tag).count) |
|
|
|
end |
|
|
|
|
|
|
|
target_tag.reload |
|
|
|
end |
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
def nico_tag_name_must_start_with_nico |
|
|
|
n = name.to_s |
|
|
|
if ((category == 'nico' && !(n.downcase.start_with?('nico:'))) || |
|
|
|
(category != 'nico' && n.downcase.start_with?('nico:'))) |
|
|
|
if ((nico? && !(n.downcase.start_with?('nico:'))) || |
|
|
|
(!(nico?) && n.downcase.start_with?('nico:'))) |
|
|
|
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.' |
|
|
|
end |
|
|
|
end |
|
|
|
|