This commit is contained in:
2026-04-06 08:41:43 +09:00
parent 64e7400ed0
commit c0d52077b9
7 changed files with 548 additions and 30 deletions
@@ -18,7 +18,7 @@ class MaterialsController < ApplicationController
count = q.count
materials = q.order(created_at: :desc, id: :desc).limit(limit).offset(offset)
render json: { materials: materials.map { |m| material_json(m) }, count: count }
render json: { materials: MaterialRepr.many(materials), count: count }
end
def show
@@ -29,11 +29,7 @@ class MaterialsController < ApplicationController
.find_by(id: params[:id])
return head :not_found unless 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))
render json: MaterialRepr.base(material)
end
def create
@@ -54,7 +50,7 @@ class MaterialsController < ApplicationController
material.file.attach(file)
if material.save
render json: material_json(material), status: :created
render json: MaterialRepr.base(material), status: :created
else
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
end
@@ -80,15 +76,11 @@ class MaterialsController < ApplicationController
if file
material.file.attach(file)
else
material.file.purge(file)
material.file.purge
end
if material.save
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))
render json: MaterialRepr.base(material)
else
render json: { errors: material.errors.full_messages }, status: :unprocessable_entity
end
@@ -104,15 +96,4 @@ class MaterialsController < ApplicationController
material.discard
head :no_content
end
private
def material_json(material)
MaterialRepr.base(material).merge(
'filename' => material.file.attached? ? material.file.filename.to_s : nil,
'byte_size' => material.file.attached? ? material.file.byte_size : nil,
'content_type' => material.file.attached? ? material.file.content_type : nil,
'url' => material.file.attached? ? url_for(material.file) : nil
)
end
end
+1 -1
View File
@@ -204,7 +204,7 @@ class TagsController < ApplicationController
tag = Tag.joins(:tag_name)
.includes(:tag_name, tag_name: :wiki_page)
.find_by(tag_names: { name: })
return :not_found unless tag
return head :not_found unless tag
render json: build_tag_children(tag)
end
+9 -3
View File
@@ -2,13 +2,19 @@
module MaterialRepr
BASE = { only: [:id, :url, :parent_id, :created_at, :updated_at],
include: { created_by_user: UserRepr::BASE, tag: TagRepr::BASE } }.freeze
BASE = { methods: [:content_type],
include: { created_by_user: UserRepr::BASE,
updated_by_user: UserRepr::BASE } }.freeze
module_function
def base(material)
material.as_json(BASE)
material.as_json(BASE).merge(
file: if material.file.attached?
Rails.application.routes.url_helpers.rails_storage_proxy_url(
material.file, only_path: false)
end,
tag: TagRepr.base(material.tag))
end
def many(materials)