Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add short-hand syntaxes to add reference replacements

Jan Sandbrink07a2b146ae73
diff --git a/app/services/principals/replace_references_service.rb b/app/services/principals/replace_references_service.rb
index faef6f921ce..a3b7af255f8 100644
--- a/app/services/principals/replace_references_service.rb
+++ b/app/services/principals/replace_references_service.rb
@@ -33,6 +33,12 @@ module Principals
class << self
attr_reader :replacements, :foreign_keys
+ def add_replacements(attributes_by_class_name)
+ attributes_by_class_name.each do |class_name, attributes|
+ Array(attributes).each { |attribute| add_replacement(class_name, attribute) }
+ end
+ end
+
def add_replacement(class_name, attribute)
@replacements ||= {}
@replacements[class_name] ||= Set.new
diff --git a/config/initializers/replace_references_service.rb b/config/initializers/replace_references_service.rb
index 39684a6c270..e04b0ca16cb 100644
--- a/config/initializers/replace_references_service.rb
+++ b/config/initializers/replace_references_service.rb
@@ -27,28 +27,27 @@
#++
Rails.application.reloader.to_prepare do
- Principals::ReplaceReferencesService.add_replacement("AuthProvider", :creator_id)
- Principals::ReplaceReferencesService.add_replacement("Attachment", :author_id)
- Principals::ReplaceReferencesService.add_replacement("Budget", :author_id)
- Principals::ReplaceReferencesService.add_replacement("Changeset", :user_id)
- Principals::ReplaceReferencesService.add_replacement("Comment", :author_id)
- Principals::ReplaceReferencesService.add_replacement("CostEntry", :logged_by_id)
- Principals::ReplaceReferencesService.add_replacement("CostEntry", :user_id)
- Principals::ReplaceReferencesService.add_replacement("CostQuery", :user_id)
- Principals::ReplaceReferencesService.add_replacement("::Doorkeeper::Application", :owner_id)
- Principals::ReplaceReferencesService.add_replacement("MeetingAgenda", :author_id)
- Principals::ReplaceReferencesService.add_replacement("MeetingAgendaItem", :author_id)
- Principals::ReplaceReferencesService.add_replacement("MeetingAgendaItem", :presenter_id)
- Principals::ReplaceReferencesService.add_replacement("MeetingMinutes", :author_id)
- Principals::ReplaceReferencesService.add_replacement("MeetingParticipant", :user_id)
- Principals::ReplaceReferencesService.add_replacement("Message", :author_id)
- Principals::ReplaceReferencesService.add_replacement("News", :author_id)
- Principals::ReplaceReferencesService.add_replacement("::Notification", :actor_id)
- Principals::ReplaceReferencesService.add_replacement("::Query", :user_id)
- Principals::ReplaceReferencesService.add_replacement("TimeEntry", :logged_by_id)
- Principals::ReplaceReferencesService.add_replacement("TimeEntry", :user_id)
- Principals::ReplaceReferencesService.add_replacement("WikiPage", :author_id)
- Principals::ReplaceReferencesService.add_replacement("WorkPackage", :author_id)
- Principals::ReplaceReferencesService.add_replacement("WorkPackage", :assigned_to_id)
- Principals::ReplaceReferencesService.add_replacement("WorkPackage", :responsible_id)
+ Principals::ReplaceReferencesService.add_replacements(
+ {
+ "AuthProvider" => :creator_id,
+ "Attachment" => :author_id,
+ "Budget" => :author_id,
+ "Changeset" => :user_id,
+ "Comment" => :author_id,
+ "CostEntry" => %i[logged_by_id user_id],
+ "CostQuery" => :user_id,
+ "::Doorkeeper::Application" => :owner_id,
+ "MeetingAgenda" => :author_id,
+ "MeetingAgendaItem" => %i[author_id presenter_id],
+ "MeetingMinutes" => :author_id,
+ "MeetingParticipant" => :user_id,
+ "Message" => :author_id,
+ "News" => :author_id,
+ "::Notification" => :actor_id,
+ "::Query" => :user_id,
+ "TimeEntry" => %i[logged_by_id user_id],
+ "WikiPage" => :author_id,
+ "WorkPackage" => %i[author_id assigned_to_id responsible_id]
+ }
+ )
end
diff --git a/lib/open_project/plugins/acts_as_op_engine.rb b/lib/open_project/plugins/acts_as_op_engine.rb
index 5e2213a6634..8cd78d0f83c 100644
--- a/lib/open_project/plugins/acts_as_op_engine.rb
+++ b/lib/open_project/plugins/acts_as_op_engine.rb
@@ -313,6 +313,23 @@ module OpenProject::Plugins
OpenProject::Inflector.inflection(overrides)
end
end
+
+ # Adds a replacement rule for a principal reference. When the module allows adding a
+ # principal (e.g. a user) reference to a model, a replacement rule should be added so
+ # that the user can be deleted, by making sure references to that user are replaced
+ # on that model as well.
+ def replace_principal_reference(class_name, attribute)
+ config.to_prepare do
+ Principals::ReplaceReferencesService.add_replacement(class_name, attribute)
+ end
+ end
+
+ # Like #replace_principal_reference, but allows to add multiple classes and attributes at once.
+ def replace_principal_references(attributes_by_class_name)
+ config.to_prepare do
+ Principals::ReplaceReferencesService.add_replacements(attributes_by_class_name)
+ end
+ end
end
end
end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index 2e38777f9a5..c98483d74a8 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -50,14 +50,6 @@ module OpenProject::Storages
# please see comments inside ActsAsOpEngine class
include OpenProject::Plugins::ActsAsOpEngine
- initializer "openproject_storages.replace_references" do
- Rails.application.reloader.to_prepare do
- Principals::ReplaceReferencesService.add_replacement("::Storages::Storage", :creator_id)
- Principals::ReplaceReferencesService.add_replacement("::Storages::ProjectStorage", :creator_id)
- Principals::ReplaceReferencesService.add_replacement("::Storages::FileLink", :creator_id)
- end
- end
-
initializer "openproject_storages.feature_decisions" do
OpenProject::FeatureDecisions.add :storage_file_picking_select_all
end
@@ -370,5 +362,9 @@ module OpenProject::Storages
}
}
end
+
+ replace_principal_reference("::Storages::Storage", :creator_id)
+ replace_principal_reference("::Storages::ProjectStorage", :creator_id)
+ replace_principal_reference("::Storages::FileLink", :creator_id)
end
end