Skip to content

subject prefix issue#10

Merged
neerajsingh0101 merged 1 commit intomasterfrom
10-subject-prefix-issue
Jan 26, 2015
Merged

subject prefix issue#10
neerajsingh0101 merged 1 commit intomasterfrom
10-subject-prefix-issue

Conversation

@sharmaansh21
Copy link
Copy Markdown

  def test_subject_prefix_in_test
    interceptor = ::MailInterceptor::Interceptor.new forward_emails_to: 'test@example.com',
                                                       subject_prefix: 'wheel'
    @message.subject = 'Forgot password'

    interceptor.delivering_email @message
    assert_equal "[wheel TEST] Forgot password", @message.subject

    @message.subject = 'Forgot password'
    interceptor.delivering_email @message
    assert_equal "[wheel TEST] Forgot password", @message.subject
  end

this scenario fails

-"[wheel TEST] Forgot password"
+"[[wheel TEST] TEST] Forgot password"

we need to move add_env_info_to_subject_prefix from https://github.com/bigbinary/mail_interceptor/blob/master/lib/mail_interceptor.rb#L18
to
https://github.com/bigbinary/mail_interceptor/blob/master/lib/mail_interceptor.rb#L13

@neerajdotname

@sharmaansh21 sharmaansh21 self-assigned this Jan 26, 2015
@vipulnsward
Copy link
Copy Markdown
Contributor

When/why would this happen?
Where would users manually rewrite email subject?

@prathamesh-sonpatki
Copy link
Copy Markdown
Contributor

This will happen because the same instance of mail_interceptor object will be used for sending all the mails. So when the first mail is sent value of subject_prefix becomes [MAB TEST].

While sending next email, as same object is again used and we have following logic for subject_prefix

def add_env_info_to_subject_prefix
... 
  _prefix = production? ? subject_prefix : "#{subject_prefix} #{env.upcase}"
  self.subject_prefix = "[#{_prefix}]"
...

It will evaluate as

_prefix = "[MAB TEST] TEST"
self.subject_prefix = "[[MAB TEST] TEST]

So when we run rake test:all we see this behavior because it sends multiple emails using same object of mail_interceptor because of the way we have setup the interceptor -

interceptor = MailInterceptor::Interceptor.new(options)
ActionMailer::Base.register_interceptor(interceptor)

@vipulnsward
Copy link
Copy Markdown
Contributor

I think we should not mutate subject prefix in add_env_info_to_subject_prefix.
Can you change it to just return ENV prefix if needed. It can then be directly used like-

def add_subject_prefix message
  return if subject_prefix.blank?
  message.subject = "[#{env_for_subject_prefix} #{subject_prefix}] #{message.subject}"
end

neerajsingh0101 pushed a commit that referenced this pull request Jan 26, 2015
neerajsingh0101 added a commit that referenced this pull request Jan 26, 2015
@neerajsingh0101 neerajsingh0101 merged commit e434342 into master Jan 26, 2015
@neerajsingh0101 neerajsingh0101 deleted the 10-subject-prefix-issue branch January 26, 2015 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants