outbox-rails 0.3.0 → 0.4.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: 16b0ef1c8739ebb7a5111e9f21ae02cf5d7f6cb9
4
- data.tar.gz: 35e6634f4ce96b930cfbd8707f1895a292824554
3
+ metadata.gz: 91a62722084b298ce8cc71b4225743d1ded08c8f
4
+ data.tar.gz: 4c282c245537e0b0b56e2c325fd50e5ed168814e
5
5
  SHA512:
6
- metadata.gz: 0486bee46a316dd4e87de33b70319d7f32939e1bbdc61efd99e3bcb85364757a3a319c6c50ae791b128ec6a0bc43c7ab71d4e50dd8364653a202868197081d70
7
- data.tar.gz: 5e2b1ceff8710a8d5ab5b6d5933226791c2bef7196f3eea3c8de1b0f2f9dfeffc39c81409639a517dac11f812bb0c43e6ebbb9867a2ec4d5e14ff350de5ced96
6
+ metadata.gz: b4f52277e2f943095288ca66c3365799a1ee3785f673cf5aae89858c3958b69d7b829d2bce60eb6d0b8e3c0cd53c06f1e9abc8a5bdfb2c2e70b63874de299e97
7
+ data.tar.gz: a26a2fb0b2f945f694dfa6e4eb17a32e0d3dd44d0d50f0f38225db33233d6f272969076814bc67989073565f8bb66a360f4260de041a21afb8a818ccc58aeb90
data/README.md CHANGED
@@ -66,6 +66,22 @@ Send a message using the `deliver` method:
66
66
  AccountNotifier.welcome.deliver email: 'user@gmail.com', sms: '+15557654321'
