mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-05 20:23:16 +08:00
Add task state change error handling and validation
This commit is contained in:
parent
bd05214514
commit
9ff5e8bd7d
4 changed files with 26 additions and 13 deletions
app
config/locales
|
@ -246,7 +246,7 @@ function applyTaskStatusChangeCallBack() {
|
|||
if (e.status === 403) {
|
||||
HelperModule.flashAlertMsg(I18n.t('my_module_statuses.update_status.error.no_permission'), 'danger');
|
||||
} else if (e.status === 422) {
|
||||
HelperModule.flashAlertMsg(e.errors, 'danger');
|
||||
HelperModule.flashAlertMsg(e.responseJSON.errors, 'danger');
|
||||
} else {
|
||||
HelperModule.flashAlertMsg('error', 'danger');
|
||||
}
|
||||
|
|
|
@ -263,18 +263,17 @@ class MyModulesController < ApplicationController
|
|||
end
|
||||
|
||||
def update_state
|
||||
new_status = @my_module.my_module_status_flow.my_module_statuses.find_by(id: update_status_params[:status_id])
|
||||
return render_404 unless new_status
|
||||
|
||||
old_status_id = @my_module.my_module_status_id
|
||||
@my_module.update(my_module_status: new_status)
|
||||
|
||||
log_activity(:change_status_on_task_flow, @my_module, my_module_status_old: old_status_id,
|
||||
my_module_status_new: @my_module.my_module_status.id)
|
||||
|
||||
render json: { content: render_to_string(
|
||||
partial: 'my_modules/status_flow/task_flow_button.html.erb', locals: { my_module: @my_module }
|
||||
) }, status: :ok
|
||||
if @my_module.update(my_module_status_id: update_status_params[:status_id])
|
||||
log_activity(:change_status_on_task_flow, @my_module, my_module_status_old: old_status_id,
|
||||
my_module_status_new: @my_module.my_module_status.id)
|
||||
render json: { content: render_to_string(
|
||||
partial: 'my_modules/status_flow/task_flow_button.html.erb',
|
||||
locals: { my_module: @my_module }
|
||||
) }, status: :ok
|
||||
else
|
||||
render json: { errors: @my_module.errors.messages.values.flatten.join('\n') }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -22,6 +22,7 @@ class MyModule < ApplicationRecord
|
|||
validate :coordinates_uniqueness_check, if: :active?
|
||||
validates :completed_on, presence: true, if: proc { |mm| mm.completed? }
|
||||
|
||||
validate :check_status_order, if: :my_module_status_id_changed?
|
||||
validate :check_status_conditions, if: :my_module_status_id_changed?
|
||||
validate :check_status_implications, unless: :my_module_status_id_changed?
|
||||
|
||||
|
@ -538,6 +539,17 @@ class MyModule < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def check_status_order
|
||||
return if my_module_status.blank?
|
||||
|
||||
original_status = MyModuleStatus.find(my_module_status_id_was)
|
||||
|
||||
unless original_status.next_status == my_module_status || original_status.previous_status == my_module_status
|
||||
errors.add(:my_module_status_id,
|
||||
I18n.t('activerecord.errors.models.my_module.attributes.my_module_status_id.not_correct_order'))
|
||||
end
|
||||
end
|
||||
|
||||
def exec_status_consequences
|
||||
return if my_module_status.blank?
|
||||
|
||||
|
|
|
@ -108,8 +108,10 @@ en:
|
|||
same_team: "Inventory can't be shared to the same team as it belongs to"
|
||||
my_module:
|
||||
attributes:
|
||||
my_module_status_id:
|
||||
not_correct_order: "Status can be changed only on next or previous status."
|
||||
position:
|
||||
not_unique: "X and Y position has already been taken by another task in the experiment."
|
||||
not_unique: "X and Y position has already been taken by another task in the experiment."
|
||||
my_module_status:
|
||||
attributes:
|
||||
next_status:
|
||||
|
|
Loading…
Reference in a new issue