padrino-mailer 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
data/.document CHANGED
@@ -1,5 +1,5 @@
1
- README.rdoc
2
1
  lib/**/*.rb
3
2
  bin/*
4
- features/**/*.feature
5
- LICENSE
3
+ -
4
+ README.rdoc
5
+ LICENSE.txt
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --title 'Padrino Mailer Documentation' --protected
File without changes
data/README.rdoc CHANGED
@@ -91,7 +91,7 @@ is invoking the render function passing the <tt>name</tt> attribute to the body
91
91
 
92
92
  Once the mailer definition has been completed and the template has been defined, the email can be sent using:
93
93
 
94
- deliver(:sample, :registration_email, "Bob", "21")
94
+ deliver(:sample, :registration, "Bob", "21")
95
95
 
96
96
  And that will then deliver the email according the the configured options. This is all you need to send basic emails.
97
97
 
@@ -5,28 +5,58 @@ module Padrino
5
5
  #
6
6
  # You can set the default delivery settings from your app through:
7
7
  #
8
- # set :delivery_method, :smtp => {
9
- # :address => 'smtp.yourserver.com',
10
- # :port => '25',
11
- # :user_name => 'user',
12
- # :password => 'pass',
13
- # :authentication => :plain # :plain, :login, :cram_md5, no auth by default
14
- # :domain => "localhost.localdomain" # the HELO domain provided by the client to the server
15
- # }
8
+ # set :delivery_method, :smtp => {
9
+ # :address => 'smtp.yourserver.com',
10
+ # :port => '25',
11
+ # :user_name => 'user',
12
+ # :password => 'pass',
13
+ # :authentication => :plain
14
+ # }
16
15
  #
17
16
  # or sendmail:
18
17
  #
19
- # set :delivery_method, :sendmail
18
+ # set :delivery_method, :sendmail
20
19
  #
21
20
  # or for tests:
22
21
  #
23
- # set :delivery_method, :test
22
+ # set :delivery_method, :test
24
23
  #
25
- # and then all delivered mail will use these settings unless otherwise specified.
24
+ # and all delivered mail will use these settings unless otherwise specified.
25
+ #
26
+ # Define a mailer in your application:
27
+ #
28
+ # # app/mailers/sample_mailer.rb
29
+ # MyAppName.mailers :sample do
30
+ # defaults :content_type => 'html'
31
+ # email :registration do |name, age|
32
+ # to 'user@domain.com'
33
+ # from 'admin@site.com'
34
+ # subject 'Welcome to the site!'
35
+ # locals :name => name
36
+ # render 'registration'
37
+ # end
38
+ # end
39
+ #
40
+ # Use the mailer to deliver messages:
41
+ #
42
+ # deliver(:sample, :registration, "Bob", "21")
43
+ #
44
+ # For a more detailed guide, please read the {Padrino Mailer}[http://www.padrinorb.com/guides/padrino-mailer] guide.
26
45
  #
27
46
  class Base
28
47
  attr_accessor :delivery_settings, :app, :mailer_name, :messages
29
48
 
49
+ # Constructs a +Mailer+ base object with specified options.
50
+ #
51
+ # @param [Sinatra::Application] app
52
+ # The application tied to this mailer.
53
+ # @param [Symbol] name
54
+ # The name of this mailer.
55
+ # @param [Proc] block
56
+ # The +email+ definitions block.
57
+ #
58
+ # @see Padrino::Mailer::Helpers::ClassMethods#mailer
59
+ # @api private
30
60
  def initialize(app, name, &block) # @private
31
61
  @mailer_name = name
32
62
  @messages = {}
@@ -38,8 +68,12 @@ module Padrino
38
68
  ##
39
69
  # Defines a mailer object allowing the definition of various email messages that can be delivered
40
70
  #
41
- # ==== Examples
71
+ # @param [Symbol] name
72
+ # The name of this email message.
73
+ # @param [Proc] block
74
+ # The message definition (i.e subject, to, from, locals).
42
75
  #
76
+ # @example
43
77
  # email :birthday do |name, age|
44
78
  # subject "Happy Birthday!"
45
79
  # to 'john@fake.com'
@@ -48,6 +82,7 @@ module Padrino
48
82
  # render 'birthday'
49
83
  # end
50
84
  #
85
+ # @api public
51
86
  def email(name, &block)
52
87
  raise "The email '#{name}' is already defined" if self.messages[name].present?
