class WorkflowStepsController < ApplicationController layout 'admin' before_action :require_admin before_action :find_workflow, only: [:create, :update, :destroy, :move] before_action :find_step, only: [:update, :destroy, :move] def create @step = @workflow.workflow_steps.build(step_params) if @step.save flash[:notice] = '단계가 추가되었습니다.' else flash[:error] = @step.errors.full_messages.join(', ') end redirect_to custom_workflow_path(@workflow) end def update if @step.update(step_params) flash[:notice] = '단계가 수정되었습니다.' else flash[:error] = @step.errors.full_messages.join(', ') end redirect_to custom_workflow_path(@workflow) end def destroy @step.destroy flash[:notice] = '단계가 삭제되었습니다.' redirect_to custom_workflow_path(@workflow) end def move direction = params[:direction] if direction == 'up' && @step.position > 0 swap_step = @workflow.workflow_steps.find_by(position: @step.position - 1) if swap_step swap_step.update(position: @step.position) @step.update(position: @step.position - 1) end elsif direction == 'down' swap_step = @workflow.workflow_steps.find_by(position: @step.position + 1) if swap_step swap_step.update(position: @step.position) @step.update(position: @step.position + 1) end end redirect_to custom_workflow_path(@workflow) end # 담당자 추가 def add_assignee @step = WorkflowStep.find(params[:step_id]) assignee = @step.workflow_step_assignees.build( assignee_type: params[:assignee_type], assignee_id: params[:assignee_id] ) if assignee.save flash[:notice] = '담당자가 추가되었습니다.' else flash[:error] = assignee.errors.full_messages.join(', ') end redirect_to custom_workflow_path(@step.custom_workflow) end # 담당자 제거 def remove_assignee @step = WorkflowStep.find(params[:step_id]) assignee = @step.workflow_step_assignees.find(params[:id]) assignee.destroy flash[:notice] = '담당자가 제거되었습니다.' redirect_to custom_workflow_path(@step.custom_workflow) end private def find_workflow @workflow = CustomWorkflow.find(params[:workflow_id]) rescue ActiveRecord::RecordNotFound render_404 end def find_step @step = @workflow.workflow_steps.find(params[:id]) rescue ActiveRecord::RecordNotFound render_404 end def step_params params.require(:workflow_step).permit(:name, :description, :issue_status_id, :is_start, :is_end, :due_days) end end