Commit Graph

636 Commits

Author SHA1 Message Date
Martin Spickermann
dd8b0e4684 Sort mailers on mailer preview page alphabetically 2023-08-21 11:11:25 +02:00
Alex Ghiculescu
455e922b49
Introduce capture_emails and capture_broadcasts (#48798) 2023-07-25 06:42:54 +02:00
Alex Ghiculescu
ba70a5be86 Make perform_enqueued_jobs compatible with all Active Job adapters 2023-06-28 20:08:08 +10:00
Shivam Chahar
f30fc95c75 Remove redundant test 2023-05-22 19:31:46 +05:30
Jonathan Hefner
19df946585 Deprecate params via :args for assert_enqueued_email_with
In #45752, a `:params` kwarg was added to `assert_enqueued_email_with`
so that mailer params and args could be specified simultaneously.
However, for backward compatibility, the old behavior of treating an
`:args` Hash as params was preserved.  The result is that if both
`:params` and `:args` are specified and `:args` is a Hash, `:params` is
ignored.

This commit deprecates specifying params via `:args` (i.e. passing a
Hash to `:args`).

After the deprecated behavior is removed, it will be possible to pass
a Hash to `:args`, and the Hash will be treated as named args instead.
2023-05-11 14:11:17 -05:00
Max Chernyak
96f19e965b Add matcher support to assert_enqueued_email_with
Support args/params proc matchers in `assert_enqueued_email_with` to
make it behave more like `assert_enqueued_with`.

Fix #46621.

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2023-05-11 11:43:54 -05:00
Shivam Chahar
88f9bc1237 Remove noise from actionmailer callbacks test 2023-04-26 21:38:54 +05:30
Ben Sheldon
468d806406 Add *_deliver callbacks for Action Mailer 2023-03-29 17:12:19 -07:00
Andrew Novoselac
f2cacd5d11 Added deliver_enqueued_emails to ActionMailer::TestHelper. This method delivers all enqueued email jobs.
Example:

```ruby
def test_deliver_enqueued_emails
  deliver_enqueued_emails do
    ContactMailer.welcome.deliver_later
    end
  assert_emails 1
end
```
2023-02-28 14:07:42 -05:00
Jeffrey Hardy
fa17c9ac99 Allow mailer classes to customize the deliver_later queue name
The `deliver_later_queue_name` is already configurable on ActionMailer::Base,
however the value is inherited by all subclasses. Use a class-inheritable
attribute instead, so that subclasses can override.

Refs:
- https://github.com/rails/rails/pull/18587#issuecomment-324975192
2023-02-15 13:31:12 -05:00
Sean Doyle
6fea9a09f2 Gracefully degrade when ActionMailer::Base#params is nil
Prior to this change, access to `params` on `ActionMailer::Base`
instances prior to being decorated by `ActionMailer::Parameterized.with`
calls results in a `NoMethodError`:

```
Error:
ParameterizedTest#test_degrade_gracefully_when_.with_is_not_called:
NoMethodError: undefined method `[]' for nil:NilClass

  before_action { @inviter, @invitee = params[:inviter], params[:invitee] }
                                             ^^^^^^^^^^
```

This change modifies the `attr_accessor :params` to be an `attr_writer`
paired with a `params` method that assigns `@params` to an empty `Hash`
whenever it's accessed without being otherwise initialized.
2023-01-21 12:04:43 -05:00
Alex Ghiculescu
a3724cdd20 assert_emails: return the emails that were sent
This pattern is pretty common:

```ruby
assert_emails 1 do
  post invite_user_path # ...
end
email = ActionMailer::Base.deliveries.last
assert_equal "You were invited!", email.subject
```

We can make it a bit nicer, by returning the email object from `assert_emails`. This is similar to how `assert_raises` returns the error so you can do additional checks on it. With this PR:

```ruby
email = assert_emails 1 do
  post invite_user_path # ...
end
assert_equal "You were invited!", email.subject
```

If a single email is sent, a single `Mail::Message` is returned. If multiple emails were sent, an array is returned.

```ruby
emails = assert_emails 2 do
  post invite_user_path # ...
end
emails.each do |email|
  assert_equal "You were invited!", email.subject
end
```
2023-01-16 12:44:32 -07:00
Andy
0398459e9a Change sendmail default options to match Mail 2.8.x arguments format.
The Mail gem changed format of the delivery method arguments for
sendmail from a string to an array of strings in this commit
7e1196bd29

As the settings are now a sendmail delivery method produces the
following error:
```
.../mail-2.8.0/lib/mail/network/delivery_methods/sendmail.rb:53:in `initialize': :arguments expected to be an Array of individual string args (ArgumentError)
```

This also updates the mail dependency since the new default won't work
with the older versions.
2022-12-06 20:41:09 -05:00
Jonathan Hefner
b3a2bb326c Add ActionMailer.deprecator
This commit adds `ActionMailer.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `actionmailer/lib` with
`ActionMailer.deprecator`.

Additionally, this commit adds `ActionMailer.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.
2022-10-29 16:01:02 -05:00
Jonathan Hefner
48d4e6e02b Add ActionDispatch.deprecator
This commit adds `ActionDispatch.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `actionpack/lib/action_dispatch`
with `ActionDispatch.deprecator`.

Additionally, this commit adds `ActionDispatch.deprecator` to
`Rails.application.deprecators` so that it can be configured via
settings such as `config.active_support.report_deprecations`.
2022-10-27 17:11:02 -05:00
Jean Boussier
d917896f45 Enable verbose mode in test and report warnings as errors
We recently let a few very easy to avoid warnings get merged.
The root cause is that locally the test suite doesn't run in
verbose mode unless you explictly pass `-w`.

On CI warnings are enabled, but there is no reason to look at the
build output unless something is failing. And even if one wanted
to do that, that would be particularly work intensive since warnings
may be specific to a Ruby version etc.

Because of this I believe we should:

  - Always run the test suite with warnings enabled.
  - Raise an error if a warning is unexpected.

We've been using this pattern for a long time at Shopify both in private
and public repositories.
2022-10-11 09:25:18 +02:00
Yasuo Honda
349a66ebed Drop Rubinius code
Rubinius has not been maintained since May 2020 and based on the
discussion at https://github.com/rails/rails/pull/44984 ,
I think we can remove Rubinius specific code from Rails.
2022-09-15 23:43:51 +09:00
Tom Rossi
22a0692a59
Improvements to assert_enqueued_email_with (#45752)
* This is an attempt to make the assert_enqueued_email_with easier to implement.

* Update actionmailer/test/test_helper_test.rb

Fix spelling.

* Documenting additional tests

* Missing a closing "end"

* Renaming tests for consistency

* Updating name

* Naming and documentation

* Leaving original test unchanged

* Fix test name, add new test

* Add assert_enqueued_emails examples to Rails guide

* Add example to test_helper

* Tweaking the Rails guide (#3)

* Updating Rails guide for consistency.

Co-authored-by: Bry <bryan.hunt@hey.com>
Co-authored-by: Ron Shinall <81988008+ron-shinall@users.noreply.github.com>
2022-08-09 14:16:19 +02:00
Jean Boussier
fc0db35fb1 Add OutputBuffer#raw and #capture to reduce the need to swap the buffer
Right now many helpers have to deal with two modes of operation to
capture view output.

The main one is to swap the `@output_buffer` variable with a new buffer.
But since some view implementations such as `builder` keep a reference
on the buffer they were initialized with, this doesn't always work.

So additionally, the various capturing helpers also record the buffer
length prior to executing the block, and then `slice!` the buffer back
to its original size.

This is wasteful and make the code rather unclear.

Now that `OutputBuffer` is a delegator, I'd like to refactor all this
so that:

  - @output_buffer is no longer re-assigned
  - A single OutputBuffer instance is used for the entire response rendering
  - Instead capturing is done through `OutputBuffer#capture`

Once the above is achieved, it should allow us to enabled Erubi's
`:chain_appends` option and get some reduced template size and some
performance.

Not re-assigning `@output_buffer` will also allow template to access
the local variable instead of an instance variable, which is cheaper.

But more importantly, that should make the code easier to understand
and easier to be compatible with `StreamingBuffer`.
2022-08-03 12:56:34 +02:00
Fabio Sangiovanni
6e30c183b6 Fix some typos. 2022-02-21 17:58:23 +01:00
Rafael Mendonça França
ddc7fb6e6e
Remove deprecated ActionMailer::DeliveryJob and ActionMailer::Parameterized::DeliveryJob 2021-11-17 21:51:11 +00:00
Jean Boussier
e974c58bf9 Set the execution context from AC::Metal rather than AbstractController
The later is used by totally different codepaths such as mailers.

Fix: https://github.com/rails/rails/pull/43598
2021-11-15 15:36:28 +01:00
Daniel Colson
ccb3cb573b
Replace ableist language
The word "Crazy" has long been associated with mental illness. While
there may be other dictionary definitions, it's difficult for some of us
to separate the word from the stigmatization, gaslighting, and bullying
that often comes along with it.

This commit replaces instances of the word with various alternatives. I
find most of these more focused and descriptive than what we had before.
2021-10-05 22:27:09 -04:00
Thomas Hutterer
524c4cad07 Return just the address if name is blank
Otherwise the returned string would look like " <foo@example.com>".
2021-09-16 09:24:27 +02:00
Alexandre Ruban
bcd1333e3f Default values are not evaluated when overridden 2021-06-08 10:06:14 +02:00
Ryuta Kamizono
e1c62dcee4 Fix "warning: instance variable @previous_delivery_method not initialized" 2021-04-13 22:21:44 +09:00
Paul Keen
7d7bd2bfba Make sure that mailers will use default job queue
Use "default" as default queue for mailers test helpers
2020-12-18 23:35:59 +02:00
Akira Matsuda
c8c638ed44 Accessors here are public 2020-10-06 21:00:03 +09:00
Edouard CHIN
859f3bf024 Deprecate custom Action Mailer delivery job:
- Action Mailer delivery job should modify their `perform` method
  signature in order to receive the new payload that Action Mailer
  sends.

  Before:

  ```ruby
    def perform(mailer, mail_method, delivery_method, *args)
    end
  ```

  After:

  ```ruby
    def perform(mailer, mail_method, delivery_method, args:)
    end
  ```

  This new behaviour was introduced couple years ago in a attempt to
  get rid of the necessity to have a different job for paramterized
  mailers. A deprecation was introduced for custom jobs inheriting
  from `ActionMailer::DeliveryJob` but for jobs that didn't it went
  unnoticed.
  The deprecated behaviour was supposed to be removed in Rails 6.1
  but we couldn't and it got reverted https://github.com/rails/rails/pull/39257
2020-08-26 17:48:47 +02:00
Aaron Patterson
397693a5ea
Merge pull request #26445 from dracos/multiparty
Correctly wrap inline attachments.
2020-08-19 10:10:36 -07:00
Vipul A M
fa251d9e48 Add spec to verify that MessageDelivery Job accepts priority 2020-06-22 22:31:15 +05:30
eileencodes
336a07b9a4
Revert "Remove deprecated ActionMailer::DeliveryJob and ActionMailer::Parameterized::DeliveryJob"
This reverts commit 0f9249c93f402d276730fcfaba1ed1b876ee7c26.

Reverted because this wasn't warning in custom jobs and therefore
applications may have not seen the deprecation. We'll need to fix the
deprecation to warn for custom jobs so that applications can migrate.
2020-05-12 15:20:07 -04:00
Ryuta Kamizono
6a4395f466 Deprecate starts_with? and ends_with? for String core extensions
In the past, we sometimes hit missing `Symbol#start_with?` and
`Symbol#end_with?`.

63256bc5d7
a8e812964d

So I proposed `Symbol#start_with?` and `Symbol#end_with?` to allow duck
typing that methods for String and Symbol, then now it is available in
Ruby 2.7.

https://bugs.ruby-lang.org/issues/16348

Using `String#starts_with?` and `String#ends_with?` could not be gained
that conveniency, so it is preferable to not use these in the future.
2020-05-05 15:51:24 +09:00
Rafael Mendonça França
0f9249c93f
Remove deprecated ActionMailer::DeliveryJob and ActionMailer::Parameterized::DeliveryJob 2020-05-05 00:31:14 -04:00
Rafael Mendonça França
d5fa9569a0
Remove deprecated ActionMailer::Base.receive in favor of Action Mailbox 2020-05-05 00:06:22 -04:00
Ryuta Kamizono
8e452c710d Fix keyword arguments warnings in Action Mailer 2020-01-21 09:23:15 +09:00
Ryuta Kamizono
99f18b618b Revert "Merge pull request #38260 from kamipo/fix_kwargs_warning_for_activejob"
This reverts commit 80e72c5eb7d4f79cc8eaa63237e5a60dff48f4f4, reversing
changes made to 0dad1e3e770273631e5ab454d2d3003d90060e17.
2020-01-21 05:26:44 +09:00
Ryuta Kamizono
c10b580909 Fix keyword arguments warnings in Action Mailer 2020-01-19 14:29:32 +09:00
Ian Fleeton
684ae07e2f Don't encourage using octals for dates and times
This could result in confusing errors or inconsistency for 08 and 09
2020-01-15 10:57:38 +00:00
Rodrigo Ramírez Norambuena
db46f44803 Remove method encode from url_test in Actionmailer:
This method is introduced in c064802d but at this moment is not
use anymore.
2020-01-08 11:52:13 -03:00
bogdanvlviv
37b72ff5de
Fix ActionMailer assertions don't work for parameterized mail with legacy delivery job
We should backport this to `6-0-stable`.

Fixes https://github.com/rails/rails/issues/37605
2019-10-30 22:22:00 +00:00
Sunny Ripert
3a8dbe1746 Escape email addresses with name 2019-08-26 17:21:06 +02:00
Rafael Mendonça França
967beb7229
Revert "MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now"
This reverts commit 98d0f7ebd34b858f12a12dcf37ae54fdbb5cab64.
2019-08-02 00:24:21 -04:00
Rafael Mendonça França
6384933994
Revert "You give jruby_skip & rubinius_skip a good name"
This reverts commit 8d2866bb80fbe81acb04f5b0c44f152f571fb29f.
2019-08-02 00:24:11 -04:00
Akira Matsuda
8d2866bb80 You give jruby_skip & rubinius_skip a good name
This hack prevails everywhere in the codebase by being copy & pasted, and it's actually not a negative thing but a necessary thing for framework implementors,
so it should better have a name and be a thing.

And with this commit, activesupport/test/abstract_unit.rb now doesn't silently autoload AS::TestCase,
so we're ready to establish clearner environment for running AS tests (probably in later commits)
2019-08-02 05:36:38 +09:00
Akira Matsuda
98d0f7ebd3 MethodCallAssertions is a regular player of the team ActiveSupport::TestCase now
It's used everywhere, clean and mature enough
2019-08-02 05:36:15 +09:00
yuuji.yaginuma
6b3d9f8f86 Do not use the same test class in different tests
This fixes the following warnings.

```
actionmailer/test/base_test.rb:272: warning: method redefined; discarding old welcome
actionmailer/test/base_test.rb:260: warning: previous definition of welcome was here
```
2019-08-01 10:26:59 +09:00
Rafael França
abaa73f34a
Merge pull request #36227 from betesh/avoid-misleading-error-about-late-attachments
Prevent reading inline attachments after `mail` was called from raising an inaccurate exception
2019-07-26 16:00:44 -04:00
Ryuta Kamizono
c81af6ae72 Enable Layout/EmptyLinesAroundAccessModifier cop
We sometimes say "✂️ newline after `private`" in a code review (e.g.
https://github.com/rails/rails/pull/18546#discussion_r23188776,
https://github.com/rails/rails/pull/34832#discussion_r244847195).

Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style
`EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059).

That cop and enforced style will reduce the our code review cost.
2019-06-13 12:00:45 +09:00
Isaac Betesh
efeddb08da Prevent reading inline attachments after mail was called from raising an inaccurate exception
Without this change, `attachments.inline['my_attachment'].present?`, for example,
would raise the exception `Can't add attachments after mail was called`.

I first brought this issue up at https://github.com/rails/rails/issues/16163#issuecomment-437378347.

Note that this commit addresses only one of the 2 problems I described in that comment.
The other problem is that using `attachments.inline['my_attachment']` for reading an
attachment is unnecessary--it's the same as `attachments['my_attachment']`--even before
`mail` is called.  We could add a warning about the unnecessary use of `inline` but I'm
saving that for a later PR since my comment has not received any feedback yet.
2019-05-21 13:11:19 -07:00