53
88
  self.messages[name] = Proc.new { |*attrs|
@@ -62,13 +97,16 @@ module Padrino
62
97
 
63
98
  # Defines the default attributes for a message in this mailer (including app-wide defaults)
64
99
  #
65
- # ==== Examples
100
+ # @param [Hash] attributes
101
+ # The hash of message options to use as default.
66
102
  #
103
+ # @example
67
104
  # mailer :alternate do
68
- # defaults :from => 'padrino@from.com', :to => 'padrino@to.com'
69
- # email(:foo) do ... end
105
+ # defaults :from => 'padrino@from.com', :to => 'padrino@to.com'
106
+ # email(:foo) do; end
70
107
  # end
71
108
  #
109
+ # @api public
72
110
  def defaults(attributes=nil)
73
111
  if attributes.nil? # Retrieve the default values
74
112
  @app.respond_to?(:mailer_defaults) ? @app.mailer_defaults.merge(@defaults) : @defaults
@@ -16,7 +16,6 @@ module Mail # @private
16
16
  settings.views = File.expand_path("./mailers")
17
17
  settings.reload_templates = true
18
18
  end
19
-
20
19
  # Run the original initialize
21
20
  initialize_without_app(*args, &block)
22
21
  end
@@ -25,8 +24,7 @@ module Mail # @private
25
24
  ##
26
25
  # Setup like in Sinatra/Padrino apps content_type and template lookup.
27
26
  #
28
- # ==== Examples
29
- #
27
+ # @example
30
28
  # # This add a email plain part if a template called bar.plain.* is found
31
29
  # # and a html part if a template called bar.html.* is found
32
30
  # email do
@@ -50,8 +48,7 @@ module Mail # @private
50
48
  # html_part are both defined in a message, then it will be a multipart/alternative
51
49
  # message and set itself that way.
52
50
  #
53
- # ==== Examples
54
- #
51
+ # @example
55
52
  # text_part "Some text"
56
53
  # text_part { render('multipart/basic.text') }
57
54
  #
@@ -69,8 +66,7 @@ module Mail # @private
69
66
  # text_part are both defined in a message, then it will be a multipart/alternative
70
67
  # message and set itself that way.
71
68
  #
72
- # ==== Examples
73
- #
69
+ # @example
74
70
  # html_part "Some <b>Html</b> text"
75
71
  # html_part { render('multipart/basic.html') }
76
72
  #
@@ -86,8 +82,7 @@ module Mail # @private
86
82
  ##
87
83
  # Allows you to add a part in block form to an existing mail message object
88
84
  #
89
- # ==== Examples
90
- #
85
+ # @example
91
86
  # mail = Mail.new do
92
87
  # part :content_type => "multipart/alternative", :content_disposition => "inline" do |p|
93
88
  # p.part :content_type => "text/plain", :body => "test text\nline #2"
@@ -118,7 +113,6 @@ module Mail # @private
118
113
  def settings
119
114
  self.class
120
115
  end
121
- alias :options :settings
122
116
 
123
117
  ##
124
118
  # Sets the message defined template path to the given view path
@@ -191,6 +185,17 @@ module Mail # @private
191
185
  @_defaults.each_pair { |k, v| default(k.to_sym, v) } if @_defaults.is_a?(Hash)
192
186
  end
193
187
 
188
+ ##
189
+ # Check if we can log
190
+ #
191
+ def self.logging?
192
+ @_logging
193
+ end
194
+
195
+ def self.logging=(value)
196
+ @_logging = value
197
+ end
198
+
194
199
  # Shortcut for delivery_method with smarter smtp overwrites
195
200
  def via(method = nil, settings = {})
196
201
  if method.nil?
@@ -6,30 +6,44 @@ module Padrino
6
6
  end
7
7
 
8
8
  ##
9
- # Delivers an email with the given mail attributes (to, from, subject, cc, bcc, body, et.al)
9
+ # Delivers an email with the given mail attributes.
10
10
  #
11
- # ==== Examples
11
+ # @param [Hash] mail_attributes
12
+ # The attributes for this message (to, from, subject, cc, bcc, body, etc)
13
+ # @param [Proc] block
14
+ # The block mail attributes for this message.
12
15
  #
16
+ # @example
13
17
  # email do
14
18
  # to @user.email
15
- # from "awesomeness@example.com",
19
+ # from "awesomeness@example.com"
16
20
  # subject "Welcome to Awesomeness!"
17
21
  # locals :a => a, :b => b
18
22
  # render 'path/to/my/template'
19
23
  # end
20
24
  #
25
+ # @see ClassMethods#email
26
+ # @api public
21
27
  def email(mail_attributes={}, &block)
22
28
  settings.email(mail_attributes, &block)
23
29
  end
24
30
 
25
31
  ##
26
- # Delivers a mailer message email with the given attributes
32
+ # Delivers a mailer message email with the given attributes.
27
33
  #
28
- # ==== Examples
34
+ # @param [Symbol] mailer_name
35
+ # The name of the mailer.
36
+ # @param [Symbol] message_name
37
+ # The name of the message to deliver.
38
+ # @param attributes
39
+ # The parameters to pass to the mailer.
29
40
  #
41
+ # @example
30
42
  # deliver(:sample, :birthday, "Joey", 21)
31
43
  # deliver(:example, :message, "John")
32
44
  #
45
+ # @see ClassMethods#deliver
46
+ # @api public
33
47
  def deliver(mailer_name, message_name, *attributes)
34
48
  settings.deliver(mailer_name, message_name, *attributes)
35
49
  end
@@ -42,16 +56,18 @@ module Padrino
42
56
 
43
57
  ##
44
58
  # Returns all registered mailers for this application
45
- #
59
+ # @private
46
60
  def registered_mailers
47
61
  @_registered_mailers ||= {}
48
62
  end
49
63
 
50
64
  ##
51
- # Defines a mailer object allowing the definition of various email messages that can be delivered
65
+ # Defines a mailer object allowing the definition of various email messages that can be delivered.
52
66
  #
53
- # ==== Examples
67
+ # @param [Symbol] name
68
+ # The name of the mailer to initialize.
54
69
  #
70
+ # @example
55
71
  # mailer :sample do
56
72
  # email :birthday do |name, age|
57
73
  # subject 'Happy Birthday!'
@@ -62,6 +78,7 @@ module Padrino
62
78
  # end
63
79
  # end
64
80
  #
81
+ # @api public
65
82
  def mailer(name, &block)
66
83
  mailer = Padrino::Mailer::Base.new(self, name, &block)
67
84
  mailer.delivery_settings = delivery_settings
@@ -73,11 +90,18 @@ module Padrino
73
90
  ##
74
91
  # Delivers a mailer message email with the given attributes
75
92
  #
76
- # ==== Examples
93
+ # @param [Symbol] mailer_name
94
+ # The name of the mailer.
95
+ # @param [Symbol] message_name
96
+ # The name of the message to deliver.
97
+ # @param attributes
98
+ # The parameters to pass to the mailer.
77
99
  #
100
+ # @example
78
101
  # deliver(:sample, :birthday, "Joey", 21)
79
102
  # deliver(:example, :message, "John")
80
103
  #
104
+ # @api public
81
105
  def deliver(mailer_name, message_name, *attributes)
82
106
  message = registered_mailers[mailer_name].messages[message_name].call(*attributes)
83
107
  message.delivery_method(*delivery_settings)
@@ -85,22 +109,26 @@ module Padrino
85
109
  end
86
110
 
87
111
  ##
88
- # Delivers an email with the given mail attributes (to, from, subject, cc, bcc, body, et.al) using settings of
89
- # the given app.
112
+ # Delivers an email with the given mail attributes with specified and default settings.
90
113
  #
91
- # ==== Examples
114
+ # @param [Hash] mail_attributes
115
+ # The attributes for this message (to, from, subject, cc, bcc, body, etc)
116
+ # @param [Proc] block
117
+ # The block mail attributes for this message.
92
118
  #
119
+ # @example
93
120
  # MyApp.email(:to => 'to@ma.il', :from => 'from@ma.il', :subject => 'Welcome!', :body => 'Welcome Here!')
94
121
  #
95
122
  # # or if you prefer blocks
96
123
  #
97
124
  # MyApp.email do
98
125
  # to @user.email
99
- # from "awesomeness@example.com",
126
+ # from "awesomeness@example.com"
100
127
  # subject "Welcome to Awesomeness!"
101
128
  # body 'path/to/my/template', :locals => { :a => a, :b => b }
102
129
  # end
103
130
  #
131
+ # @api public
104
132
  def email(mail_attributes={}, &block)
105
133
  message = Mail::Message.new(self)
106
134
  message.delivery_method(*delivery_settings)
@@ -111,10 +139,11 @@ module Padrino
111
139
 
112
140
  private
113
141
  ##
114
- # Return the parsed delivery method
142
+ # Returns the parsed delivery method options
115
143
  #
116
144
  def delivery_settings
117
145
  @_delivery_setting ||= begin
146
+ raise "You must setup :delivery_method, see api for more details" if RUBY_PLATFORM =~ /win32/ && !respond_to?(:delivery_method)
118
147
  return [:sendmail, { :location => `which sendmail`.chomp }] unless respond_to?(:delivery_method)
119
148
  return [delivery_method.keys[0], delivery_method.values[0]] if delivery_method.is_a?(Hash)
120
149
  return [delivery_method, {}] if delivery_method.is_a?(Symbol)
@@ -7,14 +7,18 @@ module Padrino
7
7
  # +mime+ should be the content type like "text/plain"
8
8
  # +fallback+ may be any symbol
9
9
  #
10
- # Also see the documentation for MIME_TYPES
10
+ # Also see the documentation for {MIME_TYPES}.
11
11
  #
12
- # ==== Examples
12
+ # @param [String] mime
13
+ # The mime alias to fetch (i.e 'text/plain').
14
+ # @param [Symbol] fallback
15
+ # The fallback mime to use if +mime+ doesn't exist.
13
16
  #
14
- # => :plain
17
+ # @example
15
18
  # Padrino::Mailer::Mime.mime_type('text/plain')
16
- # => :html
19
+ # # => :plain
17
20
  # Padrino::Mailer::Mime.mime_type('text/html')
21
+ # # => :html
18
22
  #
19
23
  # This is a shortcut for:
20
24
  #
@@ -24,9 +28,8 @@ module Padrino
24
28
  MIME_TYPES.fetch(mime.to_s.downcase, fallback)
25
29
  end
26
30
 
27
- # List of most common mime-types, selected various sources
28
- # according to their usefulness in a emailg scope for Ruby
29
- # users.
31
+ # List of common mime-types, selected from various sources
32
+ # according to their usefulness for an email scope.
30
33
  #
31
34
  # You can add your own mime types like:
32
35
  #
@@ -1,4 +1,4 @@
1
- # require tilt if available; fall back on bundled version.
1
+ # requires tilt if available; falls back on bundled version.
2
2
  begin
3
3
  require 'tilt'
4
4
  rescue LoadError
@@ -12,19 +12,38 @@ FileSet.glob_require('padrino-mailer/**/*.rb', __FILE__)
12
12
 
13
13
  module Padrino
14
14
  ##
15
- # This component uses the 'mail' library to create a powerful but simple mailer system within Padrino (and Sinatra).
16
- # There is full support for using plain or html content types as well as for attaching files.
17
- # The MailerPlugin has many similarities to ActionMailer but is much lighterweight and (arguably) easier to use.
15
+ # This component uses the +mail+ library to create a powerful but simple mailer within Padrino (and Sinatra).
16
+ # There is full support for using plain or html content-types as well as for file attachments.
17
+ #
18
+ # Using the mailer in Padrino has two forms. The 'quick' method requires only use
19
+ # of the +email+ method directly in the controller:
20
+ #
21
+ # # app/controllers/session.rb
22
+ # post :create do
23
+ # email do
24
+ # from "tony@reyes.com"
25
+ # to "john@smith.com"
26
+ # subject "Welcome!"
27
+ # body render('email/registered')
28
+ # end
29
+ # end
30
+ #
31
+ # For a more detailed guide, please read the {Padrino Mailer}[http://www.padrinorb.com/guides/padrino-mailer] guide.
18
32
  #
19
33
  module Mailer
20
- ##
21
- # Register
22
- #
23
- # Padrino::Mailer::Helpers
24
- #
25
- # for Padrino::Application
26
- #
27
34
  class << self
35
+ ##
36
+ # Registers the Padrino::Mailer helpers with the application.
37
+ #
38
+ # @param [Sinatra::Application] app The application that needs mailers.
39
+ #
40
+ # @example
41
+ # require 'padrino-mailer'
42
+ # class Demo < Padrino::Application
43
+ # register Padrino::Mailer::Helpers
44
+ # end
45
+ #
46
+ # @api public
28
47
  def registered(app)
29
48
  app.helpers Padrino::Mailer::Helpers
30
49
  end
data/test/helper.rb CHANGED
@@ -2,15 +2,13 @@ ENV['PADRINO_ENV'] = 'test'
2
2
  PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
3
3
 
4
4
  require File.expand_path('../../../load_paths', __FILE__)
5
- require 'test/unit'
6
- require 'shoulda'
7
- require 'mocha'
5
+ require File.join(File.dirname(__FILE__), '..', '..', 'padrino-core', 'test', 'mini_shoulda')
8
6
  require 'rack/test'
9
7
  require 'padrino-core'
10
8
  require 'padrino-helpers'
11
9
  require 'padrino-mailer'
12
10
 
13
- class Test::Unit::TestCase
11
+ class MiniTest::Spec
14
12
  include Rack::Test::Methods
15
13
 
16
14
  # Sets up a Sinatra::Base subclass defined with the block
@@ -24,16 +22,6 @@ class Test::Unit::TestCase
24
22
  Rack::Lint.new(@app)
25
23
  end
26
24
 
27
- # Silences the output by redirecting to stringIO
28
- # silence_logger { ...commands... } => "...output..."
29
- def silence_logger(&block)
30
- orig_stdout = $stdout
31
- $stdout = log_buffer = StringIO.new
32
- block.call
33
- $stdout = orig_stdout
34
- log_buffer.rewind && log_buffer.read
35
- end
36
-
37
25
  def pop_last_delivery
38
26
  Mail::TestMailer.deliveries.pop
39
27
  end
data/test/test_email.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
- class TestEmail < Test::Unit::TestCase
3
+ describe "Email" do
4
4
 
5
5
  context 'the mailer in a app' do
6
6
 
@@ -147,7 +147,7 @@ class TestEmail < Test::Unit::TestCase
147
147
  end
148
148
 
149
149
  should 'raise an error if there are two messages with the same name' do
150
- assert_raise RuntimeError do
150
+ assert_raises RuntimeError do
151
151
  mock_app do
152
152
  register Padrino::Mailer
153
153
  mailer :foo do
data/test/test_message.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
- class TestMessage < Test::Unit::TestCase
3
+ describe "Message" do
4
4
 
5
5
  context 'the message' do
6
6
  should "accept headers and body" do
@@ -18,7 +18,7 @@ class TestMessage < Test::Unit::TestCase
18
18
  end
19
19
 
20
20
  should "raise an error if template was not found" do
21
- assert_raise Padrino::Rendering::TemplateNotFound do
21
+ assert_raises Padrino::Rendering::TemplateNotFound do
22
22
  Mail::Message.new do
23
23
  from 'padrino@me.com'
24
24
  to 'padrino@you.com'
@@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
  require File.expand_path(File.dirname(__FILE__) + '/fixtures/sinatra_app/app')
3
3
  require File.expand_path(File.dirname(__FILE__) + '/fixtures/padrino_app/app')
4
4
 
5
- class TestPadrinoMailer < Test::Unit::TestCase
5
+ describe "PadrinoMailer" do
6
6
 
7
7
  context 'for mail delivery in sample sinatra application' do
8
8
  setup { @app = SinatraApp }
@@ -82,7 +82,7 @@ class TestPadrinoMailer < Test::Unit::TestCase
82
82
  assert_email_sent(:to => 'john@apple.com', :from => 'joe@smith.com',
83
83
  :subject => 'Test Email', :body => 'Test Body', :delivery_method => @app.delivery_method)
84
84
  end
85
-
85
+
86
86
  should_eventually 'be able to deliver a basic email using Padrino::Helpers' do
87
87
  post '/deliver/helper'
88
88
  assert_equal 'mail delivered', body
@@ -90,6 +90,6 @@ class TestPadrinoMailer < Test::Unit::TestCase
90
90
  :content_type => 'text/html', :delivery_method => @app.delivery_method,
91
91
  :subject => "Welcome Helper!", :body => "<a href=\"#\">jim</a>")
