Skip to content

Commit 3c6e968

Browse files
author
Neeraj Singh
committed
Get out of subject_prefix business
1 parent 2f69c18 commit 3c6e968

3 files changed

Lines changed: 7 additions & 61 deletions

File tree

README.md

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ non-production environment. However it also provides ability to not
77
intercept certain emails so that testing of emails is easier in
88
development/staging environment.
99

10-
If `subject_prefix` is supplied then that is added to every single email
11-
in both production and non-production environment.
12-
1310
## Installation
1411

1512
Add this line to your application's Gemfile:
1613

1714
```ruby
18-
gem 'mail_interceptor'
15+
# There is no need to include this gem for production or for test environment
16+
gem 'mail_interceptor', group: [:development, :staging]
1917
```
2018

2119
## Usage
@@ -24,8 +22,7 @@ gem 'mail_interceptor'
2422
# config/initializer/mail_interceptor.rb
2523

2624
options = { forward_emails_to: 'intercepted_emails@domain.com',
27-
deliver_emails_to: ["@wheel.com"],
28-
subject_prefix: 'WHEEL' }
25+
deliver_emails_to: ["@wheel.com"] }
2926

3027
interceptor = MailInterceptor::Interceptor.new(options)
3128
unless Rails.env.test?
@@ -64,17 +61,6 @@ will be intercepted and forwarded.
6461
The regular expression is matched without case sensitive. So you can mix lowercase
6562
and uppercase and it won't matter.
6663

67-
### subject_prefix
68-
69-
__subject_prefix__ is optional. If it is supplied then it is added to
70-
the front of the subject. In non-production environment the environment
71-
name is also added.
72-
73-
```
74-
[WHEEL] Forgot password
75-
[WHEEL STAGING] Forgot password
76-
```
77-
7864
### forward_emails_to
7965

8066
This is a required field.

lib/mail_interceptor.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
module MailInterceptor
66
class Interceptor
7-
attr_accessor :deliver_emails_to, :forward_emails_to, :subject_prefix, :env
7+
attr_accessor :deliver_emails_to, :forward_emails_to, :env
88

99
def initialize options = {}
1010
@deliver_emails_to = Array.wrap options[:deliver_emails_to]
11-
@subject_prefix = options[:subject_prefix] || ''
1211
@forward_emails_to = options.fetch :forward_emails_to
1312
@env = options.fetch :env, InterceptorEnv.new
1413

1514
sanitize_forward_emails_to
1615
end
1716

1817
def delivering_email message
19-
add_subject_prefix message
2018
message.to = normalize_recipients(message.to).flatten.uniq
2119
end
2220

@@ -36,13 +34,6 @@ def normalize_recipients recipients
3634
end
3735
end
3836

39-
def add_subject_prefix message
40-
return if subject_prefix.blank?
41-
42-
modified_subject_prefix = add_env_info_to_subject_prefix
43-
message.subject = "#{modified_subject_prefix} #{message.subject}"
44-
end
45-
4637
def sanitize_forward_emails_to
4738
self.forward_emails_to = Array.wrap forward_emails_to
4839

@@ -51,10 +42,6 @@ def sanitize_forward_emails_to
5142
end
5243
end
5344

54-
def add_env_info_to_subject_prefix
55-
"[#{subject_prefix} #{env.name}]"
56-
end
57-
5845
def forward_emails_to_empty?
5946
Array.wrap(forward_emails_to).reject(&:blank?).empty?
6047
end

test/mail_interceptor_test.rb

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,26 @@ def test_invocation_of_regular_expression
3636
assert_equal ["a@wheel.com", "b@wheel.com", "c@pump.com", "test@example.com", "john@gmail.com"], @message.to
3737
end
3838

39-
def test_no_subject_prefix_in_test
40-
interceptor = ::MailInterceptor::Interceptor.new env: env,
41-
forward_emails_to: 'test@example.com',
42-
subject_prefix: nil
43-
@message.subject = 'Forgot password'
44-
45-
interceptor.delivering_email @message
46-
assert_equal "Forgot password", @message.subject
47-
end
48-
49-
def test_subject_prefix_in_test
50-
interceptor = ::MailInterceptor::Interceptor.new env: env,
51-
forward_emails_to: 'test@example.com',
52-
subject_prefix: 'wheel'
53-
@message.subject = 'Forgot password'
54-
55-
interceptor.delivering_email @message
56-
assert_equal "[wheel TEST] Forgot password", @message.subject
57-
58-
@message.subject = 'Another Forgot password'
59-
interceptor.delivering_email @message
60-
assert_equal "[wheel TEST] Another Forgot password", @message.subject
61-
end
62-
6339
def test_error_if_forward_emails_to_is_empty
6440
message = "forward_emails_to should not be empty"
6541

6642
exception = assert_raises(RuntimeError) do
6743
::MailInterceptor::Interceptor.new env: env,
68-
forward_emails_to: '',
69-
subject_prefix: 'wheel'
44+
forward_emails_to: ''
7045
end
7146

7247
assert_equal message, exception.message
7348

7449
exception = assert_raises(RuntimeError) do
7550
::MailInterceptor::Interceptor.new env: env,
76-
forward_emails_to: [],
77-
subject_prefix: 'wheel'
51+
forward_emails_to: []
7852
end
7953

8054
assert_equal message, exception.message
8155

8256
exception = assert_raises(RuntimeError) do
8357
::MailInterceptor::Interceptor.new env: env,
84-
forward_emails_to: [''],
85-
subject_prefix: 'wheel'
58+
forward_emails_to: ['']
8659
end
8760

8861
assert_equal message, exception.message

0 commit comments

Comments
 (0)