diff --git a/app/models/custom_workflow.rb b/app/models/custom_workflow.rb index 2835e32..ffe99a8 100644 --- a/app/models/custom_workflow.rb +++ b/app/models/custom_workflow.rb @@ -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 end - Rails.logger.info "= Finished running shared code for #{object.class} \"#{object}\" (\##{object.id})" + 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 end - Rails.logger.info "= Finished running #{event} custom workflows for #{object.class} \"#{object}\" (\##{object.id})" + log_message "= Finished running #{event} custom workflows", object true end end diff --git a/lib/redmine_custom_workflows/group_patch.rb b/lib/redmine_custom_workflows/group_patch.rb index ef80225..76f22b4 100644 --- a/lib/redmine_custom_workflows/group_patch.rb +++ b/lib/redmine_custom_workflows/group_patch.rb @@ -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 diff --git a/lib/redmine_custom_workflows/issue_patch.rb b/lib/redmine_custom_workflows/issue_patch.rb index 254ea5e..9ee8688 100644 --- a/lib/redmine_custom_workflows/issue_patch.rb +++ b/lib/redmine_custom_workflows/issue_patch.rb @@ -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 diff --git a/lib/redmine_custom_workflows/user_patch.rb b/lib/redmine_custom_workflows/user_patch.rb index 9eab10b..dc9f640 100644 --- a/lib/redmine_custom_workflows/user_patch.rb +++ b/lib/redmine_custom_workflows/user_patch.rb @@ -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