From 083d7c09e654ffe70e97abb225e30991df3b473e Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sat, 10 Aug 2019 17:30:24 +1200 Subject: [PATCH] Migration of notifications to mailboxer, without sending new emails --- app/models/notification.rb | 2 ++ ...190720000625_notifications_to_mailboxer.rb | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 096a0fefa..8a786c8fa 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -4,6 +4,8 @@ class Notification < ApplicationRecord belongs_to :post, optional: true validates :subject, length: { maximum: 255 } + validates :subject, presence: true + validates :body, presence: true scope :unread, -> { where(read: false) } scope :by_recipient, ->(recipient) { where(recipient_id: recipient) } diff --git a/db/migrate/20190720000625_notifications_to_mailboxer.rb b/db/migrate/20190720000625_notifications_to_mailboxer.rb index 9ea60b7d1..513ae1db2 100644 --- a/db/migrate/20190720000625_notifications_to_mailboxer.rb +++ b/db/migrate/20190720000625_notifications_to_mailboxer.rb @@ -1,7 +1,27 @@ class NotificationsToMailboxer < ActiveRecord::Migration[5.2] - def change + def up Notification.all.each do |n| - n.sender.send_message(n.recipient, n.body, n.subject) + next unless n.valid? + + conversation = Mailboxer::ConversationBuilder.new( + subject: n.subject, + created_at: n.created_at, + updated_at: n.updated_at + ).build + + message = Mailboxer::MessageBuilder.new( + sender: n.sender, + conversation: conversation, + recipients: [n.recipient], + body: n.body, + subject: n.subject, + # attachment: attachment, + created_at: n.created_at, + updated_at: n.updated_at + ).build + + conversation.save! + message.save! end end end