Permalink
Browse files

Add mailer delivery feature

  • Loading branch information...
inescoelho committed Aug 7, 2015
1 parent 2a360be commit 6217918d39752a6a860df37a85d697728f55396a
Showing with 243 additions and 9 deletions.
  1. +12 −2 lib/lotus/mailer.rb
  2. +124 −2 lib/lotus/mailer/dsl.rb
  3. +38 −1 test/fixtures.rb
  4. +4 −4 test/mailer_test.rb
  5. +65 −0 test/to_from_test.rb
View
@@ -128,8 +128,7 @@ def self.dupe
# # it's equivalent to:
#
# module MyApp
# Mailer = Lotus::Mailer.dupe
# Layout = Lotus::Layout.dup
# Mailer = Lotus::Mailer.dupe
#
# module Mailers
# end
@@ -237,6 +236,17 @@ def self.included(base)
conf.copy!(base)
end
def from
raise self.class.instance_variables.inspect
if (self.class.instance_variable_get(:@from)).is_a?(Proc)
instance_exec(&self.class.instance_variable_get(:@from))
else
self.class.instance_variable_get(:@from)
end
end
# Load the framework
#
# @since 0.1.0
View
@@ -76,7 +76,7 @@ def root(value = nil)
# require 'lotus/mailer'
#
# module Frontend
# View = Lotus::Mailer.generate(self)
# Mailer = Lotus::Mailer.generate(self)
#
# class InvoiceMailer
# include Frontend::Mailer
@@ -97,7 +97,7 @@ def root(value = nil)
#
# Frontend::InvoiceMailer.template # => 'standalone_mailer'
# Frontend::Mailers::Invoice.template # => 'standalone'
# Frontend::Views::Sessions::New.template # => 'sessions/new'
# Frontend::Mailers::Sessions::New.template # => 'sessions/new'
def template(format = nil, value = nil)
if value.nil?
if !@templates.has_key?(format)
@@ -135,6 +135,128 @@ def templates(value = nil)
end
end
# When a value is given, specify the sender of the email
# Otherwise, it returns the sender of the email
#
# @param value [Object] String or Proc to be evaluated containing the sender of the email
#
# @return [String] the sender of the email
#
# @since 0.1.0
#
# @example With String
# class StringMailer
# include Lotus::Mailer
#
# from "noreply@example.com"
#
# end
#
# @example With Procs
# class ProcMailer
# include Lotus::Mailer
#
# from = Proc.new { customized_sender }
#
# def customized_sender
# "user_sender@example.com"
# end
# end
def from (value = nil)
if value.nil?
return new.from
else
@from = value
end
end
# When a value is given, specify the email addresses that will be the recipients of the email
# Otherwise, it returns the email addresses that will be the recipients of the email
#
# @param value [Object] String, Array of Strings or Proc to be evaluated containing the email addresses that will be the recipients of the email
#
# @return [String] the email addresses that will be the recipients of the email
#
# @since 0.1.0
#
# @example With String
# class StringMailer
# include Lotus::Mailer
#
# to "noreply@example.com"
#
# end
#
# @example With Array of Strings
# class ArrayMailer
# include Lotus::Mailer
#
# to ["noreply1@example.com", "noreply2@example.com"]
#
# end
#
# @example With Procs
# class ProcMailer
# include Lotus::Mailer
#
# to = Proc.new { customized_receiver }
#
# def customized_receiver
# "user_receiver@example.com"
# end
# end
def to (value = nil)
if value.nil?
return @to
end
if value.is_a?(String)
@to = value
elsif value.is_a?(Array)
@to = value.join(',')
elsif value.is_a?(Proc)
#@to.instance_eval(value)
end
end
# When a value is given, specify the subject of the email
# Otherwise, it returns the subject of the email
#
# @param value [Object] String or Proc to be evaluated containing the subject of the email
#
# @return [String] the subject of the email
#
# @since 0.1.0
#
# @example With String
# class StringMailer
# include Lotus::Mailer
#
# subject "This is the subject"
#
# end
#
# @example With Procs
# class ProcMailer
# include Lotus::Mailer
#
# from = Proc.new { customized_subject }
#
# def customized_subject
# "This is the subject"
# end
# end
def subject (value = nil)
if value.nil?
return @subject
end
if value.is_a?(String)
@subject = value
elsif value.is_a?(Proc)
#@subject.instance_eval(value)
end
end
protected
# Loading mechanism hook.
View
@@ -1,7 +1,6 @@
class InvoiceMailer
include Lotus::Mailer
template :html, 'invoice.html.erb'
#Lotus::Mailer.load!
end
class RenderMailer
@@ -11,3 +10,41 @@ class RenderMailer
class LazyMailer
include Lotus::Mailer
end
class StringMailer
include Lotus::Mailer
from "noreply@example.com"
to "noreply1@example.com"
subject "This is the subject"
end
class ProcMailer
include Lotus::Mailer
from = Proc.new { customized_sender }
to -> { customized_receiver }
subject -> { customized_subject }
def customized_sender
"user_sender@example.com"
end
def customized_receiver
"user_receiver@example.com"
end
def customized_subject
"This is the subject"
end
end
class ArrayMailer
include Lotus::Mailer
from "noreply@example.com"
to ["noreply1@example.com", "noreply2@example.com"]
subject "This is the subject"
end
View
@@ -7,17 +7,17 @@
end
describe '#template' do
describe 'set the correct templates' do
it 'has the template in the hash' do
describe 'set_the_correct_templates' do
it 'has_the_template_in_the_hash' do
templateTest = InvoiceMailer.templates[:html]
templateTest.file.must_equal("#{ InvoiceMailer.root }/invoice.html.erb")
end
end
end
describe '#templates' do
describe 'finds all the templates with the same name' do
it 'has the template in the hash' do
describe 'finds_all_the_templates_with_the_same_name' do
it 'has_the_template_in_the_hash' do
templateTest = LazyMailer.templates[:html]
templateTest.file.must_equal("#{ LazyMailer.root }/lazy_mailer.html.erb")
end
View
@@ -0,0 +1,65 @@
require 'test_helper'
require 'lotus/mailer'
describe Lotus::Mailer do
before do
Lotus::Mailer.reset!
end
describe '#from' do
describe 'returns_from_address' do
it 'returns_the_variable_content' do
end
end
describe 'sets_the_correct_from_address_given_a_string' do
it 'has_the_address_in_the_variable' do
StringMailer.from.must_equal 'noreply@example.com'
end
end
describe 'sets_the_correct_from_address_given_a_proc' do
it 'has_the_address_in_the_variable' do
ProcMailer.from.must_equal 'user_sender@example.com'
end
end
end
describe '#to' do
describe 'returns_recipients' do
it 'returns_the_variable_content' do
end
end
describe 'sets_the_correct_recipients_given_a_string' do
it 'has_the_recipients_in_the_variable' do
StringMailer.to.must_equal 'noreply1@example.com'
end
end
describe 'sets_the_correct_recipients_given_an_array' do
it 'has_the_recipients_in_the_variable' do
ArrayMailer.to.must_equal 'noreply1@example.com,noreply2@example.com'
end
end
describe 'sets_the_correct_recipients_given_a_proc' do
it 'has_the_recipients_in_the_variable' do
ProcMailer.to.must_equal 'user_receiver@example.com'
end
end
end
describe '#subject' do
describe 'returns_subject' do
it 'returns_the_variable_content' do
end
end
describe 'sets_the_correct_subject_given_a_string' do
it 'has_the_subject_in_the_variable' do
StringMailer.subject.must_equal 'This is the subject'
end
end
describe 'sets_the_correct_subject_given_a_proc' do
it 'has_the_subject_in_the_variable' do
ProcMailer.subject.must_equal 'This is the subject'
end
end
end
end

0 comments on commit 6217918

Please sign in to comment.