Skip to content

Commit

Permalink
Delivey Method Implementation (hanami#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
inescoelho committed Aug 24, 2015
1 parent aeea788 commit dcd68c6
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ gem 'tilt'

gem 'simplecov', require: false
gem 'coveralls', require: false
gem 'mail'
4 changes: 3 additions & 1 deletion lib/lotus/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'lotus/mailer/version'
require 'lotus/mailer/configuration'
require 'lotus/mailer/dsl'
require 'lotus/mailer/rendering'
require 'mail'

module Lotus
module Mailer
Expand Down Expand Up @@ -53,6 +55,7 @@ def self.included(base)

base.class_eval do
extend Dsl.dup
extend Rendering.dup

include Utils::ClassAttribute
class_attribute :configuration
Expand Down Expand Up @@ -91,6 +94,5 @@ def self.load!
def self.reset!
configuration.reset!
end

end
end
41 changes: 40 additions & 1 deletion lib/lotus/mailer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ def load!
# Reset the configuration
def reset!
root(DEFAULT_ROOT)

@mailers = Set.new
@modules = []
end
Expand All @@ -183,10 +182,50 @@ def copy!(base)
end
end

# Specify a global delivery method
#
# @param method [Symbol] delivery method
# @param options [Hash] optional settings
#
# @return [Array] an array containing the delivery method and the optional settings as an Hash
#
# @since 0.1.0
#
# @example With Method only
# Lotus::Mailer.configure do
# delivery_method :sendmail
# end
#
# @example With Method and Options
# Lotus::Mailer.configure do
# delivery_method :smtp, address: "localhost", port: 1025
# end
#
# @example Using a Method Alias
# Lotus::Mailer.configure do
# delivery :test
# end
#
# @example With Custom Method
# MyCustomDeliveryMethod = :smtp
#
# Lotus::Mailer.configure do
# delivery_method MyCustomDeliveryMethod, foo: 'bar'
# end
def delivery_method(method = nil, options = {})
if method.nil?
@delivery_method
else
@delivery_method = [method, options]
end
end

alias_method :unload!, :reset!
alias_method :delivery, :delivery_method

protected
attr_writer :root
attr_writer :delivery_method
attr_writer :namespace
attr_writer :modules
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/mailer/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def to(value = nil)
# class ProcMailer
# include Lotus::Mailer
#
# from = Proc.new { customized_subject }
# subject -> { customized_subject }
#
# def customized_subject
# "This is the subject"
Expand Down
20 changes: 20 additions & 0 deletions lib/lotus/mailer/rendering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Lotus
module Mailer
# Render mechanism
#
# @since 0.1.0
module Rendering

# Render a single template with the specified format.
#
# @return [String] the output of the rendering process.
#
# @since 0.1.0
def render(format)
templates
@templates[format].render
end

end
end
end
4 changes: 2 additions & 2 deletions lib/lotus/mailer/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def initialize(template)
# @since 0.1.0
#
# @see Lotus::Mailer::Scope
def render(scope, &blk)
@_template.render(scope, {}, &blk)
def render
@_template.render
end

# Get the path to the template
Expand Down
41 changes: 14 additions & 27 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,56 +113,30 @@ class PrepareMailer
describe '#duplicate' do
before do
@configuration.root 'test'
# @configuration.load_paths << '..'
# @configuration.layout :application
@configuration.add_mailer( InvoiceMailer )
# @configuration.add_layout(ApplicationLayout)
@configuration.prepare { include Kernel }

@config = @configuration.duplicate
end

it 'returns a copy of the configuration' do
@config.root.must_equal @configuration.root
# @config.load_paths.must_equal @configuration.load_paths
# @config.layout.must_equal @configuration.layout
@config.modules.must_equal @configuration.modules
@config.mailers.must_be_empty
# @config.layouts.must_be_empty
end

it "doesn't affect the original configuration" do
@config.root '.'
# @config.load_paths << '../..'
# @config.layout :global
@config.add_mailer(RenderMailer)
# @config.add_layout(GlobalLayout)
@config.prepare { include Comparable }

@config.root.must_equal Pathname.new('.').realpath

# @config.load_paths.must_include @config.root
# @config.load_paths.must_include '..'
# @config.load_paths.must_include '../..'

# @config.layout.must_equal GlobalLayout
@config.mailers.must_include RenderMailer
# @config.layouts.must_include GlobalLayout
@config.modules.size.must_equal 2

@configuration.root.must_equal Pathname.new('test').realpath

# @configuration.load_paths.must_include @config.root
# @configuration.load_paths.must_include '..'
# @configuration.load_paths.wont_include '../..'

# @configuration.layout.must_equal ApplicationLayout

@configuration.mailers.must_include InvoiceMailer
@configuration.mailers.wont_include RenderMailer

# @configuration.layouts.must_include ApplicationLayout
# @configuration.layouts.wont_include GlobalLayout
end

it 'duplicates namespace' do
Expand Down Expand Up @@ -196,7 +170,7 @@ class PrepareMailer
end

end

describe '#load!' do
before do
@configuration.root 'test'
Expand All @@ -208,4 +182,17 @@ class PrepareMailer
@configuration.root.must_equal root
end
end

describe '#delivery_method' do
before do
MyCustomDeliveryMethod = :smtp
Lotus::Mailer.configure do
delivery_method MyCustomDeliveryMethod, foo: 'bar'
end
end

it 'saves the delivery method in the configuration' do
Lotus::Mailer.configuration.delivery_method.must_equal [:smtp, { foo: 'bar' }]
end
end
end
2 changes: 2 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'mail'

class InvoiceMailer
include Lotus::Mailer
template :html, 'invoice.html.erb'
Expand Down
13 changes: 13 additions & 0 deletions test/rendering_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'test_helper'
require 'lotus/mailer'

describe Lotus::Mailer do
describe 'render' do
it 'renders a single template with a given format' do
InvoiceMailer.render(:html).must_include %(<h1>Invoice template</h1>)
LazyMailer.render(:html).must_include %(Hello World)
LazyMailer.render(:haml).must_include %(This is a haml template)
LazyMailer.render(:txt).must_include %(This is a txt template)
end
end
end

0 comments on commit dcd68c6

Please sign in to comment.