Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActionMailer::Base can unregister observer(s) and interceptor(s). #32207

Merged

Conversation

@kmiyake
Copy link
Contributor

@kmiyake kmiyake commented Mar 8, 2018

Summary

This adds the ability to unregister previously registered observer(s) and interceptor(s).

Other Information

This changes was re-implemented this.

One or multiple mail observers can be unregistered using
`ActionMailer::Base.unregister_observers` or
`ActionMailer::Base.unregister_observer`.

One or multiple mail interceptors can be unregistered using
`ActionMailer::Base.unregister_interceptors` or
`ActionMailer::Base.unregister_interceptor`.

For preview interceptors, it's possible to use
`ActionMailer::Base.unregister_preview_interceptors` or
`ActionMailer::Base.unregister_preview_interceptor`.
@rails-bot
Copy link

@rails-bot rails-bot commented Mar 8, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

mail.deliver_now
end

ActionMailer::Base.unregister_observer(MyObserver)
Copy link
Member

@rafaelfranca rafaelfranca Mar 8, 2018

This needs to be called inside the ensure otherwise if the test fail in the lines above this the observer will still be registered.

Copy link
Contributor Author

@kmiyake kmiyake Mar 9, 2018

Thanks for your review. I'll fix it as soon as possible.

Copy link
Contributor Author

@kmiyake kmiyake Mar 19, 2018

@rafaelfranca
How about the commit below.
1286c12

Copy link
Member

@sikachu sikachu left a comment

Looks good to me.

@kmiyake do you mind adding changeling for this feature as well?

This makes it possible to dynamically add and remove email observers and
interceptors at runtime in the same way they're registered.

*Kota Miyake*
Copy link
Member

@eugeneius eugeneius May 29, 2018

Since this builds on #18446, could you add the original author's name here too?

Copy link
Contributor Author

@kmiyake kmiyake May 30, 2018

Since this builds on #18446, could you add the original author's name here too?

Fixed.

@kmiyake
Copy link
Contributor Author

@kmiyake kmiyake commented May 30, 2018

@kmiyake do you mind adding changeling for this feature as well?

Done.

@rafaelfranca rafaelfranca merged commit b74edd3 into rails:master May 30, 2018
2 checks passed
Cruikshanks added a commit to DEFRA/waste-carriers-frontend that referenced this issue Apr 25, 2019
**Only intercept when necessary**

This was the original intention but after many attempts realised that the initializers are called before any kind of mocking/stubbing goes on in the RSpec test. So we would never be able to control whether the interceptor is registered within our tests.

So next we looked at ignoring the interceptor and using either RSpec filters or hooks to control the registering and unregistering of the interceptor. However we hit an issue there as well. Registering the interceptor was fine, but unregistering was only merged into ActionMailer in May 2018 (rails/rails#32207).

So we'd be left with having to also introduce a monkey patch to ensure the tests were clean and the interceptor was removed. 😩

This seemed overkill for what we wanted to achieve. Hence we hit on a final hack, which is to always register the interceptor in the initializer if we are running in "test".

**Fixing the test**

Realised when working through trying to control whether the interceptor is registered or not that the previous version of the test wasn't actually testing its intent. It is based on the fact that the last email cache feature is enabled, and as such when we check the response we expect to see some email content.

However when the interceptor was not being registered `LastEmailCache` was just returning its default error message. As the test was just comparing the response to `LastEmailCache.last_email_json` they were always going to match whether the interceptor was registered or not.

So this also tweaks the test such that it matches the intent of checking that the response contains content from an actual sent email.
Cruikshanks added a commit to DEFRA/waste-carriers-frontend that referenced this issue Apr 25, 2019
**Only intercept when necessary**

This was the original intention but after many attempts realised that the initializers are called before any kind of mocking/stubbing goes on in the RSpec test. So we would never be able to control whether the interceptor is registered within our tests.

So next we looked at ignoring the interceptor and using either RSpec filters or hooks to control the registering and unregistering of the interceptor. However we hit an issue there as well. Registering the interceptor was fine, but unregistering was only merged into ActionMailer in May 2018 (rails/rails#32207).

So we'd be left with having to also introduce a monkey patch to ensure the tests were clean and the interceptor was removed. 😩

This seemed overkill for what we wanted to achieve. Hence we hit on a final hack, which is to always register the interceptor in the initializer if we are running in "test".

**Fixing the test**

Realised when working through trying to control whether the interceptor is registered or not that the previous version of the test wasn't actually testing its intent. It is based on the fact that the last email cache feature is enabled, and as such when we check the response we expect to see some email content.

However when the interceptor was not being registered `LastEmailCache` was just returning its default error message. As the test was just comparing the response to `LastEmailCache.last_email_json` they were always going to match whether the interceptor was registered or not.

So this also tweaks the test such that it matches the intent of checking that the response contains content from an actual sent email.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants