mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-29 03:35:25 +08:00
Refactor step elements controllers [SCI-6848] (#4117)
This commit is contained in:
parent
c33b426c03
commit
df0f001afa
7 changed files with 97 additions and 61 deletions
|
@ -46,7 +46,7 @@ module StepComponents
|
|||
if @checklist_item.destroy
|
||||
render json: @checklist_item, serializer: ChecklistItemSerializer
|
||||
else
|
||||
render json: @checklist, serializer: ChecklistItemSerializer, status: :unprocessable_entity
|
||||
render json: @checklist_item, serializer: ChecklistItemSerializer, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,23 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module StepComponents
|
||||
class ChecklistsController < StepOrderableElementsController
|
||||
private
|
||||
class ChecklistsController < BaseController
|
||||
before_action :load_checklist, only: %i(update destroy)
|
||||
|
||||
def create_step_element
|
||||
@step.checklists.create!(
|
||||
def create
|
||||
checklist = @step.checklists.build(
|
||||
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
|
||||
|
||||
def orderable_params
|
||||
params.permit(:name)
|
||||
def update
|
||||
@checklist.update!(checklist_params)
|
||||
render json: @checklist, serializer: ChecklistSerializer
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
head :unprocessable_entity
|
||||
end
|
||||
|
||||
def load_vars
|
||||
@element = @step.checklists.find_by(id: params[:id])
|
||||
@orderable_element = @element.step_orderable_elements.find_by(step: @step)
|
||||
return render_404 unless @element && @orderable_element
|
||||
def destroy
|
||||
if @checklist.destroy
|
||||
head :ok
|
||||
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
|
||||
|
|
|
@ -1,25 +1,46 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module StepComponents
|
||||
class TablesController < StepOrderableElementsController
|
||||
private
|
||||
class TablesController < BaseController
|
||||
before_action :load_table, only: %i(update destroy)
|
||||
|
||||
def create_step_element
|
||||
@step.step_tables.create!(table:
|
||||
Table.create!(
|
||||
def create
|
||||
step_table = @step.step_tables.new(table:
|
||||
Table.new(
|
||||
name: t('protocols.steps.table.default_name', position: @step.step_tables.length + 1),
|
||||
contents: { data: Array.new(5, Array.new(5, '')) }.to_json,
|
||||
created_by: current_user
|
||||
))
|
||||
|
||||
create_in_step!(@step, step_table)
|
||||
|
||||
render_step_orderable_element(step_table)
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
head :unprocessable_entity
|
||||
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)
|
||||
end
|
||||
|
||||
def load_vars
|
||||
@element = @step.tables.find_by(id: params[:id])
|
||||
@orderable_element = @element.step_table.step_orderable_elements.find_by(step: @step)
|
||||
return render_404 unless @element && @orderable_element
|
||||
def load_table
|
||||
@table = @step.tables.find_by(id: params[:id])
|
||||
return render_404 unless @table
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,21 +1,42 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module StepComponents
|
||||
class TextsController < StepOrderableElementsController
|
||||
private
|
||||
class TextsController < BaseController
|
||||
before_action :load_step_text, only: %i(update destroy)
|
||||
|
||||
def create_step_element
|
||||
@step.step_texts.create!
|
||||
def 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
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
def load_vars
|
||||
@element = @step.step_texts.find_by(id: params[:id])
|
||||
@orderable_element = @element.step_orderable_elements.find_by(step: @step)
|
||||
return render_404 unless @element && @orderable_element
|
||||
def load_step_text
|
||||
@step_text = @step.step_texts.find_by(id: params[:id])
|
||||
return render_404 unless @step_text
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,35 +2,9 @@
|
|||
|
||||
class StepOrderableElementsController < ApplicationController
|
||||
before_action :load_vars_nested
|
||||
before_action :load_vars, only: %i(destroy update)
|
||||
before_action :check_manage_permissions, only: %i(create destroy)
|
||||
|
||||
def create
|
||||
ActiveRecord::Base.transaction do
|
||||
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
|
||||
def reorder
|
||||
# element reordering logic goes here
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -19,7 +19,7 @@ export default {
|
|||
success: (result) => {
|
||||
this.$emit(
|
||||
'component:delete',
|
||||
result.data
|
||||
this.element.attributes.position
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -160,8 +160,7 @@
|
|||
HelperModule.flashAlertMsg(this.i18n.t('errors.general'), 'danger');
|
||||
})
|
||||
},
|
||||
deleteComponent(element) {
|
||||
let position = element.attributes.position;
|
||||
deleteComponent(position) {
|
||||
this.elements.splice(position, 1)
|
||||
let unordered_elements = this.elements.map( e => {
|
||||
if (e.attributes.position >= position) {
|
||||
|
|
Loading…
Reference in a new issue