Skip to content

Commit

Permalink
Merge branch 'master' into 0.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 1, 2016
2 parents 3565110 + 50441a5 commit 98faa9f
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ language: ruby
sudo: false
cache: bundler
script: 'bundle exec rake test:coverage --trace'
before_install:
- gem install bundler
- gem update bundler
rvm:
- 2.2.4
- 2.3.0
- 2.2.5
- 2.3.1
- ruby-head
- jruby-9.0.5.0
- jruby-head
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Mail for Ruby applications

## v0.3.0 - (unreleased)
### Added
[Anton Dabydov] Blind carbon copy (bcc) option
[Anton Dabydov] Carbon copy (cc) option

### Changed
[Luca Guidi] Drop support for Ruby 2.0 and 2.1

Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ A mailer with `.to` and `.from` addresses and mailer delivery:
```ruby
require 'hanami/mailer'

Hanami::Mailer.configuration do
Hanami::Mailer.configure do
delivery_method :smtp,
address: "smtp.gmail.com",
port: 587,
Expand All @@ -80,8 +80,11 @@ end.load!
class WelcomeMailer
include Hanami::Mailer

from 'noreply@sender.com'
to 'noreply@recipient.com'
from 'noreply@sender.com'
to 'noreply@recipient.com'
cc 'cc@sender.com'
bcc 'alice@example.com'

subject 'Welcome'
end

Expand All @@ -90,15 +93,13 @@ WelcomeMailer.deliver

### Locals

The set of objects passed in the `deliver` call are called `locals` and are avaliable inside the mailer and the template.
The set of objects passed in the `deliver` call are called `locals` and are available inside the mailer and the template.

```ruby
require 'hanami/mailer'

User = Struct.new(:name, :username)
luca = User.new('Luca', 'jodosha')

Hanami::Mailer.load!
User = Struct.new(:name, :username, :email)
luca = User.new('Luca', 'jodosha', 'luca@jodosha.com')

class WelcomeMailer
include Hanami::Mailer
Expand All @@ -110,17 +111,17 @@ class WelcomeMailer
private

def recipient
luca.email
user.email
end
end

InvoiceMailer.deliver(user: luca)
WelcomeMailer.deliver(user: luca)
```

The corresponding `erb` file:

```erb
Hello <%= luca.name %>!
Hello <%= user.name %>!
```

### Scope
Expand Down
4 changes: 3 additions & 1 deletion lib/hanami/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module ClassMethods
# <tt>Signup::Welcome.deliver(format: :txt)</tt>
#
# All the given locals, excepted the reserved ones (<tt>:format</tt> and
# <tt>charset</tt>), are avaliable as rendering context for the templates.
# <tt>charset</tt>), are available as rendering context for the templates.
#
# @param locals [Hash] a set of objects that acts as context for the rendering
# @option :format [Symbol] specify format to deliver
Expand Down Expand Up @@ -201,6 +201,8 @@ def initialize(locals = {})
@mail = Mail.new.tap do |m|
m.from = __dsl(:from)
m.to = __dsl(:to)
m.cc = __dsl(:cc)
m.bcc = __dsl(:bcc)
m.subject = __dsl(:subject)

m.charset = charset
Expand Down
180 changes: 180 additions & 0 deletions lib/hanami/mailer/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def self.extended(base)
base.class_eval do
@from = nil
@to = nil
@cc = nil
@bcc = nil
@subject = nil
end
end
Expand Down Expand Up @@ -139,6 +141,184 @@ def from(value = nil)
end
end

# Sets the cc (carbon copy) for mail messages
#
# It accepts a hardcoded value as a string or array of strings.
# For dynamic values, you can specify a symbol that represents an instance
# method.
#
# This value is optional.
#
# When a value is given, it specifies the cc for the email.
# When a value is not given, it returns the cc of the email.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding class variable. When
# called without, it will return the already set value, or the default.
#
# @overload cc(value)
# Sets the cc
# @param value [String, Array, Symbol] the hardcoded value or method name
# @return [NilClass]
#
# @overload cc
# Returns the cc
# @return [String, Array, Symbol] the recipient
#
# @since x.x.x
#
# @example Hardcoded value (String)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
#
# to "user@example.com"
# cc "other.user@example.com"
# end
#
# @example Hardcoded value (Array)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
#
# to ["user-1@example.com", "user-2@example.com"]
# cc ["other.user-1@example.com", "other.user-2@example.com"]
# end
#
# @example Method (Symbol)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
# to "user@example.com"
# cc :email_address
#
# private
#
# def email_address
# user.email
# end
# end
#
# other_user = User.new(name: 'L')
# WelcomeMailer.deliver(user: other_user)
#
# @example Method that returns a collection of recipients
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
# to "user@example.com"
# cc :recipients
#
# private
#
# def recipients
# users.map(&:email)
# end
# end
#
# other_users = [User.new(name: 'L'), User.new(name: 'MG')]
# WelcomeMailer.deliver(users: other_users)
def cc(value = nil)
if value.nil?
@cc
else
@cc = value
end
end

# Sets the bcc (blind carbon copy) for mail messages
#
# It accepts a hardcoded value as a string or array of strings.
# For dynamic values, you can specify a symbol that represents an instance
# method.
#
# This value is optional.
#
# When a value is given, it specifies the bcc for the email.
# When a value is not given, it returns the bcc of the email.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding class variable. When
# called without, it will return the already set value, or the default.
#
# @overload bcc(value)
# Sets the bcc
# @param value [String, Array, Symbol] the hardcoded value or method name
# @return [NilClass]
#
# @overload bcc
# Returns the bcc
# @return [String, Array, Symbol] the recipient
#
# @since x.x.x
#
# @example Hardcoded value (String)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
#
# to "user@example.com"
# bcc "other.user@example.com"
# end
#
# @example Hardcoded value (Array)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
#
# to ["user-1@example.com", "user-2@example.com"]
# bcc ["other.user-1@example.com", "other.user-2@example.com"]
# end
#
# @example Method (Symbol)
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
# to "user@example.com"
# bcc :email_address
#
# private
#
# def email_address
# user.email
# end
# end
#
# other_user = User.new(name: 'L')
# WelcomeMailer.deliver(user: other_user)
#
# @example Method that returns a collection of recipients
# require 'hanami/mailer'
#
# class WelcomeMailer
# include Hanami::Mailer
# to "user@example.com"
# bcc :recipients
#
# private
#
# def recipients
# users.map(&:email)
# end
# end
#
# other_users = [User.new(name: 'L'), User.new(name: 'MG')]
# WelcomeMailer.deliver(users: other_users)
def bcc(value = nil)
if value.nil?
@bcc
else
@bcc = value
end
end

# Sets the recipient for mail messages
#
# It accepts a hardcoded value as a string or array of strings.
Expand Down
4 changes: 3 additions & 1 deletion test/delivery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@

it 'sends the correct information' do
@mail.from.must_equal ['noreply@sender.com']
@mail.to.must_equal ['noreply@recipient.com', 'cc@recipient.com']
@mail.to.must_equal ['noreply@recipient.com', 'owner@recipient.com']
@mail.cc.must_equal ["cc@recipient.com"]
@mail.bcc.must_equal ["bcc@recipient.com"]
@mail.subject.must_equal "Welcome"
end

Expand Down
5 changes: 4 additions & 1 deletion test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class WelcomeMailer
include Hanami::Mailer

from "noreply@sender.com"
to ["noreply@recipient.com", "cc@recipient.com"]
to ["noreply@recipient.com", "owner@recipient.com"]
cc "cc@recipient.com"
bcc "bcc@recipient.com"

subject "Welcome"

def greeting
Expand Down

0 comments on commit 98faa9f

Please sign in to comment.