Skip to content

Commit

Permalink
allow clipped render calls in mailer, closes #1906
Browse files Browse the repository at this point in the history
  • Loading branch information
ujifgc committed Jun 5, 2016
1 parent 11060b6 commit cc6fe63
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
== Edge

- NEW Remove RightJS, Riot, Steak support (@ujifgc)
- NEW Allow shortened mailer render calls with default mailer and message names (@ujifgc)

== 0.13.2 (May 9th 2016)

Expand Down
2 changes: 2 additions & 0 deletions padrino-mailer/lib/padrino-mailer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def email(name, &block)
raise "The email '#{name}' is already defined" if self.messages[name].present?
self.messages[name] = Proc.new { |*attrs|
message = app.settings._padrino_mailer::Message.new(self.app)
message.mailer_name = mailer_name
message.message_name = name
message.defaults = self.defaults if self.defaults.any?
message.delivery_method(*delivery_settings)
message.instance_exec(*attrs, &block)
Expand Down
10 changes: 9 additions & 1 deletion padrino-mailer/lib/padrino-mailer/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Message # @private
include Padrino::Rendering if defined?(Padrino::Rendering)
include Padrino::Helpers::RenderHelpers if defined? Padrino::Helpers::RenderHelpers
attr_reader :template_cache
attr_accessor :mailer_name, :message_name

def initialize_with_app(*args, &block)
@template_cache = Tilt::Cache.new
Expand Down Expand Up @@ -253,10 +254,17 @@ def content_type_with_symbol(value=nil)
##
# Defines the render for the mailer utilizing the padrino 'rendering' module
#
def render(engine, data=nil, options={}, locals={}, &block)
def render(engine=nil, data=nil, options={}, locals={}, &block)
locals = @_locals if options[:locals].blank? && locals.blank?
@template_cache.clear if settings.reload_templates?

engine ||= message_name

if mailer_name
settings.views += "/#{mailer_name}"
engine = engine.to_s.sub(%r{^#{mailer_name}/}, '')
end

provides.each do |format|
part do |p|
p.content_type(format)
Expand Down
14 changes: 14 additions & 0 deletions padrino-mailer/test/fixtures/padrino_app/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class PadrinoApp < Padrino::Application
via :test
render 'default_mailer_name'
end

message :default_mailer_email_name do |name|
subject "Welcome Helper!"
to 'jim@fake.com'
from 'noreply@custom.com'
locals :name => name
via :test
render
end
end

mailer :nonexistant do
Expand Down Expand Up @@ -95,6 +104,11 @@ class PadrinoApp < Padrino::Application
result = deliver(:sample, :default_mailer_name, "Jim")
result ? "mail delivered" : 'mail not delivered'
end

post "/deliver/default_mailer_email_name" do
result = deliver(:sample, :default_mailer_email_name, "Jim")
result ? "mail delivered" : 'mail not delivered'
end
end

Padrino.mount("PadrinoApp").to("/")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dmen
8 changes: 7 additions & 1 deletion padrino-mailer/test/test_padrino_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,17 @@
end

it 'should be able to render default mailer names' do
skip
post '/deliver/default_mailer_name'
assert_equal 'mail delivered', body
assert_email_sent(:to => 'jim@fake.com', :from => 'noreply@custom.com',
:content_type => 'text/plain', :body => "dmn")
end

it 'should be able to render default mailer email names' do
post '/deliver/default_mailer_email_name'
assert_equal 'mail delivered', body
assert_email_sent(:to => 'jim@fake.com', :from => 'noreply@custom.com',
:content_type => 'text/plain', :body => "dmen")
end
end
end

0 comments on commit cc6fe63

Please sign in to comment.