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

Attempt to reduce `Metrics/AbcSize` size on `AddWorkPackageNoteService#call` (#19311)

Kabiru Mwenjaeaecefc346fc
diff --git a/app/services/add_work_package_note_service.rb b/app/services/add_work_package_note_service.rb
index 34efb080103..5f6b9beb75f 100644
--- a/app/services/add_work_package_note_service.rb
+++ b/app/services/add_work_package_note_service.rb
@@ -44,31 +44,46 @@ class AddWorkPackageNoteService
self.contract_class = WorkPackages::CreateNoteContract
end
- def call(notes, send_notifications: nil, internal: false) # rubocop:disable Metrics/AbcSize
+ def call(notes, send_notifications: nil, internal: false)
in_context(work_package, send_notifications:) do
- work_package.add_journal(user:, notes:, internal:)
+ add_journal_to_work_package(notes, internal)
+ success, errors = save_journal_with_validation
- success, errors = validate_and_yield(work_package, user) do
- work_package.save_journals
- end
+ journal = fetch_latest_journal if success
- if success
- # In test environment, because of the difference in the way of handling transactions,
- # the journal needs to be actively loaded without SQL caching in place.
- journal = Journal.connection.uncached do
- work_package.journals.last
- end
- end
-
- ServiceResult.new(success:, result: journal, errors:).on_success do |r|
- attachments_claims = claim_attachments_for(r.result)
- r.add_dependent!(attachments_claims)
- end
+ build_service_result(success, journal, errors)
end
end
private
+ def add_journal_to_work_package(notes, internal)
+ work_package.add_journal(user:, notes:, internal:)
+ end
+
+ def save_journal_with_validation
+ validate_and_yield(work_package, user) do
+ work_package.save_journals
+ end
+ end
+
+ def fetch_latest_journal
+ # In test environment, because of the difference in the way of handling transactions,
+ # the journal needs to be actively loaded without SQL caching in place.
+ Journal.connection.uncached do
+ work_package.journals.last
+ end
+ end
+
+ def build_service_result(success, result, errors)
+ ServiceResult.new(success:, result:, errors:).on_success { add_attachment_claims_to(it) }
+ end
+
+ def add_attachment_claims_to(service_result)
+ attachments_claims = claim_attachments_for(service_result.result)
+ service_result.add_dependent!(attachments_claims)
+ end
+
def claim_attachments_for(journal)
WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService
.new(user: User.current, model: journal)