diff --git a/app/assets/javascripts/my_modules.js b/app/assets/javascripts/my_modules.js index 562625293..e1661534f 100644 --- a/app/assets/javascripts/my_modules.js +++ b/app/assets/javascripts/my_modules.js @@ -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'); } diff --git a/app/controllers/my_modules_controller.rb b/app/controllers/my_modules_controller.rb index 47340ec25..838debbd4 100644 --- a/app/controllers/my_modules_controller.rb +++ b/app/controllers/my_modules_controller.rb @@ -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 diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 57361e5eb..eb5370bb4 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -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? diff --git a/config/locales/en.yml b/config/locales/en.yml index 15f75aac9..cf208033a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: