diff --git a/app/controllers/api/v1/steps_controller.rb b/app/controllers/api/v1/steps_controller.rb index 6cff716d0..cb5e07073 100644 --- a/app/controllers/api/v1/steps_controller.rb +++ b/app/controllers/api/v1/steps_controller.rb @@ -31,19 +31,29 @@ module Api def create raise PermissionError.new(Protocol, :create) unless can_manage_protocol_in_module?(@protocol) - step = @protocol.steps.create!(step_params.merge!(completed: false, - user: current_user, - position: @protocol.number_of_steps, - last_modified_by_id: current_user.id)) + step = @protocol.steps.create!( + step_params.except(:description) + .merge!(completed: false, + user: current_user, + position: @protocol.number_of_steps, + last_modified_by_id: current_user.id) + ) + step.step_texts.create!(text: step_params[:description]) if step_params[:description] render jsonapi: step, serializer: StepSerializer, status: :created end def update @step.assign_attributes( - step_params.merge!(last_modified_by_id: current_user.id) + step_params.except(:description).merge!(last_modified_by_id: current_user.id) ) + if step_params[:description] + (@step.description_step_text || @step.step_texts.create!).update!( + text: step_params[:description] + ) + end + if @step.changed? && @step.save! if @step.saved_change_to_attribute?(:completed) completed_steps = @protocol.steps.where(completed: true).count diff --git a/app/models/step.rb b/app/models/step.rb index ede51e199..bd3290ec6 100644 --- a/app/models/step.rb +++ b/app/models/step.rb @@ -137,6 +137,10 @@ class Step < ApplicationRecord step_comments end + def description_step_text + step_texts.order(created_at: :asc).first + end + private def move_in_protocol(direction) diff --git a/app/serializers/api/v1/step_serializer.rb b/app/serializers/api/v1/step_serializer.rb index 7e109f29d..ed8837975 100644 --- a/app/serializers/api/v1/step_serializer.rb +++ b/app/serializers/api/v1/step_serializer.rb @@ -15,18 +15,21 @@ module Api has_many :assets, serializer: AssetSerializer has_many :checklists, serializer: ChecklistSerializer has_many :tables, serializer: TableSerializer + has_many :step_texts, serializer: StepTextSerializer has_many :step_comments, key: :comments, serializer: CommentSerializer include TimestampableModel def description + return unless object.description_step_text + if instance_options[:rte_rendering] - custom_auto_link(object.tinymce_render(:description), + custom_auto_link(object.description_step_text.tinymce_render(:description), simple_format: false, tags: %w(img), team: instance_options[:team]) else - object.description + object.description_step_text.text end end end