Skip to content

Fix deprecated use of config.action_mailer.preview_path = in favor of ...preview_paths << to support Rails 7.1 upgrade #2705

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example_app_generator/spec/support/default_preview_path
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ require_file_stub 'config/environment' do
config.action_mailer.raise_delivery_errors = false unless ENV['NO_ACTION_MAILER']

if ENV['CUSTOM_PREVIEW_PATH']
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
config.action_mailer.preview_paths << ENV['CUSTOM_PREVIEW_PATH']
end
if ENV['SHOW_PREVIEWS']
config.action_mailer.show_previews = (ENV['SHOW_PREVIEWS'] == 'true')
Expand All @@ -63,7 +63,7 @@ exit if ENV['NO_ACTION_MAILER']
if ENV['DEFAULT_URL']
puts ActionMailer::Base.default_url_options[:host]
elsif defined?(::ActionMailer::Preview)
puts Rails.application.config.action_mailer.preview_path
puts Rails.application.config.action_mailer.preview_paths
end

# This will force the loading of ActionMailer settings to ensure we do not
Expand Down
23 changes: 15 additions & 8 deletions lib/rspec-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ class Railtie < ::Rails::Railtie
end

# This is called after the environment has been loaded but before Rails
# sets the default for the `preview_path`
# sets the default for the `preview_paths`
initializer "rspec_rails.action_mailer",
before: "action_mailer.set_configs" do |app|
setup_preview_path(app)
setup_preview_paths(app)
end

private

def setup_preview_path(app)
def setup_preview_paths(app)
return unless supports_action_mailer_previews?(app.config)

options = app.config.action_mailer
config_default_preview_path(options) if config_preview_path?(options)
config_default_preview_paths(options) if config_preview_paths?(options)
end

def config_preview_path?(options)
def config_preview_paths?(options)
# We cannot use `respond_to?(:show_previews)` here as it will always
# return `true`.
if options.show_previews.nil?
Expand All @@ -47,10 +47,17 @@ def config_preview_path?(options)
end
end

def config_default_preview_path(options)
return unless options.preview_path.blank?
def config_default_preview_paths(options)
if options.respond_to?(:preview_paths)
return unless options.preview_paths.blank?

options.preview_path = "#{::Rails.root}/spec/mailers/previews"
options.preview_paths << "#{::Rails.root}/spec/mailers/previews"
else
return unless options.preview_path.blank?

options.preview_path = "#{::Rails.root}/spec/mailers/previews"

end
end

def supports_action_mailer_previews?(config)
Expand Down