Skip to content

Commit

Permalink
Add short-hand syntaxes to add reference replacements
Browse files Browse the repository at this point in the history
There is now add_replacements which accepts a hash, allowing to
add multiple replacements in one call. This should make mass-adding
replacements look less bulky.

Also including helpers to ActsAsOpEngine to make adding replacements
from a module nicer.
  • Loading branch information
NobodysNightmare committed Dec 19, 2024
1 parent 4536859 commit 7164635
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
6 changes: 6 additions & 0 deletions app/services/principals/replace_references_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class ReplaceReferencesService
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
Expand Down
45 changes: 21 additions & 24 deletions config/initializers/replace_references_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,25 @@
#++

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,

Check notice on line 31 in config/initializers/replace_references_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] config/initializers/replace_references_service.rb#L31 <Layout/FirstHashElementIndentation>

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
Raw output
config/initializers/replace_references_service.rb:31:5: C: Layout/FirstHashElementIndentation: Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.
"Attachment" => :author_id,
"Budget" => :author_id,
"Changeset" => :user_id,
"Comment" => :author_id,
"CostEntry" => [:logged_by_id, user_id],
"CostQuery" => :user_id,
"::Doorkeeper::Application" => :owner_id,
"MeetingAgenda" => :author_id,
"MeetingAgendaItem" => [:author_id, :presenter_id],

Check notice on line 40 in config/initializers/replace_references_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] config/initializers/replace_references_service.rb#L40 <Style/SymbolArray>

Use `%i` or `%I` for an array of symbols.
Raw output
config/initializers/replace_references_service.rb:40:28: C: Style/SymbolArray: Use `%i` or `%I` for an array of symbols.
"MeetingMinutes" => :author_id,
"MeetingParticipant" => :user_id,
"Message" => :author_id,
"News" => :author_id,
"::Notification" => :actor_id,
"::Query" => :user_id,
"TimeEntry" => [:logged_by_id, :user_id],

Check notice on line 47 in config/initializers/replace_references_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] config/initializers/replace_references_service.rb#L47 <Style/SymbolArray>

Use `%i` or `%I` for an array of symbols.
Raw output
config/initializers/replace_references_service.rb:47:20: C: Style/SymbolArray: Use `%i` or `%I` for an array of symbols.
"WikiPage" => :author_id,
"WorkPackage" => [:author_id, :assigned_to_id, :responsible_id]

Check notice on line 49 in config/initializers/replace_references_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] config/initializers/replace_references_service.rb#L49 <Style/SymbolArray>

Use `%i` or `%I` for an array of symbols.
Raw output
config/initializers/replace_references_service.rb:49:22: C: Style/SymbolArray: Use `%i` or `%I` for an array of symbols.
})

Check notice on line 50 in config/initializers/replace_references_service.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] config/initializers/replace_references_service.rb#L50 <Layout/FirstHashElementIndentation>

Indent the right brace the same as the first position after the preceding left parenthesis.
Raw output
config/initializers/replace_references_service.rb:50:3: C: Layout/FirstHashElementIndentation: Indent the right brace the same as the first position after the preceding left parenthesis.
end
17 changes: 17 additions & 0 deletions lib/open_project/plugins/acts_as_op_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ def class_inflection_override(overrides)
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
12 changes: 4 additions & 8 deletions modules/storages/lib/open_project/storages/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ def self.external_file_permissions
# 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
Expand Down Expand Up @@ -370,5 +362,9 @@ def self.external_file_permissions
}
}
end

replace_principal_reference("::Storages::Storage", :creator_id)
replace_principal_reference("::Storages::ProjectStorage", :creator_id)
replace_principal_reference("::Storages::FileLink", :creator_id)
end
end

0 comments on commit 7164635

Please sign in to comment.