Add task state change error handling and validation

This commit is contained in:
aignatov-bio 2020-09-01 11:07:02 +02:00
parent bd05214514
commit 9ff5e8bd7d
4 changed files with 26 additions and 13 deletions
app
assets/javascripts
controllers
models
config/locales

View file

@ -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');
}

View file

@ -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

View file

@ -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?

View file

@ -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: