From d6b55cf0e5ef786827fd2bc078741c465e8535bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Friebel?= Date: Wed, 30 Sep 2020 17:22:16 +0200 Subject: [PATCH] enable custom workflows for issue relations --- app/models/custom_workflow.rb | 4 +- config/locales/en.yml | 4 + lib/redmine_custom_workflows.rb | 3 +- .../patches/issue_relation_patch.rb | 78 +++++++++++++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 lib/redmine_custom_workflows/patches/issue_relation_patch.rb diff --git a/app/models/custom_workflow.rb b/app/models/custom_workflow.rb index f4f2a89..1ee2318 100644 --- a/app/models/custom_workflow.rb +++ b/app/models/custom_workflow.rb @@ -21,11 +21,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class CustomWorkflow < ActiveRecord::Base - OBSERVABLES = [:issue, :issue_attachments, :user, :attachment, :group, :group_users, :project, :project_attachments, + OBSERVABLES = [:issue, :issue_relation, :issue_attachments, :user, :attachment, :group, :group_users, :project, :project_attachments, :wiki_content, :wiki_page_attachments, :time_entry, :version, :shared] PROJECT_OBSERVABLES = [:issue, :issue_attachments, :project, :project_attachments, :wiki_content, :wiki_page_attachments, :time_entry, :version] COLLECTION_OBSERVABLES = [:group_users, :issue_attachments, :project_attachments, :wiki_page_attachments] - SINGLE_OBSERVABLES = [:issue, :user, :group, :attachment, :project, :wiki_content, :time_entry, :version] + SINGLE_OBSERVABLES = [:issue, :issue_relation, :user, :group, :attachment, :project, :wiki_content, :time_entry, :version] has_and_belongs_to_many :projects acts_as_list diff --git a/config/locales/en.yml b/config/locales/en.yml index 1541931..4fd7651 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -73,6 +73,9 @@ en: text_custom_workflow_issue_code_note: Scripts are executed in the context of Issue object like ordinary before_save and after_save callbacks. So use methods and properties of the issue directly (or through `self`). Instance variables (@variable) are also allowed and may be used if needed. + text_custom_workflow_issue_relation_code_note: Scripts are executed in the context of IssueRelation object like ordinary + before_save and after_save callbacks. So use methods and properties of the issue relation directly (or through `self`). + Instance variables (@variable) are also allowed and may be used if needed. text_custom_workflow_shared_code_note: This code will run before any other workflow and may contain shared code, e.g. functions and classes needed by other workflows text_custom_workflow_user_code_note: Scripts are executed in the context of User object when user object changes @@ -107,6 +110,7 @@ en: custom_workflow_observable_shared: custom_workflow_observable_issue: Issue + custom_workflow_observable_issue_relation: Issue Relation custom_workflow_observable_issue_attachments: Issue Attachments custom_workflow_observable_group: Group custom_workflow_observable_user: User diff --git a/lib/redmine_custom_workflows.rb b/lib/redmine_custom_workflows.rb index c69786e..a3e2b86 100644 --- a/lib/redmine_custom_workflows.rb +++ b/lib/redmine_custom_workflows.rb @@ -30,10 +30,11 @@ require 'redmine_custom_workflows/errors/workflow_error' require 'redmine_custom_workflows/patches/attachment_patch' require 'redmine_custom_workflows/patches/group_patch' require 'redmine_custom_workflows/patches/issue_patch' +require 'redmine_custom_workflows/patches/issue_relation_patch' require 'redmine_custom_workflows/patches/project_patch' require 'redmine_custom_workflows/patches/projects_helper_patch' require 'redmine_custom_workflows/patches/time_entry_patch' require 'redmine_custom_workflows/patches/user_patch' require 'redmine_custom_workflows/patches/version_patch' require 'redmine_custom_workflows/patches/wiki_content_patch' -require 'redmine_custom_workflows/patches/wiki_page_patch' \ No newline at end of file +require 'redmine_custom_workflows/patches/wiki_page_patch' diff --git a/lib/redmine_custom_workflows/patches/issue_relation_patch.rb b/lib/redmine_custom_workflows/patches/issue_relation_patch.rb new file mode 100644 index 0000000..3842f79 --- /dev/null +++ b/lib/redmine_custom_workflows/patches/issue_relation_patch.rb @@ -0,0 +1,78 @@ +# encoding: utf-8 +# frozen_string_literal: true +# +# Redmine plugin for Custom Workflows +# +# Copyright © 2015-19 Anton Argirov +# Copyright © 2019-20 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module RedmineCustomWorkflows + module Patches + module IssueRelationPatch + + def self.included(base) + base.class_eval do + before_save :before_save_custom_workflows + after_save :after_save_custom_workflows + before_destroy :before_destroy_custom_workflows + after_destroy :after_destroy_custom_workflows + + # override IssueRelation.to_s to rescue NoMethodError during CustomWorkflow.validate_syntax due to + # logging of temporarily instatiated IssueRelation with no related issues set + alias_method :old_to_s, :to_s + + def to_s(issue=nil) + block_given? ? old_to_s(issue, &Proc.new) : old_to_s(issue) + rescue NoMethodError => e + if issue_from.present? || issue_to.present? + raise e + end + self.class.to_s + end + end + end + + def before_save_custom_workflows + @issue_relation = self + @saved_attributes = attributes.dup + CustomWorkflow.run_shared_code self + CustomWorkflow.run_custom_workflows :issue_relation, self, :before_save + throw :abort if errors.any? + errors.empty? && (@saved_attributes == attributes || valid?) + ensure + @saved_attributes = nil + end + + def after_save_custom_workflows + CustomWorkflow.run_custom_workflows :issue_relation, self, :after_save + end + + def before_destroy_custom_workflows + CustomWorkflow.run_custom_workflows :issue_relation, self, :before_destroy + end + + def after_destroy_custom_workflows + CustomWorkflow.run_custom_workflows :issue_relation, self, :after_destroy + end + + end + end +end + +# Apply patch +RedmineExtensions::PatchManager.register_model_patch 'IssueRelation', + 'RedmineCustomWorkflows::Patches::IssueRelationPatch'