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

extract methods

ulfertsd37bdb71f049
diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb
index 1ca2f58b3c3..d43083b2027 100644
--- a/app/controllers/journals_controller.rb
+++ b/app/controllers/journals_controller.rb
@@ -64,7 +64,7 @@ class JournalsController < ApplicationController
end
def diff
- journal = Journal::AggregatedJournal.for_journal(@journal)
+ journal = Journal::AggregatedJournal.containing_journal(@journal)
field = params[:field].parameterize.underscore.to_sym
unless valid_diff?
diff --git a/app/models/journal/aggregated_journal.rb b/app/models/journal/aggregated_journal.rb
index 16aa4691d74..95367d36c5f 100644
--- a/app/models/journal/aggregated_journal.rb
+++ b/app/models/journal/aggregated_journal.rb
@@ -41,8 +41,13 @@
# be dropped
class Journal::AggregatedJournal
class << self
+ def with_version(pure_journal)
+ wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: pure_journal.journable)
+ wp_journals.detect { |journal| journal.version == pure_journal.version }
+ end
+
# Returns the aggregated journal that contains the specified (vanilla/pure) journal.
- def for_journal(pure_journal)
+ def containing_journal(pure_journal)
raw = Journal::AggregatedJournal.query_aggregated_journals(journable: pure_journal.journable)
.where("#{version_projection} >= ?", pure_journal.version)
.first
diff --git a/app/services/notifications/journal_notification_service.rb b/app/services/notifications/journal_notification_service.rb
index 94c829e3d95..a1eab12b3fd 100644
--- a/app/services/notifications/journal_notification_service.rb
+++ b/app/services/notifications/journal_notification_service.rb
@@ -44,14 +44,21 @@ class Notifications::JournalNotificationService
return nil unless send_notification?(journal) && send_mails
return nil unless ::UserMailer.perform_deliveries
+ notify_for_predecessor(journal)
+ enqueue_work_package_notification(journal)
+ end
+
+ # Send the notification on behalf of the predecessor in case it could not send it on its own
+ def notify_for_predecessor(journal)
aggregated = find_aggregated_journal_for(journal)
- # Send the notification on behalf of the predecessor in case it could not send it on its own
if Journal::AggregatedJournal.hides_notifications?(aggregated, aggregated.predecessor)
work_package = aggregated.predecessor.journable
notify_journal_complete(work_package, aggregated.predecessor)
end
+ end
+ def enqueue_work_package_notification(journal)
job = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id)
Delayed::Job.enqueue job,
run_at: delivery_time,
diff --git a/app/services/notifications/journal_notifier.rb b/app/services/notifications/journal_notifier.rb
index 775743d92fb..f280614162d 100644
--- a/app/services/notifications/journal_notifier.rb
+++ b/app/services/notifications/journal_notifier.rb
@@ -32,18 +32,25 @@ module Notifications::JournalNotifier
private
def find_aggregated_journal_for(raw_journal)
- wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: raw_journal.journable)
- wp_journals.detect { |journal| journal.version == raw_journal.version }
+ Journal::AggregatedJournal.with_version(raw_journal)
end
def notify_journal_complete(work_package, journal)
+ journal_complete_mail(work_package, journal)
+
+ journal_complete_notification(journal)
+ end
+
+ def journal_complete_mail(work_package, journal)
notification_receivers(work_package, journal).each do |recipient|
job = DeliverWorkPackageNotificationJob.new(journal.id,
recipient.id,
User.current.id)
Delayed::Job.enqueue job, priority: ::ApplicationJob.priority_number(:notification)
end
+ end
+ def journal_complete_notification(journal)
OpenProject::Notifications.send(
OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
journal_id: journal.id,
@@ -83,8 +90,8 @@ module Notifications::JournalNotifier
.each(&:compact!)
base_scope = User
- .includes(:groups)
- .references(:groups_users)
+ .includes(:groups)
+ .references(:groups_users)
by_id = base_scope.where(id: user_ids || [])
by_login = base_scope.where(login: user_login_names || [])
diff --git a/app/workers/deliver_work_package_notification_job.rb b/app/workers/deliver_work_package_notification_job.rb
index 8f0258d396a..a72cdc91517 100644
--- a/app/workers/deliver_work_package_notification_job.rb
+++ b/app/workers/deliver_work_package_notification_job.rb
@@ -36,7 +36,7 @@ class DeliverWorkPackageNotificationJob < DeliverNotificationJob
def render_mail(recipient:, sender:)
return nil unless raw_journal # abort, assuming that the underlying WP was deleted
- journal = find_aggregated_journal
+ journal = Journal::AggregatedJournal.with_version(raw_journal)
# The caller should have ensured that the journal can't outdate anymore
# before queuing a notification
@@ -55,11 +55,6 @@ class DeliverWorkPackageNotificationJob < DeliverNotificationJob
@raw_journal ||= Journal.find_by(id: @journal_id)
end
- def find_aggregated_journal
- wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: work_package)
- wp_journals.detect { |journal| journal.version == raw_journal.version }
- end
-
def work_package
@work_package ||= raw_journal.journable
end
diff --git a/spec/models/journal/aggregated_journal_spec.rb b/spec/models/journal/aggregated_journal_spec.rb
index 5608138519f..314c733a6c0 100644
--- a/spec/models/journal/aggregated_journal_spec.rb
+++ b/spec/models/journal/aggregated_journal_spec.rb
@@ -128,10 +128,10 @@ describe Journal::AggregatedJournal, type: :model do
end
it 'returns the single journal for both original journals' do
- expect(described_class.for_journal work_package.journals.first)
+ expect(described_class.containing_journal work_package.journals.first)
.to be_equivalent_to_journal subject.first
- expect(described_class.for_journal work_package.journals.second)
+ expect(described_class.containing_journal work_package.journals.second)
.to be_equivalent_to_journal subject.first
end
@@ -171,15 +171,15 @@ describe Journal::AggregatedJournal, type: :model do
end
it 'returns the same aggregated journal for the first two originals' do
- expect(described_class.for_journal work_package.journals.first)
+ expect(described_class.containing_journal work_package.journals.first)
.to be_equivalent_to_journal subject.first
- expect(described_class.for_journal work_package.journals.second)
+ expect(described_class.containing_journal work_package.journals.second)
.to be_equivalent_to_journal subject.first
end
it 'returns a different aggregated journal for the last original' do
- expect(described_class.for_journal work_package.journals.last)
+ expect(described_class.containing_journal work_package.journals.last)
.to be_equivalent_to_journal subject.second
end
end