mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-05-16 20:49:26 -04:00
Migration from notifications to mailboxer
This commit is contained in:
@@ -24,4 +24,35 @@ class Notification < ApplicationRecord
|
||||
def send_message
|
||||
sender.send_message(recipient, body, subject)
|
||||
end
|
||||
|
||||
def migrate_to_mailboxer!
|
||||
conversation = Mailboxer::ConversationBuilder.new(
|
||||
subject: subject,
|
||||
created_at: created_at,
|
||||
updated_at: updated_at
|
||||
).build
|
||||
|
||||
message = Mailboxer::MessageBuilder.new(
|
||||
sender: sender,
|
||||
conversation: conversation,
|
||||
recipients: [recipient],
|
||||
body: body,
|
||||
subject: subject,
|
||||
# attachment: attachment,
|
||||
created_at: created_at,
|
||||
updated_at: updated_at
|
||||
).build
|
||||
|
||||
notification = Mailboxer::NotificationBuilder.new(
|
||||
recipients: [recipient],
|
||||
subject: subject,
|
||||
body: body,
|
||||
sender: sender
|
||||
).build
|
||||
|
||||
conversation.save!
|
||||
message.save!
|
||||
notification.save!
|
||||
notification.deliver(false, false)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,27 +1,10 @@
|
||||
class NotificationsToMailboxer < ActiveRecord::Migration[5.2]
|
||||
def up
|
||||
Notification.all.each do |n|
|
||||
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!
|
||||
Notification.find_in_batches do |group|
|
||||
group.each do |n|
|
||||
next unless n.valid?
|
||||
n.migrate_to_mailboxer!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,6 +3,27 @@ require 'rails_helper'
|
||||
describe Notification do
|
||||
let(:notification) { FactoryBot.create(:notification) }
|
||||
|
||||
describe 'migration to mailboxer' do
|
||||
let(:body) do
|
||||
"Hellos
|
||||
how are you today?
|
||||
I am fine
|
||||
|
||||
-- me
|
||||
"
|
||||
end
|
||||
let(:sender) { FactoryBot.create :member }
|
||||
let(:recipient) { FactoryBot.create :member }
|
||||
let(:notification) do
|
||||
FactoryBot.create :notification,
|
||||
subject: 'hello', body: body,
|
||||
sender: sender,
|
||||
recipient: recipient
|
||||
end
|
||||
before { notification.migrate_to_mailboxer! }
|
||||
it { expect(recipient.mailbox.inbox.count).to eq 1 }
|
||||
it { expect(recipient.mailbox.inbox.first.subject).to eq 'hello' }
|
||||
end
|
||||
it "belongs to a post" do
|
||||
expect(notification.post).to be_an_instance_of Post
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user