Files
astuto/app/controllers/post_statuses_controller.rb

81 lines
1.8 KiB
Ruby
Raw Normal View History

2019-09-03 12:58:44 +02:00
class PostStatusesController < ApplicationController
2022-05-01 18:00:38 +02:00
include ApplicationHelper
2022-06-10 12:03:33 +02:00
before_action :authenticate_user!, only: [:create, :update, :update_order, :destroy]
2022-05-01 18:00:38 +02:00
2019-09-03 12:58:44 +02:00
def index
post_statuses = PostStatus.order(order: :asc)
render json: post_statuses
end
2022-05-01 18:00:38 +02:00
def create
post_status = PostStatus.new(post_status_params)
2022-06-10 12:03:33 +02:00
authorize post_status
2022-05-01 18:00:38 +02:00
if post_status.save
render json: post_status, status: :created
else
render json: {
error: post_status.errors.full_messages
2022-05-01 18:00:38 +02:00
}, status: :unprocessable_entity
end
end
def update
post_status = PostStatus.find(params[:id])
2022-06-10 12:03:33 +02:00
authorize post_status
2022-05-01 18:00:38 +02:00
post_status.assign_attributes(post_status_params)
if post_status.save
render json: post_status, status: :ok
else
render json: {
error: post_status.errors.full_messages
2022-05-01 18:00:38 +02:00
}, status: :unprocessable_entity
end
end
def destroy
post_status = PostStatus.find(params[:id])
2022-06-10 12:03:33 +02:00
authorize post_status
2022-05-01 18:00:38 +02:00
if post_status.destroy
render json: {
id: params[:id]
}, status: :accepted
else
render json: {
error: post_status.errors.full_messages
2022-05-01 18:00:38 +02:00
}, status: :unprocessable_entity
end
end
def update_order
2022-06-10 12:03:33 +02:00
authorize PostStatus
2022-05-01 18:00:38 +02:00
workflow_output = ReorderWorkflow.new(
entity_classname: PostStatus,
column_name: 'order',
entity_id: params[:post_status][:id],
src_index: params[:post_status][:src_index],
dst_index: params[:post_status][:dst_index]
).run
if workflow_output
render json: workflow_output
else
render json: {
error: t("backend.errors.post_status.update_order")
2022-05-01 18:00:38 +02:00
}, status: :unprocessable_entity
end
end
private
def post_status_params
params
.require(:post_status)
.permit(:name, :color, :show_in_roadmap)
2022-05-01 18:00:38 +02:00
end
end