From 0620da99de55497397eae5c93b2fb62e343aa6d7 Mon Sep 17 00:00:00 2001 From: aignatov-bio Date: Mon, 30 Nov 2020 12:28:29 +0100 Subject: [PATCH] [SCI-5260] Add direction condition for status consequences --- app/jobs/my_module_status_consequences_job.rb | 4 ++-- app/models/my_module.rb | 14 ++++++++++++-- app/models/my_module_status_consequence.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/jobs/my_module_status_consequences_job.rb b/app/jobs/my_module_status_consequences_job.rb index aa0a4d37c..5ed582c3a 100644 --- a/app/jobs/my_module_status_consequences_job.rb +++ b/app/jobs/my_module_status_consequences_job.rb @@ -3,11 +3,11 @@ class MyModuleStatusConsequencesJob < ApplicationJob queue_as :high_priority - def perform(my_module, my_module_status_consequences) + def perform(my_module, my_module_status_consequences, status_changing_direction) error_raised = false my_module.transaction do my_module_status_consequences.each do |consequence| - consequence.call(my_module) + consequence.call(my_module) if consequence.public_send("#{status_changing_direction}_execution") end my_module.update!(status_changing: false) rescue StandardError => e diff --git a/app/models/my_module.rb b/app/models/my_module.rb index c4f960e8e..22c395625 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -506,13 +506,23 @@ class MyModule < ApplicationRecord self.changing_from_my_module_status_id = my_module_status_id_was if my_module_status_id_was.present? self.status_changing = true + status_changing_direction = if my_module_status.next_status&.id == my_module_status_id_was + :backward + else + :forward + end + yield if my_module_status.my_module_status_consequences.any?(&:runs_in_background?) - MyModuleStatusConsequencesJob.perform_later(self, my_module_status.my_module_status_consequences.to_a) + MyModuleStatusConsequencesJob.perform_later( + self, + my_module_status.my_module_status_consequences.to_a, + status_changing_direction + ) else my_module_status.my_module_status_consequences.each do |consequence| - consequence.call(self) + consequence.call(self) if consequence.public_send("#{status_changing_direction}_execution") end update!(status_changing: false) end diff --git a/app/models/my_module_status_consequence.rb b/app/models/my_module_status_consequence.rb index 5ed34193e..6e8fbf5e1 100644 --- a/app/models/my_module_status_consequence.rb +++ b/app/models/my_module_status_consequence.rb @@ -6,4 +6,12 @@ class MyModuleStatusConsequence < ApplicationRecord def runs_in_background? false end + + def forward_execution + true + end + + def backward_execution + true + end end