ニジラー管理(#247) (#275)

#247

#247

Co-authored-by: miteruzo <miteruzo@naver.com>
Reviewed-on: #275
This commit was merged in pull request #275.
This commit is contained in:
2026-03-05 20:35:11 +09:00
parent 7d1a87f452
commit e0e7a22c38
12 changed files with 438 additions and 4 deletions
@@ -0,0 +1,47 @@
class DeerjikistsController < ApplicationController
def show
platform = params[:platform].to_s.strip
code = params[:code].to_s.strip
return head :bad_request if platform.blank? || code.blank?
deerjikist = Deerjikist
.joins(:tag)
.includes(tag: :tag_name)
.find_by(platform:, code:)
if deerjikist
render json: DeerjikistRepr.base(deerjikist)
else
head :not_found
end
end
def update
return head :unauthorized unless current_user
return head :forbidden unless current_user.member?
platform = params[:platform].to_s.strip
code = params[:code].to_s.strip
tag_id = params[:tag_id].to_i
return head :bad_request if platform.blank? || code.blank? || tag_id <= 0
deerjikist = Deerjikist.find_or_initialize_by(platform:, code:).tap do |d|
d.tag_id = tag_id
d.save!
end
render json: DeerjikistRepr.base(deerjikist)
end
def destroy
return head :unauthorized unless current_user
return head :forbidden unless current_user.member?
platform = params[:platform].to_s.strip
code = params[:code].to_s.strip
return head :bad_request if platform.blank? || code.blank?
Deerjikist.find([platform, code]).destroy!
head :no_content
end
end
@@ -86,6 +86,27 @@ class TagsController < ApplicationController
end
end
def deerjikists
tag = Tag.joins(:tag_name)
.includes(:tag_name, tag_name: :wiki_page)
.find_by(id: params[:id])
return head :not_found unless tag
render json: DeerjikistRepr.many(tag.deerjikists)
end
def deerjikists_by_name
name = params[:name].to_s.strip
return head :bad_request if name.blank?
tag = Tag.joins(:tag_name)
.includes(:tag_name, tag_name: :wiki_page)
.find_by(tag_names: { name: })
return head :not_found unless tag
render json: DeerjikistRepr.many(tag.deerjikists)
end
def update
return head :unauthorized unless current_user
return head :forbidden unless current_user.member?
+21
View File
@@ -0,0 +1,21 @@
class Deerjikist < ApplicationRecord
self.primary_key = :platform, :code
belongs_to :tag
validates :platform, presence: true
validates :code, presence: true
validates :tag_id, presence: true
validate :tag_must_be_deerjikist
enum :platform, nico: 'nico', youtube: 'youtube'
private
def tag_must_be_deerjikist
if tag && !(tag.deerjikist?)
errors.add :tag, 'タグはニジラー・カテゴリである必要があります.'
end
end
end
+9
View File
@@ -24,6 +24,8 @@ class Tag < ApplicationRecord
has_many :tag_similarities, dependent: :delete_all
has_many :deerjikists, dependent: :delete_all
belongs_to :tag_name
delegate :wiki_page, to: :tag_name
@@ -42,6 +44,7 @@ class Tag < ApplicationRecord
validate :nico_tag_name_must_start_with_nico
validate :tag_name_must_be_canonical
validate :category_must_be_deerjikist_with_deerjikists
scope :nico_tags, -> { where(category: :nico) }
@@ -149,4 +152,10 @@ class Tag < ApplicationRecord
errors.add :tag_name, 'tag_names へは実体を示す必要があります.'
end
end
def category_must_be_deerjikist_with_deerjikists
if !(deerjikist?) && deerjikists.exists?
errors.add :category, 'ニジラーと紐づいてゐるタグはニジラー・カテゴリである必要があります.'
end
end
end
@@ -0,0 +1,16 @@
# frozen_string_literal: true
module DeerjikistRepr
BASE = { only: [:platform, :code], include: { tag: TagRepr::BASE } }.freeze
module_function
def base deerjikist
deerjikist.as_json(BASE)
end
def many deerjikists
deerjikists.map { |d| base(d) }
end
end