This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user