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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
fix[Op#59433]: simplify reminder model to directly linked to notification
Kabiru Mwenjaae3f77da803f
diff --git a/app/models/reminder.rb b/app/models/reminder.rbindex bc4d9802b5a..8218ea97221 100644--- a/app/models/reminder.rb+++ b/app/models/reminder.rb@@ -29,9 +29,7 @@class Reminder < ApplicationRecordbelongs_to :remindable, polymorphic: truebelongs_to :creator, class_name: "User"+ belongs_to :notification, optional: true- has_many :reminder_notifications, dependent: :destroy-- def notified? = notified_at.present?- def scheduled? = job_id.present?+ enum :status, { pending: 0, scheduled: 1, notified: 2, done: 9 }enddiff --git a/app/models/reminder_notification.rb b/app/models/reminder_notification.rbdeleted file mode 100644index cbdd70905a9..00000000000--- a/app/models/reminder_notification.rb+++ /dev/null@@ -1,4 +0,0 @@-class ReminderNotification < ApplicationRecord- belongs_to :reminder- belongs_to :notification-enddiff --git a/app/services/reminders/create_service.rb b/app/services/reminders/create_service.rbindex 426c0881959..7674e63e059 100644--- a/app/services/reminders/create_service.rb+++ b/app/services/reminders/create_service.rb@@ -31,7 +31,7 @@ module Remindersdef after_perform(service_call)reminder = service_call.resultjob = Reminders::ScheduleReminderJob.schedule(reminder)- reminder.update_column(:job_id, job.job_id)+ reminder.update_columns(status: :scheduled, job_id: job.job_id)service_callenddiff --git a/app/workers/reminders/schedule_reminder_job.rb b/app/workers/reminders/schedule_reminder_job.rbindex 395c95123dc..95d64057b92 100644--- a/app/workers/reminders/schedule_reminder_job.rb+++ b/app/workers/reminders/schedule_reminder_job.rb@@ -40,8 +40,7 @@ module Reminderscreate_notification_service = create_notification_from_reminder(reminder)create_notification_service.on_success do |service_result|- ReminderNotification.create!(reminder:, notification: service_result.result)- reminder.update_column(:notified_at, Time.current)+ reminder.update_columns(status: :notified, notification_id: service_result.result.id)endcreate_notification_service.on_failure do |service_result|diff --git a/db/migrate/20241119131205_create_reminders.rb b/db/migrate/20241119131205_create_reminders.rbindex bcc84c73ae9..e74d1601642 100644--- a/db/migrate/20241119131205_create_reminders.rb+++ b/db/migrate/20241119131205_create_reminders.rb@@ -3,10 +3,11 @@ class CreateReminders < ActiveRecord::Migration[7.1]create_table :reminders do |t|t.references :remindable, polymorphic: true, null: falset.references :creator, null: false, foreign_key: { to_table: :users }+ t.references :notification, foreign_key: truet.datetime :remind_at, null: false- t.datetime :notified_at+ t.integer :status, default: 0, null: falset.string :job_id- t.text :note+ t.string :notet.timestampsenddiff --git a/db/migrate/20241121113638_create_reminder_notifications.rb b/db/migrate/20241121113638_create_reminder_notifications.rbdeleted file mode 100644index eaa711b431e..00000000000--- a/db/migrate/20241121113638_create_reminder_notifications.rb+++ /dev/null@@ -1,14 +0,0 @@-class CreateReminderNotifications < ActiveRecord::Migration[7.1]- def change- create_table :reminder_notifications do |t|- t.references :reminder, foreign_key: true- t.references :notification, foreign_key: true-- t.timestamps- end-- add_index :reminder_notifications, :notification_id,- unique: true,- name: "index_reminder_notifications_unique"- end-enddiff --git a/spec/factories/reminders_factory.rb b/spec/factories/reminders_factory.rbindex e6335941afa..cda7d6a84e9 100644--- a/spec/factories/reminders_factory.rb+++ b/spec/factories/reminders_factory.rb@@ -38,7 +38,9 @@ FactoryBot.define doendtrait :notified do- notified_at { remind_at + 1.second }+ job_id { SecureRandom.uuid }+ status { :notified }+ notification factory: :notificationendendenddiff --git a/spec/models/reminder_notification_spec.rb b/spec/models/reminder_notification_spec.rbdeleted file mode 100644index 95c8193813c..00000000000--- a/spec/models/reminder_notification_spec.rb+++ /dev/null@@ -1,36 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) the OpenProject GmbH-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License version 3.-#-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:-# Copyright (C) 2006-2013 Jean-Philippe Lang-# Copyright (C) 2010-2013 the ChiliProject Team-#-# This program is free software; you can redistribute it and/or-# modify it under the terms of the GNU General Public License-# as published by the Free Software Foundation; either version 2-# of the License, or (at your option) any later version.-#-# This program is distributed in the hope that it will be useful,-# but WITHOUT ANY WARRANTY; without even the implied warranty of-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-# GNU General Public License for more details.-#-# You should have received a copy of the GNU General Public License-# along with this program; if not, write to the Free Software-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.-#-# See COPYRIGHT and LICENSE files for more details.-#++--require "spec_helper"--RSpec.describe ReminderNotification do- describe "Associations" do- it { is_expected.to belong_to(:reminder) }- it { is_expected.to belong_to(:notification) }- end-enddiff --git a/spec/models/reminder_spec.rb b/spec/models/reminder_spec.rbindex f8a6bbb6cb7..a65bcfd8857 100644--- a/spec/models/reminder_spec.rb+++ b/spec/models/reminder_spec.rb@@ -32,34 +32,14 @@ RSpec.describe Reminder dodescribe "Associations" doit { is_expected.to belong_to(:remindable) }it { is_expected.to belong_to(:creator).class_name("User") }- it { is_expected.to have_many(:reminder_notifications).dependent(:destroy) }+ it { is_expected.to belong_to(:notification).optional }end- describe "#notified?" do- context "when notified_at is present" do- subject { build(:reminder, :notified) }-- it { is_expected.to be_notified }- end-- context "when notified_at is not present" do- subject { build(:reminder) }-- it { is_expected.not_to be_notified }- end- end-- describe "#scheduled?" do- context "when job_id is present" do- subject { build(:reminder, :scheduled) }-- it { is_expected.to be_scheduled }- end-- context "when job_id is not present" do- subject { build(:reminder) }-- it { is_expected.not_to be_scheduled }+ describe "Enums" do+ it do+ expect(subject).to define_enum_for(:status)+ .with_values({ pending: 0, scheduled: 1, notified: 2, done: 9 })+ .backed_by_column_of_type(:integer)endendenddiff --git a/spec/services/reminders/create_service_spec.rb b/spec/services/reminders/create_service_spec.rbindex 78ae257ed71..76a89a0bc27 100644--- a/spec/services/reminders/create_service_spec.rb+++ b/spec/services/reminders/create_service_spec.rb@@ -36,7 +36,7 @@ RSpec.describe Reminders::CreateService, type: :model dolet(:factory) { :reminder }before do- allow(model_instance).to receive(:update_column).and_return(true)+ allow(model_instance).to receive(:update_columns)allow(Reminders::ScheduleReminderJob).to receive(:schedule).with(model_instance).and_return(instance_double(Reminders::ScheduleReminderJob, job_id: 1))@@ -46,7 +46,7 @@ RSpec.describe Reminders::CreateService, type: :model dosubjectexpect(Reminders::ScheduleReminderJob).to have_received(:schedule).with(model_instance)- expect(model_instance).to have_received(:update_column).with(:job_id, 1)+ expect(model_instance).to have_received(:update_columns).with(status: :scheduled, job_id: 1)endendenddiff --git a/spec/workers/reminders/schedule_reminder_job_spec.rb b/spec/workers/reminders/schedule_reminder_job_spec.rbindex 4b3ed9fca65..036d251e164 100644--- a/spec/workers/reminders/schedule_reminder_job_spec.rb+++ b/spec/workers/reminders/schedule_reminder_job_spec.rb@@ -49,11 +49,11 @@ RSpec.describe Reminders::ScheduleReminderJob doit "creates a notification from the reminder" donotification_svc = nil- expect { notification_svc = subject }.to change(Notification, :count).by(1) & change(ReminderNotification, :count).by(1)+ expect { notification_svc = subject }.to change(Notification, :count).by(1) & change(reminder, :status).to("notified")- aggregate_failures "notification attributes" do- notification = notification_svc.result+ notification = notification_svc.result+ aggregate_failures "notification attributes" doexpect(notification.recipient_id).to eq(reminder.creator_id)expect(notification.resource).to eq(reminder.remindable)expect(notification.reason).to eq("reminder")@@ -61,16 +61,20 @@ RSpec.describe Reminders::ScheduleReminderJob doaggregate_failures "marks the reminder as notified" doexpect(reminder.reload).to be_notified+ expect(reminder.notification_id).to eq(notification.id)endendcontext "when the reminder is already notified" do+ let(:reminder) { build_stubbed(:reminder, :notified) }+before do- reminder.update_column(:notified_at, Time.current)+ allow(Notifications::CreateService).to receive(:new).and_call_originalendit "does not create a notification from the reminder" doexpect { subject }.not_to change(Notification, :count)+ expect(Notifications::CreateService).not_to have_received(:new)endendend