outbox-rails 0.2.0 → 0.3.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: 57294d890576209761af55f8add69fbe000ecb4a
4
- data.tar.gz: 48ea5264966ae0e32d2a4d1748270e6784c70848
3
+ metadata.gz: 16b0ef1c8739ebb7a5111e9f21ae02cf5d7f6cb9
4
+ data.tar.gz: 35e6634f4ce96b930cfbd8707f1895a292824554
5
5
  SHA512:
6
- metadata.gz: 417e24b5a13534a2ccdfdb3a446d6f65e90db9d0f6ee1aaa0ba8df0998811643318c2ed8793374df05355c8bb70ffa2640418e7cd1d8a6236d13338dc76f0fc8
7
- data.tar.gz: b4bfb5c923352b9746101a22be60e2c91b87dc7078309c8ab97c8996547d90050eb7856ceef2653664d4eb211dc750bc7645005af0cb0e057a5b60ad036b9de3
6
+ metadata.gz: 0486bee46a316dd4e87de33b70319d7f32939e1bbdc61efd99e3bcb85364757a3a319c6c50ae791b128ec6a0bc43c7ab71d4e50dd8364653a202868197081d70
7
+ data.tar.gz: 5e2b1ceff8710a8d5ab5b6d5933226791c2bef7196f3eea3c8de1b0f2f9dfeffc39c81409639a517dac11f812bb0c43e6ebbb9867a2ec4d5e14ff350de5ced96
data/README.md CHANGED
@@ -45,6 +45,9 @@ class AccountNotifier < Outbox::Notifier
45
45
  end
46
46
 
47
47
  sms do
48
+ from '<shortcode_id>'
49
+ # The "text" template will automatically be used for the body of the SMS.
50
+ # But you can explicitly override by calling the #body method.
48
51
  body 'Welcome to our App!'
49
52
  end
50
53
 
@@ -58,11 +61,49 @@ end
58
61
  Send a message using the `deliver` method:
59
62
 
60
63
  ```ruby
61
- # Unlike ActionMailer, deliver takes a single argument that defines the recipients
64
+ # Unlike ActionMailer, deliver takes an argument that defines the recipients
62
65
  # for the message types you want to send.
63
66
  AccountNotifier.welcome.deliver email: 'user@gmail.com', sms: '+15557654321'
64
67
  ```
65
68
 
69
+ Configuration
70
+ -------------
71
+
72
+ Configure Outbox using the `config.outbox` accessor during normal Rails
73
+ configuration:
74
+
75
+ ``` ruby
76
+ # config/application.rb
77
+ module Blog
78
+ class Application < Rails::Application
79
+ # Configure defautl email fields
80
+ config.outbox.email_defaults = {
81
+ from: 'from@example.com'
82
+ }
83
+
84
+ # Setup default email settings.
85
+ config.outbox.default_email_client_settings = {
86
+ smtp_settings: {
87
+ address: 'smtp.gmail.com',
88
+ port: 587,
89
+ domain: 'example.com',
90
+ user_name: '<username>',
91
+ password: '<password>',
92
+ authentication: 'plain',
93
+ enable_starttls_auto: true
94
+ }
95
+ }
96
+ end
97
+ end
98
+
99
+ # config/environments/test.rb
100
+ Blog::Application.configure do
101
+ # Always use test client during tests
102
+ config.outbox.use_test_client = true
103
+ end
104
+
105
+ ```
106
+
66
107
  Contributing
67
108
  ------------
68
109
 
@@ -2,25 +2,28 @@ require 'action_mailer'
2
2
 
3
3
  module Outbox
4
4
  class Notifier < ActionMailer::Base
5
+ extend Outbox::DefineInheritableMethod
6
+ include Outbox::NotifierTypes
7
+
5
8
  abstract!
6
9
 
7
10
  alias_method :_render_email, :mail
8
11
  undef :mail
9
12
 
10
13
  class << self
11
- alias :defaults :default
14
+ alias_method :defaults, :default
12
15
 
13
16
  # Returns the name of current notifier. This method is also being used
14
17
  # as a path for a view lookup. If this is an anonymous notifier,
15
18
  # this method will return +anonymous+ instead.
16
19
  def notifier_name(value = nil)
17
20
  if value.nil?
