Skip to content

Commit 7e31bdf

Browse files
authored
Fix generator for mailer (#2735)
Fix filename produced by the mailer generator.
1 parent 94d9ff8 commit 7e31bdf

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

features/generator_specs/mailer_specs.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Feature: Mailer generator spec
1313
create app/views/posts_mailer/show.text.erb
1414
create app/views/posts_mailer/show.html.erb
1515
invoke rspec
16-
create spec/mailers/posts_spec.rb
16+
create spec/mailers/posts_mailer_spec.rb
1717
create spec/fixtures/posts/index
1818
create spec/fixtures/posts/show
19-
create spec/mailers/previews/posts_preview.rb
19+
create spec/mailers/previews/posts_mailer_preview.rb
2020
"""
2121

2222
Scenario: Mailer generator with customized `default-path`
@@ -36,8 +36,8 @@ Feature: Mailer generator spec
3636
create app/views/posts_mailer/show.text.erb
3737
create app/views/posts_mailer/show.html.erb
3838
invoke rspec
39-
create behaviour/mailers/posts_spec.rb
39+
create behaviour/mailers/posts_mailer_spec.rb
4040
create behaviour/fixtures/posts/index
4141
create behaviour/fixtures/posts/show
42-
create behaviour/mailers/previews/posts_preview.rb
42+
create behaviour/mailers/previews/posts_mailer_preview.rb
4343
"""

lib/generators/rspec/mailer/mailer_generator.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class MailerGenerator < Base
88
argument :actions, type: :array, default: [], banner: "method method"
99

1010
def generate_mailer_spec
11-
template "mailer_spec.rb", target_path('mailers', class_path, "#{file_name}_spec.rb")
11+
file_suffix = file_name.end_with?('mailer') ? 'spec.rb' : 'mailer_spec.rb'
12+
template "mailer_spec.rb", target_path('mailers', class_path, [file_name, file_suffix].join('_'))
1213
end
1314

1415
def generate_fixtures_files
@@ -21,7 +22,8 @@ def generate_fixtures_files
2122
def generate_preview_files
2223
return unless RSpec::Rails::FeatureCheck.has_action_mailer_preview?
2324

24-
template "preview.rb", target_path("mailers/previews", class_path, "#{file_name}_preview.rb")
25+
file_suffix = file_name.end_with?('mailer') ? 'preview.rb' : 'mailer_preview.rb'
26+
template "preview.rb", target_path("mailers/previews", class_path, [file_name, file_suffix].join('_'))
2527
end
2628
end
2729
end

lib/generators/rspec/mailer/templates/preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<% module_namespacing do -%>
22
# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %>
3-
class <%= class_name %>Preview < ActionMailer::Preview
3+
class <%= class_name %><%= 'Mailer' unless class_name.end_with?('Mailer') %>Preview < ActionMailer::Preview
44
<% actions.each do |action| -%>
55
66
# Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %>

spec/generators/rspec/mailer/mailer_generator_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
setup_default_destination
77

88
describe 'mailer spec' do
9-
subject(:filename) { file('spec/mailers/posts_spec.rb') }
9+
subject(:filename) { file('spec/mailers/posts_mailer_spec.rb') }
1010

1111
describe 'a spec is created for each action' do
1212
before do
@@ -66,13 +66,13 @@
6666
run_generator %w[posts index show]
6767
end
6868

69-
subject(:filename) { file('spec/mailers/previews/posts_preview.rb') }
69+
subject(:filename) { file('spec/mailers/previews/posts_mailer_preview.rb') }
7070

7171
it "includes the standard boilerplate" do
7272
expect(
7373
filename
7474
).to(
75-
contain(/class PostsPreview < ActionMailer::Preview/)
75+
contain(/class PostsMailerPreview < ActionMailer::Preview/)
7676
.and(contain(/def index/))
7777
.and(contain(/PostsMailer.index/))
7878
.and(contain(/def show/))

0 commit comments

Comments
 (0)