forked from mailboxer/mailboxer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor MailDispatcher to consume receipts Extract RecipentFilter into own class.
- Loading branch information
Showing
11 changed files
with
119 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20151103080417_add_delivery_tracking_info_to_mailboxer_receipts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddDeliveryTrackingInfoToMailboxerReceipts < ActiveRecord::Migration | ||
def change | ||
add_column :mailboxer_receipts, :is_delivered, :boolean, default: false | ||
add_column :mailboxer_receipts, :delivery_method, :string | ||
add_column :mailboxer_receipts, :message_id, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,6 @@ module Models | |
@@default_from = "[email protected]" | ||
mattr_accessor :uses_emails | ||
@@uses_emails = true | ||
mattr_accessor :mailer_wants_array | ||
@@mailer_wants_array = false | ||
mattr_accessor :search_enabled | ||
@@search_enabled = false | ||
mattr_accessor :search_engine | ||
|
@@ -41,3 +39,4 @@ def protected_attributes? | |
require 'mailboxer/engine' | ||
require 'mailboxer/cleaner' | ||
require 'mailboxer/mail_dispatcher' | ||
require 'mailboxer/recipient_filter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Mailboxer | ||
class RecipientFilter | ||
attr_reader :mailable, :recipients | ||
def initialize(mailable, recipients) | ||
@mailable, @recipients = mailable, recipients | ||
end | ||
|
||
# recipients can be filtered on a conversation basis | ||
def call | ||
return recipients unless mailable.respond_to?(:conversation) | ||
|
||
recipients.each_with_object([]) do |recipient, array| | ||
array << recipient if mailable.conversation.has_subscriber?(recipient) | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/dummy/db/migrate/20151103202534_add_missing_indices.mailboxer_engine.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This migration comes from mailboxer_engine (originally 20131206080417) | ||
class AddMissingIndices < ActiveRecord::Migration | ||
def change | ||
# We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63 | ||
# characters limitation. | ||
add_index :mailboxer_conversation_opt_outs, [:unsubscriber_id, :unsubscriber_type], | ||
name: 'index_mailboxer_conversation_opt_outs_on_unsubscriber_id_type' | ||
add_index :mailboxer_conversation_opt_outs, :conversation_id | ||
|
||
add_index :mailboxer_notifications, :type | ||
add_index :mailboxer_notifications, [:sender_id, :sender_type] | ||
|
||
# We'll explicitly specify its name, as the auto-generated name is too long and exceeds 63 | ||
# characters limitation. | ||
add_index :mailboxer_notifications, [:notified_object_id, :notified_object_type], | ||
name: 'index_mailboxer_notifications_on_notified_object_id_and_type' | ||
|
||
add_index :mailboxer_receipts, [:receiver_id, :receiver_type] | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
...grate/20151103202535_add_delivery_tracking_info_to_mailboxer_receipts.mailboxer_engine.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This migration comes from mailboxer_engine (originally 20151103080417) | ||
class AddDeliveryTrackingInfoToMailboxerReceipts < ActiveRecord::Migration | ||
def change | ||
add_column :mailboxer_receipts, :is_delivered, :boolean, default: false | ||
add_column :mailboxer_receipts, :delivery_method, :string | ||
add_column :mailboxer_receipts, :message_id, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,15 @@ | |
|
||
describe Mailboxer::MailDispatcher do | ||
|
||
subject(:instance) { described_class.new(mailable, recipients) } | ||
subject(:instance) { described_class.new(mailable, receipts) } | ||
|
||
let(:mailable) { Mailboxer::Notification.new } | ||
let(:recipient1) { double 'recipient1', mailboxer_email: '' } | ||
let(:recipient2) { double 'recipient2', mailboxer_email: '[email protected]' } | ||
let(:recipients) { [ recipient1, recipient2 ] } | ||
let(:recipient1) { double 'recipient1', id: 1, mailboxer_email: '' } | ||
let(:recipient2) { double 'recipient2', id: 2, mailboxer_email: '[email protected]'} | ||
let(:receipt1) { double 'receipt1', id: 1, receiver: recipient1 } | ||
let(:receipt2) { double 'receipt2', id: 2, receiver: recipient2 } | ||
|
||
let(:receipts) { [ receipt1, receipt2 ] } | ||
|
||
describe "call" do | ||
context "no emails" do | ||
|
@@ -16,19 +19,10 @@ | |
its(:call) { should be false } | ||
end | ||
|
||
context "mailer wants array" do | ||
before { Mailboxer.mailer_wants_array = true } | ||
after { Mailboxer.mailer_wants_array = false } | ||
context "mailer doesn't want array" do | ||
it 'sends collection' do | ||
expect(subject).to receive(:send_email).with(recipients) | ||
subject.call | ||
end | ||
end | ||
|
||
context "mailer doesnt want array" do | ||
it 'sends collection' do | ||
expect(subject).not_to receive(:send_email).with(recipient1) #email is blank | ||
expect(subject).to receive(:send_email).with(recipient2) | ||
expect(subject).not_to receive(:send_email).with(receipt1) #email is blank | ||
expect(subject).to receive(:send_email).with(receipt2) | ||
subject.call | ||
end | ||
end | ||
|
@@ -49,24 +43,25 @@ | |
after { Mailboxer.custom_deliver_proc = nil } | ||
it "triggers proc" do | ||
expect(my_proc).to receive(:call).with(mailer, mailable, recipient1) | ||
subject.send :send_email, recipient1 | ||
subject.send :send_email, receipt1 | ||
end | ||
end | ||
|
||
context "without custom_deliver_proc" do | ||
let(:email) { double :email } | ||
let(:email) { double :email, message_id: '[email protected]' } | ||
|
||
it "triggers standard deliver chain" do | ||
expect(mailer).to receive(:send_email).with(mailable, recipient1).and_return email | ||
expect(receipt1).to receive(:assign_attributes).with({:delivery_method=>:email, :message_id=>"[email protected]"}).and_return email | ||
expect(email).to receive :deliver | ||
|
||
subject.send :send_email, recipient1 | ||
subject.send :send_email, receipt1 | ||
end | ||
end | ||
end | ||
|
||
describe "mailer" do | ||
let(:recipients) { [] } | ||
let(:receipts) { [] } | ||
|
||
context "mailable is a Message" do | ||
let(:mailable) { Mailboxer::Notification.new } | ||
|
@@ -93,22 +88,4 @@ | |
end | ||
end | ||
end | ||
|
||
describe "filtered_recipients" do | ||
context "responds to conversation" do | ||
let(:conversation) { double 'conversation' } | ||
let(:mailable) { double 'mailable', :conversation => conversation } | ||
before(:each) do | ||
expect(conversation).to receive(:has_subscriber?).with(recipient1).and_return false | ||
expect(conversation).to receive(:has_subscriber?).with(recipient2).and_return true | ||
end | ||
|
||
its(:filtered_recipients){ should eq [recipient2] } | ||
end | ||
|
||
context 'doesnt respond to conversation' do | ||
let(:mailable) { double 'mailable' } | ||
its(:filtered_recipients){ should eq recipients } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'spec_helper' | ||
|
||
describe Mailboxer::RecipientFilter do | ||
subject(:instance) { described_class.new(mailable, recipients) } | ||
let(:recipient1) { double 'recipient1', id: 1, mailboxer_email: '' } | ||
let(:recipient2) { double 'recipient2', id: 2, mailboxer_email: '[email protected]'} | ||
let(:recipients) { [ recipient1, recipient2 ] } | ||
|
||
describe "call" do | ||
context "responds to conversation" do | ||
let(:conversation) { double 'conversation' } | ||
let(:mailable) { double 'mailable', :conversation => conversation } | ||
before(:each) do | ||
expect(conversation).to receive(:has_subscriber?).with(recipient1).and_return false | ||
expect(conversation).to receive(:has_subscriber?).with(recipient2).and_return true | ||
end | ||
|
||
its(:call){ should eq [recipient2] } | ||
end | ||
|
||
context 'doesnt respond to conversation' do | ||
let(:mailable) { double 'mailable' } | ||
its(:call){ should eq recipients } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters