aerogel-mailer 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d2ded2ae8256f5f9459d757c4365d4ec7ac6063
4
- data.tar.gz: 16cd2ad084083d218f9b4e9def41ad5685e63dcd
3
+ metadata.gz: 338b48b0e5fe4f6a72c29fcfe8ae28cc9b5c51a5
4
+ data.tar.gz: f25e3c711d479d5cd373ebcbb5f543b2a7ec6262
5
5
  SHA512:
6
- metadata.gz: a3e154ffcc6f1f6d47e7505b251f24435b9a92b888c7b5224720baba5890d265efcd960398d97f4593a291e68ce85ba22713acd17a4b9b3ab52dd587f53a2eb5
7
- data.tar.gz: 43e997ca8c98234f571ea11763df72f2f4acf2f7c5d965bc1d821969d454124ef89ba22bc40d6342a70aae498b5e4e66234d3cea651af2bc53402a21e40af47b
6
+ metadata.gz: 753dcaeb9a141af26c6ac8c2b452577965bc46231decaa45a133a3ed596bbf42687c185a07613e41b4e8ee353d1f7a626f5344ca7b39a955560e6257a270b4d4
7
+ data.tar.gz: 97926b451efa64b7171325b16ed8c22761b17ac7b677abf186bcda3b49d9805c166fba10194111320c9f48f4e8358d3b51bad2ce37943d11f34574978358c46e
data/README.md CHANGED
@@ -4,22 +4,33 @@ A mail delivery module for aerogel applications.
4
4
 
5
5
  ## Usage
6
6
 
7
-
8
- In your application's config.ru:
9
7
  ```ruby
8
+ # config.ru
10
9
  require 'aerogel/core'
11
- require 'aerogel/mailer' #
10
+ require 'aerogel/mailer'
12
11
 
13
- # define a new mailer named :test
14
- Aerogel.mailer :test do |f, t|
15
- from f
16
- to t
17
- subject 'test'
18
- body 'hello'
19
- end
12
+ run Aerogel::Application.load
13
+ ```
20
14
 
21
- # then send email message using this mailer:
22
- Aerogel.email :test, 'from@domain.org', 'to@another.org'
15
+ ```ruby
16
+ # app/mailers/my-test-mailer.rb
17
+ # define mailer:
18
+ class Aerogel::Application
19
+ mailer :test do |f, t|
20
+ from f
21
+ to t
22
+ subject 'test'
23
+ body 'hello'
24
+ end
25
+ end
26
+ ```
23
27
 
24
- run Aerogel::Application.load
28
+ ```ruby
29
+ # app/routes/my-test-route.rb
30
+ # send mail from route handler:
31
+ class Aerogel::Application
32
+ get "/test-mail" do
33
+ email :test, 'from@domain.org', 'to@another.org'
34
+ end
35
+ end
25
36
  ```
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["alex@kukushk.in"]
11
11
  spec.description = %q{Mail sending for aerogel applications}
12
12
  spec.summary = %q{Mail sending for aerogel applications}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/kukushkin/aerogel-mailer"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
data/app/helpers/email.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Aerogel::Helpers
2
2
 
3
3
  def email( *args )
4
- Aerogel.email( *args )
4
+ Aerogel::Application.email( *args )
5
5
  end
6
6
 
7
7
  end # module Aerogel::Helpers
data/config/mailer.conf CHANGED
@@ -1,3 +1,4 @@
1
1
  mailer {
2
2
  delivery_method :test
3
+ default_from 'noreply'
3
4
  }
@@ -2,6 +2,8 @@ require 'aerogel/core'
2
2
  require 'aerogel/mailer/version'
3
3
  require 'aerogel/mailer/core'
4
4
  require 'aerogel/mailer/definition'
5
+ require 'aerogel/mailer/error'
6
+
5
7
 
6
8
  module Aerogel
7
9
 
@@ -10,12 +12,8 @@ module Aerogel
10
12
 
11
13
  # configure module
12
14
  on_load do |app|
13
- Mailer.configure
15
+ app.register Aerogel::Mailer
14
16
  end
15
17
 
16
- # Extend Aerogel
17
- #
18
- extend Aerogel::Mailer
19
-
20
18
  end
21
19
 
@@ -2,21 +2,17 @@ require 'mail'
2
2
 
3
3
  module Aerogel::Mailer
4
4
 
5
- def self.configure
5
+ # Configures module Aerogel::Mailer
6
+ #
7
+ def self.registered(app)
6
8
  aerogel_delivery_method = Aerogel.config.mailer.delivery_method
7
9
  aerogel_delivery_options = Aerogel.config.mailer.delivery_options.raw || {}
8
10
  Mail.defaults do
9
11
  delivery_method aerogel_delivery_method, aerogel_delivery_options
10
12
  end
11
- end
12
-
13
- def self.register_mailer( mailer )
14
- @mailers ||= {}
15
- @mailers[mailer.name] = mailer
16
- end
17
13
 
18
- def self.mailers
19
- @mailers || {}
14
+ # load mailers
15
+ Aerogel::require_resources( :app, "mailers/**/*.rb" )
20
16
  end
21
17
 
22
18
  # Registers new mailer
@@ -25,24 +21,37 @@ module Aerogel::Mailer
25
21
  Definition.new name, block
26
22
  end
27
23
 
24
+ # Deliver email using mailer specified by +name+
25
+ #
28
26
  def email( name, *args )
29
- unless Aerogel::Mailer.mailers[name]
27
+ mailer = Aerogel::Mailer::Definition.mailers[name.to_sym]
28
+ unless mailer
30
29
  raise ArgumentError.new "Mailer '#{name}' is not defined"
31
30
  end
32
- params = Aerogel::Mailer.mailers[name].eval( *args )
31
+ params = mailer.compile( *args )
33
32
  puts "** sending mail: #{params}"
34
33
  begin
35
- Mail.deliver do
34
+ message = Mail.new do
36
35
  from params[:from]
37
36
  to params[:to]
38
37
  subject params[:subject]
39
- body params[:body]
38
+ text_part do
39
+ content_type 'text/plain; charset=UTF-8'
40
+ body params[:body][:text]
41
+ end if params[:body][:text]
42
+ html_part do
43
+ content_type 'text/html; charset=UTF-8'
44
+ body params[:body][:html]
45
+ end if params[:body][:html]
40
46
  end
41
- rescue => e
42
- raise "Mailer '#{name}' failed to deliver email: #{e}"
47
+ message.charset = "UTF-8"
48
+ message.deliver
49
+ rescue StandardError => e
50
+ raise Aerogel::Mailer::Error.new "Mailer '#{name}' failed to deliver email: #{e}"
43
51
  end
44
- # params,
52
+ true
45
53
  end
46
54
 
47
55
 
56
+
48
57
  end # module Aerogel::Mailer
@@ -1,14 +1,18 @@
1
1
  module Aerogel::Mailer
2
2
  class Definition
3
3
 
4
+ include Aerogel::Render::Scope
5
+
4
6
  attr_accessor :name, :blk, :params
5
7
 
8
+ DEFAULT_LAYOUT = "mailer"
9
+
6
10
  def initialize( name, blk )
7
11
  self.name = name.to_sym
8
12
  self.params = {}
9
13
  self.blk = blk
10
14
 
11
- Aerogel::Mailer.register_mailer( self )
15
+ self.class.register_mailer( self )
12
16
  end
13
17
 
14
18
  def from( str )
@@ -23,18 +27,128 @@ class Definition
23
27
  params[:subject] = str
24
28
  end
25
29
 
26
- def body( str )
27
- params[:body] = str
30
+ # Sets layout name for text/plain and text/html layouts
31
+ # or disables layout for message body templates.
32
+ #
33
+ # Example
34
+ # layout false # disables layout for text and html message templates
35
+ # layout 'mailer-admin' # sets layouts to 'views/layouts/mailer-admin.text.erb'
36
+ # # and 'views/layouts/mailer-admin.html.erb'
37
+ #
38
+ def layout( name )
39
+ params[:layout] = name
40
+ end
41
+
42
+ # Sets message body. Multiple calls to #body are allowed,
43
+ # e.g. for setting plain text part and html part separately.
44
+ #
45
+ # If message body is set via call to #body, existing mailer templates
46
+ # and layout will be ignored.
47
+ #
48
+ # +args+ can be a String, which sets the text/plain message body
49
+ # or a Hash.
50
+ #
51
+ # Example:
52
+ # body "This is a plain text message"
53
+ # body html: "This is a HTML only message"
54
+ # body text: "This is a plain text", html: "and <b>HTML</b> message"
55
+ #
56
+ def body( args )
57
+ params[:body] ||= {}
58
+ if args.is_a? String
59
+ params[:body][:text] = args
60
+ elsif args.is_a? Hash
61
+ params[:body][:html] = args[:html] if args.include? :html
62
+ params[:body][:text] = args[:text] if args.include? :text
63
+ else
64
+ raise ArgumentError.new "Invalid argument #{args.class} to #body"
65
+ end
66
+ end
67
+
68
+ # Sets local variables to be passed to template. Multiple calls to #locals
69
+ # are allowed, variables passed this way will be merged into one set before
70
+ # passing to a template.
71
+ #
72
+ # Example:
73
+ # locals user: current_user, url: url
74
+ # locals order: order
75
+ #
76
+ def locals( args )
77
+ params[:locals] ||= {}
78
+ params[:locals].merge! args
28
79
  end
