From e257b8e8709bde8cfa4364280e27407ae8f8df63 Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 19 Mar 2026 13:39:18 +0000 Subject: [PATCH] Use Noticed to deliver notifications on GPX import success The mailer method now needs to receive the recipient directly, instead of extracting it from the trace record. --- app/jobs/trace_importer_job.rb | 2 +- app/mailers/user_mailer.rb | 8 ++++---- app/notifiers/gpx_import_success_notifier.rb | 12 ++++++++++++ test/jobs/trace_importer_job_test.rb | 4 +++- test/mailers/previews/user_mailer_preview.rb | 2 +- test/mailers/user_mailer_test.rb | 6 +++--- 6 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 app/notifiers/gpx_import_success_notifier.rb diff --git a/app/jobs/trace_importer_job.rb b/app/jobs/trace_importer_job.rb index 953c20b40..108d300c9 100644 --- a/app/jobs/trace_importer_job.rb +++ b/app/jobs/trace_importer_job.rb @@ -7,7 +7,7 @@ class TraceImporterJob < ApplicationJob gpx = trace.import if gpx.actual_points.positive? - UserMailer.with(:trace => trace, :possible_points => gpx.actual_points).gpx_success.deliver + GpxImportSuccessNotifier.with(:record => trace, :possible_points => gpx.actual_points).deliver_later else UserMailer.with(:trace => trace, :error => "0 points parsed ok. Do they all have lat,lng,alt,timestamp?").gpx_failure.deliver trace.destroy diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 0d33a14ad..55320c9f8 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -52,10 +52,10 @@ class UserMailer < ApplicationMailer end def gpx_success - trace, possible_points = params.fetch_values(:trace, :possible_points) + trace, possible_points, recipient = params.fetch_values(:record, :possible_points, :recipient) - with_recipient_locale trace.user do - @to_user = trace.user.display_name + with_recipient_locale recipient do + @to_user = recipient.display_name @trace_url = show_trace_url(trace.user, trace) @trace_name = trace.name @trace_points = trace.size @@ -64,7 +64,7 @@ class UserMailer < ApplicationMailer @possible_points = possible_points @my_traces_url = url_for(:controller => "traces", :action => "mine") - mail :to => trace.user.email, + mail :to => recipient.email, :subject => t(".subject") end end diff --git a/app/notifiers/gpx_import_success_notifier.rb b/app/notifiers/gpx_import_success_notifier.rb new file mode 100644 index 000000000..6edf67fbf --- /dev/null +++ b/app/notifiers/gpx_import_success_notifier.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class GpxImportSuccessNotifier < ApplicationNotifier + recipients -> { record.user } + + validates :record, :presence => true + + deliver_by :email do |config| + config.mailer = "UserMailer" + config.method = "gpx_success" + end +end diff --git a/test/jobs/trace_importer_job_test.rb b/test/jobs/trace_importer_job_test.rb index ccfb1c8ce..8c5250eed 100644 --- a/test/jobs/trace_importer_job_test.rb +++ b/test/jobs/trace_importer_job_test.rb @@ -14,7 +14,9 @@ class TraceImporterJobTest < ActiveJob::TestCase end trace.stub(:import, gpx) do - TraceImporterJob.perform_now(trace) + perform_enqueued_jobs do + TraceImporterJob.perform_now(trace) + end end email = ActionMailer::Base.deliveries.last diff --git a/test/mailers/previews/user_mailer_preview.rb b/test/mailers/previews/user_mailer_preview.rb index baf21c3ee..b418a11ed 100644 --- a/test/mailers/previews/user_mailer_preview.rb +++ b/test/mailers/previews/user_mailer_preview.rb @@ -39,7 +39,7 @@ class UserMailerPreview < ActionMailer::Preview def gpx_success user = create(:user, :languages => [I18n.locale]) trace = create(:trace, :user => user) - UserMailer.with(:trace => trace, :possible_points => trace.size + 2).gpx_success + UserMailer.with(:record => trace, :possible_points => trace.size + 2, :recipient => trace.user).gpx_success end def gpx_failure diff --git a/test/mailers/user_mailer_test.rb b/test/mailers/user_mailer_test.rb index 57aa22795..1f3b1af58 100644 --- a/test/mailers/user_mailer_test.rb +++ b/test/mailers/user_mailer_test.rb @@ -54,7 +54,7 @@ class UserMailerTest < ActionMailer::TestCase create(:tracetag, :trace => t, :tag => "two&three") create(:tracetag, :trace => t, :tag => "four trace, :possible_points => 100).gpx_success + email = UserMailer.with(:record => trace, :possible_points => 100, :recipient => trace.user).gpx_success assert_match("one, two&three, four<five", email.html_part.body.to_s) assert_match("one, two&three, four trace, :possible_points => 100).gpx_success + email = UserMailer.with(:record => trace, :possible_points => 100, :recipient => trace.user).gpx_success url = url_helpers.url_for(:controller => "traces", :action => "mine") assert_select parse_html_body(email), "a[href='#{url}']" @@ -71,7 +71,7 @@ class UserMailerTest < ActionMailer::TestCase def test_gpx_success_trace_link trace = create(:trace) - email = UserMailer.with(:trace => trace, :possible_points => 100).gpx_success + email = UserMailer.with(:record => trace, :possible_points => 100, :recipient => trace.user).gpx_success url = url_helpers.show_trace_url(trace.user, trace) assert_select parse_html_body(email), "a[href='#{url}']", :text => trace.name -- 2.47.3