18
- self.mailer_name
21
+ mailer_name
19
22
  else
20
23
  self.mailer_name = value
21
24
  end
22
25
  end
23
- alias :notifier_name= :notifier_name
26
+ alias_method :notifier_name=, :notifier_name
24
27
 
25
28
  protected
26
29
 
@@ -38,7 +41,7 @@ module Outbox
38
41
  # Make sure we don't ever get a NullMail object.
39
42
  @_mail_was_called = true
40
43
  @_message_rendered = false
41
- @_message = Outbox::Message.new self.class.default_params.dup
44
+ @_message = build_message
42
45
  process(method_name, *args) if method_name
43
46
  end
44
47
 
@@ -58,16 +61,22 @@ module Outbox
58
61
  # #message object is retrieved.
59
62
  def render_message(options = {}, &block)
60
63
  @_message_rendered = true
61
- @_message.email ||= Outbox::Messages::Email.new
64
+ if @_message.email
65
+ email = @_message.email
66
+ skip_email = false
67
+ else
68
+ email = Outbox::Messages::Email.new
69
+ skip_email = true
70
+ end
62
71
 
63
72
  # Render an email using the #mail interface so we don't have
64
73
  # to rewrite the template logic. Even if we aren't sending an email
65
74
  # we can still use the rendered templates in other messages types.
66
- email_options = options.extract! :content_type, :charset, :parts_order,
67
- :body, :template_name, :template_path
68
- email_options.merge!(options.delete(:email)) if options[:email]
69
- email_options[:subject] ||= email.subject if email.subject
70
- email = render_email(email_options, &block)
75
+ begin
76
+ render_email(email, options, &block)
77
+ rescue ActionView::MissingTemplate => error
78
+ raise error unless skip_email
79
+ end
71
80
 
72
81
  @_message.assign_message_type_values(options)
73
82
  assign_body_from_email(email)
@@ -92,32 +101,42 @@ module Outbox
92
101
 
93
102
  protected
94
103
 
104
+ def build_message
105
+ message = Outbox::Message.new(self.class.default_params.dup)
106
+ Outbox::Message.message_types.each_key do |message_type|
107
+ message.public_send(message_type, {})
108
+ end
109
+ message
110
+ end
111
+
95
112
  def assign_body_from_email(email)
96
- # TODO: Implement this later once we have multiple message types. This
97
- # will extract the rendered body from the mail (preferring text/plain)
98
- # and assign it to the other message types.
113
+ text_part = email.parts.find { |p| p.mime_type == 'text/plain' }
114
+ if text_part
115
+ @_message.each_message_type do |message_type, message|
116
+ next if message.nil? || message_type == :email
117
+ message.body = text_part.body.raw_source
118
+ end
119
+ end
99
120
  end
100
121
 
101
- def render_email(options, &block)
122
+ def render_email(email, options, &block)
123
+ email_options = options.extract!(
124
+ :content_type, :charset, :parts_order,
125
+ :body, :template_name, :template_path
126
+ )
127
+ email_options.merge!(options.delete(:email)) if options[:email]
128
+ # ActionMailer will use the default i18n subject
129
+ # unless we explicitly set it on this options hash.
130
+ email_options[:subject] ||= email.subject if email.subject
131
+
102
132
  outbox_message = @_message
103
- @_message = outbox_message.email
104
- email = _render_email(options, &block)
133
+ @_message = email
134
+ _render_email(email_options, &block)
135
+ ensure
105
136
  @_message = outbox_message
106
137
  email
107
138
  end
108
139
 
109
- def method_missing(method, *args, &block)
110
- if @_message.respond_to?(method)
111
- @_message.public_send(method, *args, &block)
112
- else
113
- super
114
- end
115
- end
116
-
117
- def respond_to_missing?(method, include_private = false)
118
- super || @_message.respond_to?(method, include_private)
119
- end
120
-
121
140
  ActiveSupport.run_load_hooks(:outbox_notifier, self)
122
141
  end
123
142
  end
