mirror of
https://github.com/rickbarrette/redmine_recursive_relations.git
synced 2026-10-03 13:50:43 -04:00
Initial plugin structure with issue relation functionality
This commit is contained in:
commit
eeed26afe0
5 files changed
+180
No files matched your search
@@ -0,0 +1,19 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module RedmineRecursiveRelations
|
||||
|
||||
def self.setup
|
||||
Rails.logger.info "[RedmineRecursiveRelations] setup"
|
||||
unless IssueRelationsController.included_modules.include?(RedmineRecursiveRelations::Patches::IssueRelationsControllerPatch)
|
||||
IssueRelationsController.include(RedmineRecursiveRelations::Patches::IssueRelationsControllerPatch)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,67 @@
|
||||
module RedmineRecursiveRelations
|
||||
module Patches
|
||||
module IssueRelationsControllerPatch
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# 1. Capture the form data before Redmine modifies it in the core 'create' method
|
||||
before_action :store_original_relation_params, only: [:create]
|
||||
|
||||
# 2. Run the cross-linking after Redmine has linked the base issue to the targets
|
||||
after_action :create_cross_relations, only: [:create]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[IssueRelationsControllerPatch] #{msg}"
|
||||
end
|
||||
|
||||
def store_original_relation_params
|
||||
log "store_original_relation_params"
|
||||
return unless params[:relation]
|
||||
|
||||
# relation_issues_to_id is a core method that parses strings like "12, 13, 15" into an array
|
||||
@original_target_ids = relation_issues_to_id.dup
|
||||
|
||||
# Save the params deeply so they aren't overwritten by the core looping mechanism
|
||||
@original_relation_params = params[:relation].to_unsafe_hash.deep_dup
|
||||
end
|
||||
|
||||
def create_cross_relations
|
||||
log "create_cross_relations"
|
||||
# Ensure we have data and at least 2 issues to cross-relate
|
||||
return if @original_target_ids.blank? || @original_relation_params.blank?
|
||||
return if @original_target_ids.size < 2
|
||||
|
||||
successful_target_ids = @original_target_ids.map(&:to_i)
|
||||
|
||||
# Remove any issues that failed standard validation on the base issue
|
||||
# (e.g., they don't exist, or cross-project linking is disabled)
|
||||
if @unsaved_relations.present?
|
||||
failed_ids = @unsaved_relations.map(&:issue_to_id)
|
||||
successful_target_ids -= failed_ids
|
||||
end
|
||||
|
||||
# Fetch the valid issues for cross-linking
|
||||
valid_issues = Issue.visible(User.current).where(id: successful_target_ids).to_a
|
||||
|
||||
# Generate all unique pairs from the list (e.g., 12->13, 12->15, 13->15)
|
||||
valid_issues.combination(2).each do |issue_from, issue_to|
|
||||
relation = IssueRelation.new
|
||||
relation.issue_from = issue_from
|
||||
relation.safe_attributes = @original_relation_params.merge('issue_to_id' => issue_to.id)
|
||||
relation.init_journals(User.current)
|
||||
|
||||
begin
|
||||
# Standard saves trigger core validations. If an issue creates a circular
|
||||
# dependency (e.g., with 'blocks'), Redmine will safely reject it here silently.
|
||||
relation.save
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
# Suppress errors if a duplicate relation somehow already exists in the database
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user