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

Support multiple preview paths for mailers #31595

Merged
merged 1 commit into from Aug 9, 2022

Conversation

fatkodima
Copy link
Member

It should be great to see mailer previews from engines. This PR allows to do this.

Originated from a bit outdated #16836.
Notable changes compared to that PR:

  • config.action_mailer.preview_path deprecating
  • Engine initializer to automatically add mailer preview paths to the lookup path for mailer previews for the application

Thanks in advance.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kamipo (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.

Copy link
Contributor

@georgeclaghorn georgeclaghorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Please use preview_paths instead.
MSG
self.class.preview_paths.first
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This instance method is currently private, so I think it can be removed without deprecation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like instance reader is public

mattr_accessor :preview_path, instance_writer: false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You‘re right, sorry. 😅

assert_match '<li><a href="/rails/mailers/notifier/foo">foo</a></li>', last_response.body
assert_match '<h3><a href="/rails/mailers/confirm">Confirm</a></h3>', last_response.body
assert_match '<li><a href="/rails/mailers/confirm/foo">foo</a></li>', last_response.body
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can be combined with the existing one for loading previews from custom preview paths above.

@fatkodima fatkodima force-pushed the mailer-preview_paths branch 2 times, most recently from 833d4c9 to 6db2c09 Compare January 9, 2018 12:09
@fatkodima
Copy link
Member Author

@georgeclaghorn, thanks for review. Fixed your notes, squashed and rebased.
Anything else?

@MadLittleMods
Copy link

Friendly ping to revive 👋

We have a use case for this to support some GitLab Enterprise Edition specific email previews which are namespaced under the ee/ directory.

config.action_mailer.preview_paths = [
  'spec/mailers/previews',
  'ee/spec/mailers/previews'
]

Here is the current config, https://gitlab.com/gitlab-org/gitlab-ce/blob/b17457a7f7fb390c829ff5feed7e4635bf2b4521/config/environments/development.rb#L42

@rails-bot
Copy link

rails-bot bot commented Dec 18, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 18, 2019
@rails-bot rails-bot bot closed this Dec 25, 2019
@fatkodima
Copy link
Member Author

@georgeclaghorn Wdyt about moving this forward?

@simi
Copy link
Contributor

simi commented Dec 25, 2019

I was just looking for the implementation details. Since it pass preview_path to Dir[], isn't is possible to pass multiple values in matching literal `{a,b}?

ActionMailer::Base.preview_path = "{/etc,/tmp}"

Anyway it is just non-intuitive workaround. Introducing action_mailer.preview_paths seems more intuitive.

@@ -25,7 +25,31 @@ module Previews #:nodoc:
mattr_accessor :preview_interceptors, instance_writer: false, default: [ActionMailer::InlinePreviewInterceptor]
end

def preview_path
ActiveSupport::Deprecation.warn(<<-MSG.squish)
Using preview_path option is deprecated and will be removed in Rails 6.0.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⌛ Rails 6.1 now

@fatkodima
Copy link
Member Author

@eugeneius please, review 🙏

actionmailer/lib/action_mailer/railtie.rb Outdated Show resolved Hide resolved
actionmailer/lib/action_mailer/railtie.rb Outdated Show resolved Hide resolved
guides/source/action_mailer_basics.md Outdated Show resolved Hide resolved
railties/test/application/mailer_previews_test.rb Outdated Show resolved Hide resolved
@eugeneius
Copy link
Member

Engine initializer to automatically add mailer preview paths to the lookup path for mailer previews for the application

Did this part not work out? It sounds useful, since that's the main motivating use case for supporting multiple preview paths.

@kaspth
Copy link
Contributor

kaspth commented May 26, 2020

Yeah that, and engines auto include controllers/routes etc in the main app. So this would fit right with that.

@fatkodima fatkodima force-pushed the mailer-preview_paths branch 2 times, most recently from d8f34be to 8f8c78c Compare May 26, 2020 16:29
@fatkodima
Copy link
Member Author

@eugeneius thanks! Updated.

Re:

Engine initializer to automatically add mailer preview paths to the lookup path for mailer previews for the application

I initially implemented that. Then something went wrong and I decided to remove this and allow for engines to manually add preview paths. And now everything went right ( 😄 )and I added back engine initializer (and tests for it).

@rails-bot rails-bot bot removed the stale label Nov 24, 2020
Base automatically changed from master to main January 14, 2021 17:00
@kennyadsl
Copy link
Contributor

This would be super useful for Rails Engines that add mailer previews in the host application as we do in Solidus. We worked around the issue with this PR but would be great to have support for that in Rails directly. Any chance to move this forward?

@p8
Copy link
Member

p8 commented Jul 6, 2021

@fatkodima could you fix the merge conflicts and update the deprecations from 6.2 to 7.1? 🙇

@fatkodima
Copy link
Member Author

@p8 Rebased and changed 6.2 to 7.1.

@p8
Copy link
Member

p8 commented Jul 6, 2021

Thanks @fatkodima !

@fatkodima fatkodima force-pushed the mailer-preview_paths branch 2 times, most recently from fb22c79 to 64ad045 Compare June 30, 2022 12:10
@rafaelfranca rafaelfranca merged commit 95066e5 into rails:main Aug 9, 2022
@fxn
Copy link
Member

fxn commented Aug 25, 2023

I believe this needs a revision, it introduces a regression.

Since config.paths is cached as soon as accessed, config.autoload_paths and friends are going to have whatever paths["test/mailers/previews"] had at the time the object was memoized. Therefore, concatenating to paths["test/mailers/previews"] as it is done by the initializer does not necessarily add them to the autoload paths. If users configured config.autoload_paths, for example, mailer previews won't be found.

I would consider removing the newly introduced paths["test/mailers/previews"] and having the initializer push things to the autoload paths simply by concatenating to config.autoload_paths by hand, which is a good interface between railties and engines or applications. Please, see #49032, which needed a workaround for this.

@fxn
Copy link
Member

fxn commented Aug 25, 2023

Let me illustrate the regression.

In Rails 7.0, if you have a directory lib/mailer_previews and this in config/application.rb:

config.autoload_paths
config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"

then

% bin/rails r 'pp ActiveSupport::Dependencies.autoload_paths.grep(/lib/)'
["/Users/fxn/tmp/del_mailer_previews/lib/mailer_previews"]

With this patch, lib/mailer_previews is no longer in the autoload paths.

Besides being a regression, conceptually this does not look good. It wouldn't be right even if the feature was new.

(EDIT: In main the directory appears due to the workaround introduced in #49032, but that should be reverted, or updated after a revision is in place.)

@fatkodima
Copy link
Member Author

Please, feel free to provide a PR, if you will, since you better know how to implement it properly. 🙏

@fxn
Copy link
Member

fxn commented Aug 25, 2023

@fatkodima let me understand your patch better. The initializer

initializer "action_mailer.set_autoload_paths", before: :set_autoload_paths do |app|
  options = app.config.action_mailer
  app.config.paths["test/mailers/previews"].concat(options.preview_paths)
end

adds preview paths from the application/global configuration point app.config.action_mailer.preview_paths. In what sense does this patch allow to configure these per engine too?

@fatkodima
Copy link
Member Author

@fxn
Copy link
Member

fxn commented Aug 25, 2023

So engines do not have configuration API for it, right? (I don't consider config.paths["..."] to be public API).

@fatkodima
Copy link
Member Author

Right.

@fatkodima fatkodima deleted the mailer-preview_paths branch August 25, 2023 13:01
@jon-sully
Copy link

For those coming here because their Previews no longer got autoloaded after upgrading to Rails 7.1 and that use RSpec (so your previews are under /spec... not /test...), RSpec isn't updated to inject the preview_paths correctly quite yet:

rspec/rspec-rails#2703

In the meantime,

  config.action_mailer.preview_paths << "#{Rails.root}/spec/mailers/previews"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow preview_path accept a array, not only a string