This commit is contained in:
2026-04-05 21:00:13 +09:00
parent f576373c8f
commit 64e7400ed0
14 changed files with 576 additions and 85 deletions
+40 -16
View File
@@ -22,24 +22,35 @@ class MaterialsController < ApplicationController
end
def show
material = Material.includes(:tag, :created_by_user).with_attached_file.find_by(id: params[:id])
material =
Material
.includes(:tag)
.with_attached_file
.find_by(id: params[:id])
return head :not_found unless material
render json: material_json(material)
render json: material.as_json(methods: [:content_type]).merge(
file: if material.file.attached?
rails_storage_proxy_url(material.file, only_path: false)
end,
tag: TagRepr.base(material.tag))
end
def create
return head :unauthorized unless current_user
return head :forbidden unless current_user.gte_member?
tag_name_raw = params[:tag].to_s.strip
file = params[:file]
return head :bad_request if file.blank?
url = params[:url].to_s.strip.presence
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
material = Material.new(
url: params[:url].presence,
parent_id: params[:parent_id].presence,
tag_id: params[:tag_id].presence,
created_by_user: current_user)
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
tag = tag_name.tag
tag = Tag.create!(tag_name:, category: :material) unless tag
material = Material.new(tag:, url:,
created_by_user: current_user,
updated_by_user: current_user)
material.file.attach(file)
if material.save
@@ -56,15 +67,28 @@ class MaterialsController < ApplicationController
material = Material.with_attached_file.find_by(id: params[:id])
return head :not_found unless material
material.assign_attributes(
url: params[:url].presence,
parent_id: params[:parent_id].presence,
tag_id: params[:tag_id].presence
)
material.file.attach(params[:file]) if params[:file].present?
tag_name_raw = params[:tag].to_s.strip
file = params[:file]
url = params[:url].to_s.strip.presence
return head :bad_request if tag_name_raw.blank? || (file.blank? && url.blank?)
tag_name = TagName.find_undiscard_or_create_by!(name: tag_name_raw)
tag = tag_name.tag
tag = Tag.create!(tag_name:, category: :material) unless tag
material.update!(tag:, url:, updated_by_user: current_user)
if file
material.file.attach(file)
else
material.file.purge(file)
end
if material.save
render json: material_json(material)
render json: material.as_json(methods: [:content_type]).merge(
file: if material.file.attached?
rails_storage_proxy_url(material.file, only_path: false)
end,
tag: TagRepr.base(material.tag))
else
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
end
+24 -14
View File
@@ -107,18 +107,6 @@ class TagsController < ApplicationController
}
end
def materials_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 :not_found unless tag
render json: build_tag_children(tag)
end
def autocomplete
q = params[:q].to_s.strip.sub(/\Anot:/i, '')
@@ -209,6 +197,18 @@ class TagsController < ApplicationController
render json: DeerjikistRepr.many(tag.deerjikists)
end
def materials_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 :not_found unless tag
render json: build_tag_children(tag)
end
def update
return head :unauthorized unless current_user
return head :forbidden unless current_user.gte_member?
@@ -231,7 +231,17 @@ class TagsController < ApplicationController
private
def build_tag_children tag
TagRepr.base(tag).merge(children: tag.children.map { build_tag_children(_1) })
def build_tag_children(tag)
material = tag.materials.first
file = nil
content_type = nil
if material&.file&.attached?
file = rails_storage_proxy_url(material.file, only_path: false)
content_type = material.file.blob.content_type
end
TagRepr.base(tag).merge(
children: tag.children.sort_by { _1.name }.map { build_tag_children(_1) },
material: material.as_json&.merge(file:, content_type:))
end
end