#18: Ability to exit current workflow with return or return true and cancel workflows' execution chain with return false

This commit is contained in:
Anton Argirov 2015-07-07 11:33:04 +06:00
parent 0590df7928
commit 8a72491f60
4 changed files with 25 additions and 12 deletions

View File

@ -45,13 +45,20 @@ class CustomWorkflow < ActiveRecord::Base
CustomWorkflow.new(attributes)
end
def log_message(str, object)
Rails.logger.info str + " for #{object.class} \"#{object}\" (\##{object.id})"
end
def run_shared_code(object)
workflows = CustomWorkflow.observing(:shared).active
Rails.logger.info "= Running shared code for #{object.class} \"#{object}\" (\##{object.id})"
log_message '= Running shared code', object
workflows.each do |workflow|
return false unless workflow.run(object, :shared_code)
if workflow.run(object, :shared_code) == false
log_message '= Abort running shared code', object
return false
end
Rails.logger.info "= Finished running shared code for #{object.class} \"#{object}\" (\##{object.id})"
end
log_message '= Finished running shared code', object
true
end
@ -62,11 +69,14 @@ class CustomWorkflow < ActiveRecord::Base
workflows = workflows.for_project(object.project)
end
return true unless workflows.any?
Rails.logger.info "= Running #{event} custom workflows for #{object.class} \"#{object}\" (\##{object.id})"
log_message "= Running #{event} custom workflows", object
workflows.each do |workflow|
return false unless workflow.run(object, event)
if workflow.run(object, event) == false
log_message "= Abort running #{event} custom workflows", object
return false
end
Rails.logger.info "= Finished running #{event} custom workflows for #{object.class} \"#{object}\" (\##{object.id})"
end
log_message "= Finished running #{event} custom workflows", object
true
end
end

View File

@ -33,9 +33,10 @@ module RedmineCustomWorkflows
@group = self
@saved_attributes = attributes.dup
CustomWorkflow.run_shared_code(self)
result = CustomWorkflow.run_custom_workflows(:group, self, :before_save) && (@saved_attributes == attributes || valid?)
CustomWorkflow.run_custom_workflows(:group, self, :before_save)
errors.empty? && (@saved_attributes == attributes || valid?)
ensure
@saved_attributes = nil
result
end
def after_save_custom_workflows

View File

@ -27,9 +27,10 @@ module RedmineCustomWorkflows
@issue = self
@saved_attributes = attributes.dup
CustomWorkflow.run_shared_code(self)
result = CustomWorkflow.run_custom_workflows(:issue, self, :before_save) && (@saved_attributes == attributes || valid?)
CustomWorkflow.run_custom_workflows(:issue, self, :before_save)
errors.empty? && (@saved_attributes == attributes || valid?)
ensure
@saved_attributes = nil
result
end
def after_save_custom_workflows

View File

@ -14,9 +14,10 @@ module RedmineCustomWorkflows
@user = self
@saved_attributes = attributes.dup
CustomWorkflow.run_shared_code(self)
result = CustomWorkflow.run_custom_workflows(:user, self, :before_save) && (@saved_attributes == attributes || valid?)
CustomWorkflow.run_custom_workflows(:user, self, :before_save)
errors.empty? && (@saved_attributes == attributes || valid?)
ensure
@saved_attributes = nil
result
end
def after_save_custom_workflows