Refactor step elements controllers [SCI-6848] (#4117)

This commit is contained in:
artoscinote 2022-05-25 14:23:35 +02:00 committed by GitHub
parent c33b426c03
commit df0f001afa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 61 deletions

View file

@ -46,7 +46,7 @@ module StepComponents
if @checklist_item.destroy if @checklist_item.destroy
render json: @checklist_item, serializer: ChecklistItemSerializer render json: @checklist_item, serializer: ChecklistItemSerializer
else else
render json: @checklist, serializer: ChecklistItemSerializer, status: :unprocessable_entity render json: @checklist_item, serializer: ChecklistItemSerializer, status: :unprocessable_entity
end end
end end

View file

@ -1,23 +1,44 @@
# frozen_string_literal: true # frozen_string_literal: true
module StepComponents module StepComponents
class ChecklistsController < StepOrderableElementsController class ChecklistsController < BaseController
private before_action :load_checklist, only: %i(update destroy)
def create_step_element def create
@step.checklists.create!( checklist = @step.checklists.build(
name: t('protocols.steps.checklist.default_name', position: @step.checklists.length + 1) name: t('protocols.steps.checklist.default_name', position: @step.checklists.length + 1)
) )
create_in_step!(@step, checklist)
render_step_orderable_element(checklist)
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end end
def orderable_params def update
params.permit(:name) @checklist.update!(checklist_params)
render json: @checklist, serializer: ChecklistSerializer
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end end
def load_vars def destroy
@element = @step.checklists.find_by(id: params[:id]) if @checklist.destroy
@orderable_element = @element.step_orderable_elements.find_by(step: @step) head :ok
return render_404 unless @element && @orderable_element else
head :unprocessable_entity
end
end
private
def checklist_params
params.permit(:name, :text)
end
def load_checklist
@checklist = @step.checklists.find_by(id: params[:id])
return render_404 unless @checklist
end end
end end
end end

View file

@ -1,25 +1,46 @@
# frozen_string_literal: true
module StepComponents module StepComponents
class TablesController < StepOrderableElementsController class TablesController < BaseController
private before_action :load_table, only: %i(update destroy)
def create_step_element def create
@step.step_tables.create!(table: step_table = @step.step_tables.new(table:
Table.create!( Table.new(
name: t('protocols.steps.table.default_name', position: @step.step_tables.length + 1), name: t('protocols.steps.table.default_name', position: @step.step_tables.length + 1),
contents: { data: Array.new(5, Array.new(5, '')) }.to_json, contents: { data: Array.new(5, Array.new(5, '')) }.to_json,
created_by: current_user created_by: current_user
)) ))
create_in_step!(@step, step_table)
render_step_orderable_element(step_table)
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end end
def orderable_params def update
@table.update!(table_params)
render json: @table, serializer: TableSerializer
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end
def destroy
if @table.destroy
head :ok
else
head :unprocessable_entity
end
end
def table_params
params.permit(:name, :contents) params.permit(:name, :contents)
end end
def load_vars def load_table
@element = @step.tables.find_by(id: params[:id]) @table = @step.tables.find_by(id: params[:id])
@orderable_element = @element.step_table.step_orderable_elements.find_by(step: @step) return render_404 unless @table
return render_404 unless @element && @orderable_element
end end
end end
end end

View file

@ -1,21 +1,42 @@
# frozen_string_literal: true # frozen_string_literal: true
module StepComponents module StepComponents
class TextsController < StepOrderableElementsController class TextsController < BaseController
private before_action :load_step_text, only: %i(update destroy)
def create_step_element def create
@step.step_texts.create! step_text = @step.step_texts.build
create_in_step!(@step, step_text)
render_step_orderable_element(step_text)
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end end
def orderable_params def update
@step_text.update!(step_text_params)
TinyMceAsset.update_images(@step_text, params[:tiny_mce_images], current_user)
render json: @step_text, serializer: StepTextSerializer
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end
def destroy
if @step_text.destroy
head :ok
else
head :unprocessable_entity
end
end
private
def step_text_params
params.require(:step_text).permit(:text) params.require(:step_text).permit(:text)
end end
def load_vars def load_step_text
@element = @step.step_texts.find_by(id: params[:id]) @step_text = @step.step_texts.find_by(id: params[:id])
@orderable_element = @element.step_orderable_elements.find_by(step: @step) return render_404 unless @step_text
return render_404 unless @element && @orderable_element
end end
end end
end end

View file

@ -2,35 +2,9 @@
class StepOrderableElementsController < ApplicationController class StepOrderableElementsController < ApplicationController
before_action :load_vars_nested before_action :load_vars_nested
before_action :load_vars, only: %i(destroy update)
before_action :check_manage_permissions, only: %i(create destroy)
def create def reorder
ActiveRecord::Base.transaction do # element reordering logic goes here
element = @step.step_orderable_elements.create!(
position: @step.step_orderable_elements.length,
orderable: create_step_element
)
render json: element, serializer: StepOrderableElementSerializer, user: current_user
rescue ActiveRecord::RecordInvalid
render json: {}, status: :unprocessable_entity
end
end
def update
@element.update!(orderable_params)
TinyMceAsset.update_images(@element, params[:tiny_mce_images], current_user) if @element.is_a? StepText
render json: @element, serializer: "#{@element.class}Serializer".constantize, user: current_user
rescue ActiveRecord::RecordInvalid
render json: {}, status: :unprocessable_entity
end
def destroy
if @element.destroy
render json: @orderable_element, serializer: StepOrderableElementSerializer, user: current_user
else
render json: {}, status: :unprocessable_entity
end
end end
private private

View file

@ -19,7 +19,7 @@ export default {
success: (result) => { success: (result) => {
this.$emit( this.$emit(
'component:delete', 'component:delete',
result.data this.element.attributes.position
); );
} }
}); });

View file

@ -160,8 +160,7 @@
HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger'); HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger');
}) })
}, },
deleteComponent(element) { deleteComponent(position) {
let position = element.attributes.position;
this.elements.splice(position, 1) this.elements.splice(position, 1)
let unordered_elements = this.elements.map( e => { let unordered_elements = this.elements.map( e => {
if (e.attributes.position >= position) { if (e.attributes.position >= position) {