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
|
|
|
|
|
2022-08-22 20:02:19 +08:00
|
|
|
before_action :load_step_text, only: %i(update destroy duplicate)
|
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
|
|
|
|
|
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
|
2022-04-29 18:29:42 +08:00
|
|
|
params.require(:step_text).permit(:text)
|
|
|
|
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
|