This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
class NicoTagRelation < ApplicationRecord
|
||||
belongs_to :nico_tag, class_name: 'Tag', foreign_key: 'nico_tag_id'
|
||||
belongs_to :tag, class_name: 'Tag', foreign_key: 'tag_id'
|
||||
belongs_to :nico_tag, class_name: 'Tag'
|
||||
belongs_to :tag, class_name: 'Tag'
|
||||
|
||||
validates :nico_tag_id, presence: true
|
||||
validates :tag_id, presence: true
|
||||
@@ -14,4 +14,10 @@ class NicoTagRelation < ApplicationRecord
|
||||
errors.add :nico_tag_id, 'タグのカテゴリがニコニコである必要があります.'
|
||||
end
|
||||
end
|
||||
|
||||
def tag_mustnt_be_nico
|
||||
if tag && tag.category == 'nico'
|
||||
errors.add :tag_id, '連携先タグのカテゴリはニコニコであってはなりません.'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,14 @@ class Tag < ApplicationRecord
|
||||
has_many :post_tags, dependent: :destroy
|
||||
has_many :posts, through: :post_tags
|
||||
has_many :tag_aliases, dependent: :destroy
|
||||
has_many :wiki_pages, dependent: :nullify
|
||||
|
||||
has_many :nico_tag_relations, foreign_key: :nico_tag_id, dependent: :destroy
|
||||
has_many :linked_tags, through: :nico_tag_relations, source: :tag
|
||||
|
||||
has_many :reversed_nico_tag_relations, class_name: 'NicoTagRelation',
|
||||
foreign_key: :tag_id,
|
||||
dependent: :destroy
|
||||
has_many :linked_nico_tags, through: :reversed_nico_tag_relations, source: :nico_tag
|
||||
|
||||
enum :category, { deerjikist: 'deerjikist',
|
||||
meme: 'meme',
|
||||
@@ -17,7 +24,7 @@ class Tag < ApplicationRecord
|
||||
|
||||
validate :nico_tag_name_must_start_with_nico
|
||||
|
||||
scope :nico_tags, -> { where category: :nico }
|
||||
scope :nico_tags, -> { where(category: :nico) }
|
||||
|
||||
def self.tagme
|
||||
@tagme ||= Tag.find_or_initialize_by(name: 'タグ希望') do |tag|
|
||||
@@ -34,7 +41,8 @@ class Tag < ApplicationRecord
|
||||
private
|
||||
|
||||
def nico_tag_name_must_start_with_nico
|
||||
if category == 'nico' && name&.[](0, 5) != 'nico:'
|
||||
if ((category == 'nico' && name&.[](0, 5) != 'nico:') ||
|
||||
(category != 'nico' && name&.[](0, 5) == 'nico:'))
|
||||
errors.add :name, 'ニコニコ・タグの命名規則に反してゐます.'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user