67
67
  ```
68
68
 
69
+ ### Variants (New with Rails 4.1)
70
+
71
+ You can have different templates for each message type using [variants](http://edgeguides.rubyonrails.org/4_1_release_notes.html#action-pack-variants). *Right now, only the implicit template rendering is supported.*
72
+
73
+ ``` html+ruby
74
+ <!-- app/views/account_notifier/welcome.html+email.erb -->
75
+ <h1>Welcome!</h1>
76
+
77
+ <!-- app/views/account_notifier/welcome.text+email.erb -->
78
+ Welcome! (email)
79
+
80
+ <!-- app/views/account_notifier/welcome.text+sms.erb -->
81
+ Welcome! (sms)
82
+ ```
83
+
84
+
69
85
  Configuration
70
86
  -------------
71
87
 
@@ -38,13 +38,18 @@ module Outbox
38
38
 
39
39
  def initialize(method_name = nil, *args) # :nodoc:
40
40
  super()
41
- # Make sure we don't ever get a NullMail object.
42
- @_mail_was_called = true
43
41
  @_message_rendered = false
44
42
  @_message = build_message
45
43
  process(method_name, *args) if method_name
46
44
  end
47
45
 
46
+ def process(*args) # :nodoc:
47
+ original_message = @_message
48
+ super
49
+ # Make sure we don't ever get a NullMail object.
50
+ @_message = original_message
51
+ end
52
+
48
53
  # The composed Outbox::Message instance.
49
54
  def message
50
55
  render_message unless message_rendered?
@@ -61,25 +66,9 @@ module Outbox
61
66
  # #message object is retrieved.
62
67
  def render_message(options = {}, &block)
63
68
  @_message_rendered = true
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
71
-
72
- # Render an email using the #mail interface so we don't have
73
- # to rewrite the template logic. Even if we aren't sending an email
74
- # we can still use the rendered templates in other messages types.
75
- begin
76
- render_email(email, options, &block)
77
- rescue ActionView::MissingTemplate => error
78
- raise error unless skip_email
79
- end
80
-
69
+ render_email(@_message.email, options, &block) if @_message.email
70
+ render_message_types(options)
81
71
  @_message.assign_message_type_values(options)
82
- assign_body_from_email(email)
83
72
  @_message
84
73
  end
85
74
 
@@ -101,6 +90,10 @@ module Outbox
101
90
 
102
91
  protected
103
92
 
93
+ def details_for_lookup
94
+ { variants: message_types }
95
+ end
96
+
104
97
  def build_message
105
98
  message = Outbox::Message.new(self.class.default_params.dup)
106
99
  Outbox::Message.message_types.each_key do |message_type|
@@ -109,16 +102,6 @@ module Outbox
109
102
  message
110
103
  end
111
104
 
112
- def assign_body_from_email(email)
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
120
- end
121
-
122
105
  def render_email(email, options, &block)
123
106
  email_options = options.extract!(
124
107
  :content_type, :charset, :parts_order,
@@ -137,6 +120,46 @@ module Outbox
137
120
  email
138
121
  end
139
122
 
123
+ def render_message_types(options)
124
+ templates = find_message_type_templates(options)
125
+ templates.each do |template|
126
+ variants = (template.try(:variants) || []).compact
127
+ body = render(template: template)
128
+ assign_body(body, variants.empty? ? nil : variants)
129
+ end
130
+ end
131
+
132
+ def find_message_type_templates(options)
133
+ template_path = options[:template_path] || self.class.mailer_name
134
+ template_name = options[:template_name] || action_name
135
+ lookup_context.find_all(
136
+ template_name, Array(template_path), false, [],
137
+ formats: [:text],
138
+ variants: message_types_without_email
139
+ )
140
+ end
141
+
142
+ def assign_body(body, only_message_types = nil)
143
+ if only_message_types
144
+ only_message_types = only_message_types.map(&:to_sym)
145
+ else
146
+ only_message_types = message_types_without_email
147
+ end
148
+ @_message.each_message_type do |message_type, message|
149
+ if message && message.body.nil? && message_type.in?(only_message_types)
150
+ message.body = body
151
+ end
152
+ end
153
+ end
154
+
155
+ def message_types
156
+ Outbox::Message.message_types.keys
157
+ end
158
+
159
+ def message_types_without_email
160
+ message_types - [:email]
161
+ end
162
+
140
163
  ActiveSupport.run_load_hooks(:outbox_notifier, self)
141
164
  end
142
165
  end
@@ -1,5 +1,5 @@
1
1
  module Outbox
2
2
  module Rails
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_runtime_dependency 'outbox', '~> 0.2.0'
22
- spec.add_runtime_dependency 'rails', '~> 4.0.0'
22
+ spec.add_runtime_dependency 'rails', ['>= 4.0', '<= 4.2']
23
23
  spec.add_development_dependency 'bundler', '~> 1.3'
24
24
  spec.add_development_dependency 'rake'
25
25
  spec.add_development_dependency 'rspec'
@@ -28,4 +28,7 @@ class BaseNotifier < Outbox::Notifier
28
28
  body 'Explicit Message'
29
29
  end
30
30
  end
31
+
32
+ def implicit_variants
33
+ end
31
34
  end
@@ -97,5 +97,13 @@ describe Outbox::Notifier do
97
97
  BaseNotifier.explicit_sms_message
98
98
  }.to raise_error(ActionView::MissingTemplate)
99
99
  end
100
+
101
+ if Rails.version >= '4.1'
102
+ it 'supports implicit variants by message type' do
103
+ message = BaseNotifier.implicit_variants
104
+ expect(message.email.body.encoded.strip).to eql('Email Variant')
105
+ expect(message.sms.body.strip).to eql('SMS Variant')
106
+ end
107
+ end
100
108
  end
101
109
  end
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.3.0
4
+ version: 0.4.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-07-22 00:00:00.000000000 Z
11
+ date: 2014-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: outbox
@@ -28,16 +28,22 @@ dependencies:
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ - - <=
32
35
  - !ruby/object:Gem::Version
33
- version: 4.0.0
36
+ version: '4.2'
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - ~>
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '4.0'
44
+ - - <=
39
45
  - !ruby/object:Gem::Version
40
- version: 4.0.0
46
+ version: '4.2'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: bundler
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +180,8 @@ files:
174
180
  - spec/internal/app/views/base_notifier/custom_headers.erb
175
181
  - spec/internal/app/views/base_notifier/implicit_multipart.html.erb
176
182
  - spec/internal/app/views/base_notifier/implicit_multipart.text.erb
183
+ - spec/internal/app/views/base_notifier/implicit_variants.text+email.erb
184
+ - spec/internal/app/views/base_notifier/implicit_variants.text+sms.erb
177
185
  - spec/internal/app/views/base_notifier/welcome.erb
178
186
  - spec/internal/app/views/customized_notifier/with_defaults.erb
179
187
  - spec/internal/config/database.yml
@@ -218,6 +226,8 @@ test_files:
218
226
  - spec/internal/app/views/base_notifier/custom_headers.erb
219
227
  - spec/internal/app/views/base_notifier/implicit_multipart.html.erb
220
228
  - spec/internal/app/views/base_notifier/implicit_multipart.text.erb
229
+ - spec/internal/app/views/base_notifier/implicit_variants.text+email.erb
230
+ - spec/internal/app/views/base_notifier/implicit_variants.text+sms.erb
221
231
  - spec/internal/app/views/base_notifier/welcome.erb
222
232
  - spec/internal/app/views/customized_notifier/with_defaults.erb
223
233
  - spec/internal/config/database.yml