diff --git a/lib/lotus/mailer/rendering.rb b/lib/lotus/mailer/rendering.rb index 0f2e8ea..3f2805f 100644 --- a/lib/lotus/mailer/rendering.rb +++ b/lib/lotus/mailer/rendering.rb @@ -11,15 +11,19 @@ def self.extended(base) end module InstanceMethods + # @private + # @since 0.1.0 + attr_accessor :mail + # Initialize a mailer # # @param locals [Hash] a set of objects available during the rendering process. # # @since 0.1.0 def initialize(locals = {}) - @locals = locals - @scope = self - @mail = Mail.new + @locals = locals + @scope = self + @mail = Mail.new end # Render a single template with the specified format. @@ -33,8 +37,6 @@ def render(format) self.class.templates self.class.templates[format].render @scope, @locals end - - attr_accessor :mail end end end diff --git a/test/fixtures.rb b/test/fixtures.rb index 0210b7f..ccb43c9 100644 --- a/test/fixtures.rb +++ b/test/fixtures.rb @@ -49,23 +49,17 @@ class ArrayMailer class WelcomeMailer include Lotus::Mailer - + from "noreply@sender.com" to "noreply@recipient.com" subject "Welcome" - + def greeting "Ahoy" end end -class User - def initialize(name) - @name = name - end - - attr_reader :name -end +class User < Struct.new(:name); end class SubscriptionMailer include Lotus::Mailer diff --git a/test/fixtures/templates/welcome_mailer.html.erb b/test/fixtures/templates/welcome_mailer.html.erb index 34895e1..cede28f 100644 --- a/test/fixtures/templates/welcome_mailer.html.erb +++ b/test/fixtures/templates/welcome_mailer.html.erb @@ -1,9 +1,8 @@ -

Hello World!

- <%=greeting%> + <%= greeting %>

This is a html template

diff --git a/test/fixtures/templates/welcome_mailer.txt.erb b/test/fixtures/templates/welcome_mailer.txt.erb index 9362e81..51015e8 100644 --- a/test/fixtures/templates/welcome_mailer.txt.erb +++ b/test/fixtures/templates/welcome_mailer.txt.erb @@ -1,2 +1,2 @@ This is a txt template -<%=greeting%> +<%= greeting %> diff --git a/test/rendering_test.rb b/test/rendering_test.rb index fd9ae30..511b7cd 100644 --- a/test/rendering_test.rb +++ b/test/rendering_test.rb @@ -1,25 +1,39 @@ require 'test_helper' -require 'lotus/mailer' describe Lotus::Mailer do - describe 'render' do - it 'renders a single template with a given format' do - InvoiceMailer.new.render(:html).must_include %(

Invoice template

) - LazyMailer.new.render(:html).must_include %(Hello World) - LazyMailer.new.render(:haml).must_include %(This is a haml template) - LazyMailer.new.render(:txt).must_include %(This is a txt template) + describe '#render' do + describe 'when template is explicitly declared' do + let(:mailer) { InvoiceMailer.new } + + it 'renders the given template' do + mailer.render(:html).must_include %(

Invoice template

) + end end - it 'renders a single template with context' do - WelcomeMailer.new.render(:html).must_include %(Ahoy) - WelcomeMailer.new.render(:txt).must_include %(Ahoy) + describe 'when template is implicitly declared' do + let(:mailer) { LazyMailer.new } + + it 'looks for template with same name with inflected classname and render it' do + mailer.render(:html).must_include %(Hello World) + mailer.render(:haml).must_include %(This is a haml template) + mailer.render(:txt).must_include %(This is a txt template) + end + end + + describe 'when mailer defines context' do + let(:mailer) { WelcomeMailer.new } + + it 'renders template with defined context' do + mailer.render(:txt).must_include %(Ahoy) + end end - it 'renders a single template with locals' do - luca = User.new('Luca') - mailer = RenderMailer.new(user: luca) + describe 'when locals are parsed in' do + let(:mailer) { RenderMailer.new(user: User.new('Luca')) } - mailer.render(:html).must_include %(Luca) + it 'renders template with parsed locals' do + mailer.render(:html).must_include %(Luca) + end end end end