29
80
 
30
- def eval( *args )
81
+ def compile( *args )
31
82
  unless args.size == blk.arity
32
- raise ArgumentError.new("wrong number of arguments for mailer '#{name}': #{args.size} for #{blk.arity}")
83
+ raise Aerogel::Mailer::Error.new("wrong number of arguments for mailer '#{name}': #{args.size} for #{blk.arity}")
84
+ end
85
+ @self_before_instance_eval = eval "self", blk.binding
86
+ params.clear
87
+ instance_exec( *args, &blk )
88
+ params[:from] ||= config.mailer.default_from
89
+ if params[:from].nil?
90
+ raise Aerogel::Mailer::Error.new("'from' address is not set for mailer '#{name}'")
33
91
  end
34
- self.instance_exec( *args, &blk )
92
+ render_body
35
93
  params
36
94
  end
37
95
 
96
+ def method_missing( method, *args, &block )
97
+ @self_before_instance_eval.send method, *args, &block
98
+ end
99
+
100
+ def self.register_mailer( mailer )
101
+ @mailers ||= {}
102
+ @mailers[mailer.name] = mailer
103
+ end
104
+
105
+ def self.mailers
106
+ @mailers || {}
107
+ end
108
+
109
+ private
110
+
111
+ # Returns template file name, use cached file name if possible.
112
+ #
113
+ def fetch_template_name( filename )
114
+ @template_name_cache ||= {} # reset if reload templates is used
115
+ return @template_name_cache[filename] unless @template_name_cache[filename].nil?
116
+ if Aerogel.get_resource( :views, filename+".erb" )
117
+ @template_name_cache[filename] = filename.to_sym
118
+ else
119
+ @template_name_cache[filename] = false # template not found
120
+ end
121
+ @template_name_cache[filename]
122
+ end
123
+
124
+ # Renders message body using filled params.
125
+ # Stores rendered body (text and html parts) into params[:body] hash.
126
+ #
127
+ def render_body
128
+ params[:body] ||= {}
129
+ return unless params[:body].blank? # body set in the mailer definition block
130
+ if params[:layout] == false
131
+ layout_text = false
132
+ layout_html = false
133
+ else
134
+ layout_name = params[:layout] || DEFAULT_LAYOUT
135
+ layout_text = fetch_template_name( "layouts/#{layout_name}.text" )
136
+ layout_html = fetch_template_name( "layouts/#{layout_name}.html" )
137
+ end
138
+ body_text = fetch_template_name( "mailers/#{name}.text" )
139
+ body_html = fetch_template_name( "mailers/#{name}.html" )
140
+ if !body_text && !body_html
141
+ raise Aerogel::Mailer::Error.new "No body templates found for mailer '#{name}'"
142
+ end
143
+ if body_text
144
+ params[:body][:text] = erb body_text, layout: layout_text, locals: params[:locals]
145
+ end
146
+ if body_html
147
+ params[:body][:html] = erb body_html, layout: layout_html, locals: params[:locals]
148
+ end
149
+ true
150
+ end
151
+
38
152
  end # class Definition
39
153
  end # module Aerogel::Mailer
40
154
 
@@ -0,0 +1,5 @@
1
+ module Aerogel::Mailer
2
+ class Error < StandardError
3
+
4
+ end # class Error
5
+ end # module Aerogel::Mailer
@@ -1,5 +1,5 @@
1
1
  module Aerogel
2
2
  module Mailer
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aerogel-mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-30 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aerogel-core
@@ -80,16 +80,15 @@ files:
80
80
  - Rakefile
81
81
  - aerogel-mailer.gemspec
82
82
  - app/helpers/email.rb
83
- - config/README.md
84
83
  - config/development/mailer.conf
85
84
  - config/mailer.conf
86
85
  - config/production/mailer.conf
87
86
  - lib/aerogel/mailer.rb
88
87
  - lib/aerogel/mailer/core.rb
89
88
  - lib/aerogel/mailer/definition.rb
89
+ - lib/aerogel/mailer/error.rb
90
90
  - lib/aerogel/mailer/version.rb
91
- - views/README.md
92
- homepage: ''
91
+ homepage: https://github.com/kukushkin/aerogel-mailer
93
92
  licenses:
94
93
  - MIT
95
94
  metadata: {}
data/config/README.md DELETED
@@ -1,3 +0,0 @@
1
- A place for your ```*.conf``` files.
2
-
3
- Environment specific configs go into folders 'production' or 'development'.
data/views/README.md DELETED
@@ -1 +0,0 @@
1
- A place for module views.