@@ -0,0 +1,37 @@
1
+ module Outbox
2
+ module NotifierTypes
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ Outbox::Message.message_types.each_key do |message_type|
7
+ define_notifier_type_reader(message_type)
8
+ define_notifier_type_writer(message_type)
9
+ define_skip_notifier_type(message_type)
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ DYNAMIC_MODULE_NAME = :DynamicNotifierTypes
15
+
16
+ protected
17
+
18
+ def define_notifier_type_reader(name)
19
+ define_inheritable_method(DYNAMIC_MODULE_NAME, name) do |*args, &block|
20
+ @_message.public_send(name, *args, &block)
21
+ end
22
+ end
23
+
24
+ def define_notifier_type_writer(name)
25
+ define_inheritable_method(DYNAMIC_MODULE_NAME, "#{name}=") do |value|
26
+ @_message.public_send("#{name}=", value)
27
+ end
28
+ end
29
+
30
+ def define_skip_notifier_type(name)
31
+ define_inheritable_method(DYNAMIC_MODULE_NAME, "skip_#{name}!") do
32
+ @_message.public_send("#{name}=", nil)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -5,6 +5,7 @@ require 'active_support/rails'
5
5
 
6
6
  module Outbox
7
7
  autoload :Notifier, 'outbox/notifier'
8
+ autoload :NotifierTypes, 'outbox/notifier_types'
8
9
 
9
10
  module Rails
10
11
  end
@@ -1,5 +1,5 @@
1
1
  module Outbox
2
2
  module Rails
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency 'outbox', '~> 0.1.2'
21
+ spec.add_runtime_dependency 'outbox', '~> 0.2.0'
22
22
  spec.add_runtime_dependency 'rails', '~> 4.0.0'
23
23
  spec.add_development_dependency 'bundler', '~> 1.3'
24
24
  spec.add_development_dependency 'rake'
@@ -4,7 +4,9 @@ class BaseNotifier < Outbox::Notifier
4
4
  end
5
5
 
6
6
  def implicit_multipart(hash = {})
7
- attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
7
+ if hash.delete(:attachments)
8
+ attachments['invoice.pdf'] = 'This is test File content'
9
+ end
8
10
  render_message(hash)
9
11
  end
10
12
 
@@ -18,4 +20,12 @@ class BaseNotifier < Outbox::Notifier
18
20
  headers 'X-Custom-1' => 'foo'
19
21
  headers['X-Custom-2'] = 'bar'
20
22
  end
23
+
24
+ def explicit_sms_message(skip_email = false)
25
+ skip_email! if skip_email
26
+ sms do
27
+ from '1234'
28
+ body 'Explicit Message'
29
+ end
30
+ end
21
31
  end
@@ -79,5 +79,23 @@ describe Outbox::Notifier do
79
79
  expect(message.email.header['X-Custom-1'].value).to eql('foo')
80
80
  expect(message.email.header['X-Custom-2'].value).to eql('bar')
81
81
  end
82
+
83
+ it 'handles implicit SMS templates' do
84
+ message = BaseNotifier.implicit_multipart
85
+ expect(message.sms.body.strip).to eq('TEXT Implicit Multipart')
86
+ end
87
+
88
+ it 'handles explicit SMS messages' do
89
+ message = BaseNotifier.explicit_sms_message(true)
90
+ expect(message.email).to be_nil
91
+ expect(message.sms.from).to eq('1234')
92
+ expect(message.sms.body).to eq('Explicit Message')
93
+ end
94
+
95
+ it 'raises template errors when sending emails' do
96
+ expect {
97
+ BaseNotifier.explicit_sms_message
98
+ }.to raise_error(ActionView::MissingTemplate)
99
+ end
82
100
  end
83
101
  end
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
3
  require 'combustion'
4
4
 
5
5
  Combustion.initialize!(:all) do
6
- config.outbox.default_email_client = :test
6
+ config.outbox.use_test_client = true
7
7
  config.outbox.default_email_client_settings = { option_1: true }
8
8
  end
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outbox-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Browne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-22 00:00:00.000000000 Z
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: outbox
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.2
19
+ version: 0.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.2
26
+ version: 0.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +162,7 @@ files:
162
162
  - lib/generators/test_unit/notifier/templates/notifier_test.rb
163
163
  - lib/outbox-rails.rb
164
164
  - lib/outbox/notifier.rb
165
+ - lib/outbox/notifier_types.rb
165
166
  - lib/outbox/rails.rb
166
167
  - lib/outbox/rails/railtie.rb
167
168
  - lib/outbox/rails/version.rb