2022-04-29 18:29:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-05-30 19:45:51 +08:00
|
|
|
module StepElements
|
2022-05-25 20:23:35 +08:00
|
|
|
class TextsController < BaseController
|
2022-12-13 21:47:53 +08:00
|
|
|
include ApplicationHelper
|
|
|
|
include StepsActions
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
before_action :load_step_text, only: %i(update destroy duplicate move)
|
2022-05-25 20:23:35 +08:00
|
|
|
|
|
|
|
def create
|
|
|
|
step_text = @step.step_texts.build
|
2022-06-02 17:15:32 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
create_in_step!(@step, step_text)
|
|
|
|
log_step_activity(:text_added, { text_name: step_text.name })
|
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
render_step_orderable_element(step_text)
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def update
|
2022-12-13 21:47:53 +08:00
|
|
|
old_text = @step_text.text
|
2022-06-02 17:15:32 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@step_text.update!(step_text_params)
|
|
|
|
TinyMceAsset.update_images(@step_text, params[:tiny_mce_images], current_user)
|
|
|
|
log_step_activity(:text_edited, { text_name: @step_text.name })
|
2022-12-13 21:47:53 +08:00
|
|
|
step_text_annotation(@step, @step_text, old_text)
|
2022-06-02 17:15:32 +08:00
|
|
|
end
|
|
|
|
|
2022-06-07 18:10:10 +08:00
|
|
|
render json: @step_text, serializer: StepTextSerializer, user: current_user
|
2022-05-25 20:23:35 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2023-04-12 20:57:46 +08:00
|
|
|
render json: @step_text.errors, status: :unprocessable_entity
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
def move
|
|
|
|
target = @protocol.steps.find_by(id: params[:target_id])
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@step_text.update!(step: target)
|
|
|
|
@step_text.step_orderable_element.update!(step: target, position: target.step_orderable_elements.size)
|
|
|
|
@step.normalize_elements_position
|
2023-09-19 07:17:30 +08:00
|
|
|
|
|
|
|
log_step_activity(
|
|
|
|
:text_moved,
|
|
|
|
{
|
|
|
|
user: current_user.id,
|
|
|
|
text_name: @step_text.name,
|
|
|
|
step_position_original: @step.position + 1,
|
|
|
|
step_original: @step.id,
|
|
|
|
step_position_destination: target.position + 1,
|
|
|
|
step_destination: target.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-08-21 15:22:56 +08:00
|
|
|
render json: @step_text, serializer: StepTextSerializer, user: current_user
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: @step_text.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def destroy
|
|
|
|
if @step_text.destroy
|
2022-06-02 17:15:32 +08:00
|
|
|
log_step_activity(:text_deleted, { text_name: @step_text.name })
|
2022-05-25 20:23:35 +08:00
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-22 20:02:19 +08:00
|
|
|
def duplicate
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
position = @step_text.step_orderable_element.position
|
|
|
|
@step.step_orderable_elements.where('position > ?', position).order(position: :desc).each do |element|
|
|
|
|
element.update(position: element.position + 1)
|
|
|
|
end
|
|
|
|
new_step_text = @step_text.duplicate(@step, position + 1)
|
2022-08-30 21:26:52 +08:00
|
|
|
log_step_activity(:text_duplicated, { text_name: new_step_text.name })
|
2022-08-22 20:02:19 +08:00
|
|
|
render_step_orderable_element(new_step_text)
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def step_text_params
|
2023-09-04 19:58:11 +08:00
|
|
|
params.require(:text_component).permit(:text, :name)
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
2022-05-03 21:15:49 +08:00
|
|
|
|
2022-05-25 20:23:35 +08:00
|
|
|
def load_step_text
|
|
|
|
@step_text = @step.step_texts.find_by(id: params[:id])
|
|
|
|
return render_404 unless @step_text
|
2022-05-03 21:15:49 +08:00
|
|
|
end
|
2022-04-29 18:29:42 +08:00
|
|
|
end
|
|
|
|
end
|