Skip to content

Commit

Permalink
added configurable_mailer module
Browse files Browse the repository at this point in the history
  • Loading branch information
amaierhofer committed May 13, 2012
1 parent ad06bb7 commit 1dc688b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ Mailboxer.setup do |config|
end
````

You can change the way in which emails are delivered by specifying a custom implementation for notification and message mailer

````ruby
Mailboxer.setup do |config|
config.notification_mailer = CustomNotificationMailer
config.message_mailer = CustomMessageMailer
...
end
````

### User identities

Users must have an identity defined by a `name` and an `email`. We must assure that Messageable models have some specific methods. These methods are:
Expand Down Expand Up @@ -236,4 +246,4 @@ If you need a GUI you should take a look a this links:
* [RKushnir](https://github.com/ging/mailboxer/commits/master?author=RKushnir) (Roman Kushnir)
* [tonydewan](https://github.com/ging/mailboxer/commits/master?author=tonydewan) (Tony Dewan)
* [plentz](https://github.com/ging/mailboxer/commits/master?author=plentz) (Diego Plentz)
* [laserlemon](https://github.com/ging/mailboxer/commits/master?author=laserlemon) (Steve Richert)
* [laserlemon](https://github.com/ging/mailboxer/commits/master?author=laserlemon) (Steve Richert)
4 changes: 3 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Message < Notification
}

mount_uploader :attachment, AttachmentUploader

include Concerns::ConfigurableMailer

class << self
#Sets the on deliver callback method.
Expand Down Expand Up @@ -49,7 +51,7 @@ def deliver(reply = false, should_clean = true)
if Mailboxer.uses_emails
email_to = r.send(Mailboxer.email_method,self)
unless email_to.blank?
MessageMailer.send_email(self,r).deliver
get_mailer.send_email(self,r).deliver
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Notification < ActiveRecord::Base
joins(:receipts).where('receipts.read' => false)
}

include Concerns::ConfigurableMailer

class << self
#Sends a Notification to all the recipients
def notify_all(recipients,subject,body,obj = nil,sanitize_text = true,notification_code=nil)
Expand Down Expand Up @@ -77,9 +79,6 @@ def deliver(should_clean = true)
return temp_receipts if temp_receipts.size > 1
return temp_receipts.first
end
def get_mailer
Mailboxer.notification_mailer || NotificationMailer
end

#Returns the recipients of the Notification
def recipients
Expand Down
2 changes: 2 additions & 0 deletions lib/mailboxer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Models
mattr_accessor :name_method
@@name_method = :name
mattr_accessor :notification_mailer
mattr_accessor :message_mailer

class << self
def setup
Expand All @@ -27,3 +28,4 @@ def setup
# reopen ActiveRecord and include all the above to make
# them available to all our models if they want it
require 'mailboxer/engine'
require 'mailboxer/concerns/configurable_mailer'
13 changes: 13 additions & 0 deletions lib/mailboxer/concerns/configurable_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'active_support/inflections'

module Concerns
module ConfigurableMailer

def get_mailer
return @mailer if @mailer
method = "#{self.class.to_s.downcase}_mailer".to_sym
@mailer = Mailboxer.send(method) || "#{self.class}Mailer".constantize
end

end
end
27 changes: 27 additions & 0 deletions spec/mailboxer/concerns/configurable_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'
describe Concerns::ConfigurableMailer do

describe "Notification instance#get_mailer" do
before { @obj = Notification.new }
it "returns default_mailer" do
@obj.get_mailer.should eq NotificationMailer
end
it "returns 'foo' from Mailerbox.notification_mailer" do
Mailboxer.notification_mailer = 'foo'
@obj.get_mailer.should eq 'foo'
end
after { Mailboxer.notification_mailer = nil }
end

describe "Message instance#get_mailer" do
before { @obj = Message.new }
it "returns default_mailer" do
@obj.get_mailer.should eq MessageMailer
end
it "returns 'foo' from Mailerbox.message_mailer" do
Mailboxer.message_mailer = 'foo'
@obj.get_mailer.should eq 'foo'
end
after { Mailboxer.message_mailer = nil }
end
end
11 changes: 0 additions & 11 deletions spec/mailboxer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,4 @@
it "should be valid" do
Mailboxer.should be_a(Module)
end

describe "configuring notification mailer" do
before { Mailboxer.notification_mailer.should eq nil }

it "can override notification mailer" do
Mailboxer.notification_mailer = "foo"
Mailboxer.notification_mailer.should eq "foo"
end

after { Mailboxer.notification_mailer.should eq nil }
end
end
13 changes: 0 additions & 13 deletions spec/models/notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,4 @@

end

describe "#get_mailer obtains mailer from config" do
before { Mailboxer.notification_mailer.should be_nil }

it "defaults to NotificationMailer" do
subject.get_mailer.should eq NotificationMailer
end
it "can be overriden on Mailboxer" do
Mailboxer.notification_mailer = 'foo'
subject.get_mailer.should eq 'foo'
end

after { Mailboxer.notification_mailer = nil }
end
end

0 comments on commit 1dc688b

Please sign in to comment.