Skip to content

Commit 0dbdd6f

Browse files
committed
Handle preview_paths in Rails 7.1
1 parent ed467f4 commit 0dbdd6f

File tree

3 files changed

+44
-17
lines changed

3 files changed

+44
-17
lines changed

example_app_generator/spec/support/default_preview_path

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ require_file_stub 'config/environment' do
4343

4444
# Don't care if the mailer can't send.
4545
config.action_mailer.raise_delivery_errors = false unless ENV['NO_ACTION_MAILER']
46-
4746
if ENV['CUSTOM_PREVIEW_PATH']
48-
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
47+
if Rails::VERSION::STRING.start_with?('7.1')
48+
config.action_mailer.preview_paths = [ENV['CUSTOM_PREVIEW_PATH']]
49+
else
50+
config.action_mailer.preview_path = ENV['CUSTOM_PREVIEW_PATH']
51+
end
4952
end
5053
if ENV['SHOW_PREVIEWS']
5154
config.action_mailer.show_previews = (ENV['SHOW_PREVIEWS'] == 'true')
@@ -66,7 +69,11 @@ exit if ENV['NO_ACTION_MAILER']
6669
if ENV['DEFAULT_URL']
6770
puts ActionMailer::Base.default_url_options[:host]
6871
elsif defined?(::ActionMailer::Preview)
69-
puts Rails.application.config.action_mailer.preview_path
72+
if Rails::VERSION::STRING.start_with?('7.1')
73+
puts Rails.application.config.action_mailer.preview_paths
74+
else
75+
puts Rails.application.config.action_mailer.preview_path
76+
end
7077
end
7178

7279
# This will force the loading of ActionMailer settings to ensure we do not

example_app_generator/spec/verify_mailer_preview_path_spec.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,26 @@ def capture_exec(*ops)
3030
CaptureExec.new(out, $?.exitstatus)
3131
end
3232

33-
def have_no_preview
34-
have_attributes(io: be_blank, exit_status: 0)
35-
end
33+
if Rails::VERSION::STRING.start_with?('7.1')
34+
let(:expected_custom_path) { "/custom/path\n#{::Rails.root}/test/mailers/previews" }
35+
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews\n#{::Rails.root}/test/mailers/previews" }
36+
37+
def have_no_preview(opts = {})
38+
expected_io =
39+
if opts[:actually_blank]
40+
be_blank
41+
else
42+
"#{::Rails.root}/test/mailers/previews"
43+
end
44+
have_attributes(io: expected_io, exit_status: 0)
45+
end
46+
else
47+
let(:expected_custom_path) { '/custom/path' }
48+
let(:expected_rspec_path) { "#{::Rails.root}/spec/mailers/previews" }
3649

37-
before do
38-
skip("Currently broken for unknown reasons")
50+
def have_no_preview(_opts = {})
51+
have_attributes(io: be_blank, exit_status: 0)
52+
end
3953
end
4054

4155
let(:exec_script) {
@@ -49,9 +63,7 @@ def have_no_preview
4963

5064
it 'sets the preview path to the default rspec path' do
5165
skip "this spec fails singularly on JRuby due to weird env things" if RUBY_ENGINE == "jruby"
52-
expect(capture_exec(custom_env, exec_script)).to eq(
53-
"#{::Rails.root}/spec/mailers/previews"
54-
)
66+
expect(capture_exec(custom_env, exec_script)).to eq(expected_rspec_path)
5567
end
5668

5769
it 'respects the setting from `show_previews`' do
@@ -69,7 +81,7 @@ def have_no_preview
6981
custom_env.merge('CUSTOM_PREVIEW_PATH' => '/custom/path'),
7082
exec_script
7183
)
72-
).to eq('/custom/path')
84+
).to eq(expected_custom_path)
7385
end
7486

7587
it 'allows initializers to set options' do
@@ -87,7 +99,7 @@ def have_no_preview
8799
custom_env.merge('NO_ACTION_MAILER' => 'true'),
88100
exec_script
89101
)
90-
).to have_no_preview
102+
).to have_no_preview(actually_blank: true)
91103
end
92104
end
93105

@@ -102,7 +114,7 @@ def have_no_preview
102114
it 'respects the setting from `show_previews`' do
103115
expect(
104116
capture_exec(custom_env.merge('SHOW_PREVIEWS' => 'true'), exec_script)
105-
).to eq("#{::Rails.root}/spec/mailers/previews")
117+
).to eq(expected_rspec_path)
106118
end
107119

108120
it 'allows initializers to set options' do

lib/rspec-rails.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,18 @@ def config_preview_path?(options)
4747
end
4848
end
4949

50-
def config_default_preview_path(options)
51-
return unless options.preview_path.blank?
50+
if ::Rails::VERSION::STRING >= "7.1.0"
51+
def config_default_preview_path(options)
52+
return unless options.preview_paths.empty?
5253

53-
options.preview_path = "#{::Rails.root}/spec/mailers/previews"
54+
options.preview_paths << "#{::Rails.root}/spec/mailers/previews"
55+
end
56+
else
57+
def config_default_preview_path(options)
58+
return unless options.preview_path.blank?
59+
60+
options.preview_path = "#{::Rails.root}/spec/mailers/previews"
61+
end
5462
end
5563

5664
def supports_action_mailer_previews?(config)

0 commit comments

Comments
 (0)