92
92
  end
93
-
93
+
94
94
  end
95
95
  end
data/test/test_part.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
- class TestPart < Test::Unit::TestCase
3
+ describe "Part" do
4
4
 
5
5
  context "the part" do
6
6
  should "use correctly parts" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-mailer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 10
9
- - 2
10
- version: 0.10.2
9
+ - 3
10
+ version: 0.10.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Padrino Team
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-08-31 00:00:00 -07:00
21
+ date: 2011-10-03 00:00:00 -07:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
@@ -29,12 +29,12 @@ dependencies:
29
29
  requirements:
30
30
  - - "="
31
31
  - !ruby/object:Gem::Version
32
- hash: 51
32
+ hash: 49
33
33
  segments:
34
34
  - 0
35
35
  - 10
36
- - 2
37
- version: 0.10.2
36
+ - 3
37
+ version: 0.10.3
38
38
  type: :runtime
39
39
  version_requirements: *id001
40
40
  - !ruby/object:Gem::Dependency
@@ -64,7 +64,8 @@ extra_rdoc_files:
64
64
  files:
65
65
  - .document
66
66
  - .gitignore
67
- - LICENSE
67
+ - .yardopts
68
+ - LICENSE.txt
68
69
  - README.rdoc
69
70
  - Rakefile
70
71
  - lib/padrino-mailer.rb