Skip to content

Commit

Permalink
adds subject_prefix and append_options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Malecky committed Jul 5, 2014
1 parent bc60506 commit dbd486d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
21 changes: 19 additions & 2 deletions lib/pony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ module Pony

@@options = {}
@@override_options = {}


@@subject_prefix = false
@@append_inputs = false

# Default options can be set so that they don't have to be repeated.
#
Expand All @@ -130,13 +130,30 @@ def self.override_options
@@override_options
end

def self.subject_prefix(value)
@@subject_prefix = value
end

def self.append_inputs
@@append_inputs = true
end

# Send an email
# Pony.mail(:to => 'you@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Hello there.')
# Pony.mail(:to => 'you@example.com', :html_body => '<h1>Hello there!</h1>', :body => "In case you can't read html, Hello there.")
# Pony.mail(:to => 'you@example.com', :cc => 'him@example.com', :from => 'me@example.com', :subject => 'hi', :body => 'Howsit!')
def self.mail(options)
if @@append_inputs
options[:body] = "#{options[:body]}/n #{options.to_s}"
end

options = @@options.merge options
options = options.merge @@override_options

if @@subject_prefix
options[:subject] = "#{@@subject_prefix}#{options[:subject]}"
end

fail ArgumentError, ':to is required' unless options[:to]

options[:via] = default_delivery_method unless options.key?(:via)
Expand Down
40 changes: 39 additions & 1 deletion spec/pony_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@

Pony.override_options = { :from => 'reply@pony.com' }
Pony.mail(:to => 'foo@bar', :from => 'other_address@pony.com')

end

it "should return the orride options" do
Expand All @@ -262,6 +261,45 @@
end
end

describe "subject prefix" do
after(:all) do
Pony.subject_prefix(false)
end

it "should prefix email subject line with the given text" do
expect(Pony).to receive(:build_mail).with(hash_including(:subject => 'First: Second'))

Pony.subject_prefix('First: ')
Pony.mail(:to => 'foo@bar', :subject => 'Second')
end

it "should set the prefix as the subject if no subject is given" do
expect(Pony).to receive(:build_mail).with(hash_including(:subject => 'First: '))

Pony.subject_prefix('First: ')
Pony.mail(:to => 'foo@bar')
end
end

describe "append_inputs" do
it "appends the options passed into Pany.mail to the body" do
expect(Pony).to receive(:build_mail).with(hash_including(:body => "body/n {:to=>\"foo@bar\", :body=>\"body\"}"))

Pony.append_inputs

Pony.mail(:to => 'foo@bar', :body => 'body')
end

it "sets the options passed into Pany.mail as the body if one is not present" do
expect(Pony).to receive(:build_mail).with(hash_including(:body => "/n {:to=>\"foo@bar\"}"))

Pony.append_inputs

Pony.mail(:to => 'foo@bar')
end

end

describe "content type" do
context "mail with attachments, html_body and body " do
subject(:mail) do
Expand Down

0 comments on commit dbd486d

Please sign in to comment.