Skip to content

Commit

Permalink
Fix generator for mailer (#2735)
Browse files Browse the repository at this point in the history
Fix filename produced by the mailer generator.
  • Loading branch information
psantos10 committed Feb 26, 2024
1 parent 94d9ff8 commit 7e31bdf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
8 changes: 4 additions & 4 deletions features/generator_specs/mailer_specs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Feature: Mailer generator spec
create app/views/posts_mailer/show.text.erb
create app/views/posts_mailer/show.html.erb
invoke rspec
create spec/mailers/posts_spec.rb
create spec/mailers/posts_mailer_spec.rb
create spec/fixtures/posts/index
create spec/fixtures/posts/show
create spec/mailers/previews/posts_preview.rb
create spec/mailers/previews/posts_mailer_preview.rb
"""

Scenario: Mailer generator with customized `default-path`
Expand All @@ -36,8 +36,8 @@ Feature: Mailer generator spec
create app/views/posts_mailer/show.text.erb
create app/views/posts_mailer/show.html.erb
invoke rspec
create behaviour/mailers/posts_spec.rb
create behaviour/mailers/posts_mailer_spec.rb
create behaviour/fixtures/posts/index
create behaviour/fixtures/posts/show
create behaviour/mailers/previews/posts_preview.rb
create behaviour/mailers/previews/posts_mailer_preview.rb
"""
6 changes: 4 additions & 2 deletions lib/generators/rspec/mailer/mailer_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class MailerGenerator < Base
argument :actions, type: :array, default: [], banner: "method method"

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

def generate_fixtures_files
Expand All @@ -21,7 +22,8 @@ def generate_fixtures_files
def generate_preview_files
return unless RSpec::Rails::FeatureCheck.has_action_mailer_preview?

template "preview.rb", target_path("mailers/previews", class_path, "#{file_name}_preview.rb")
file_suffix = file_name.end_with?('mailer') ? 'preview.rb' : 'mailer_preview.rb'
template "preview.rb", target_path("mailers/previews", class_path, [file_name, file_suffix].join('_'))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/mailer/templates/preview.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% module_namespacing do -%>
# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %>
class <%= class_name %>Preview < ActionMailer::Preview
class <%= class_name %><%= 'Mailer' unless class_name.end_with?('Mailer') %>Preview < ActionMailer::Preview
<% actions.each do |action| -%>
# Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %>
Expand Down
6 changes: 3 additions & 3 deletions spec/generators/rspec/mailer/mailer_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setup_default_destination

describe 'mailer spec' do
subject(:filename) { file('spec/mailers/posts_spec.rb') }
subject(:filename) { file('spec/mailers/posts_mailer_spec.rb') }

describe 'a spec is created for each action' do
before do
Expand Down Expand Up @@ -66,13 +66,13 @@
run_generator %w[posts index show]
end

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

it "includes the standard boilerplate" do
expect(
filename
).to(
contain(/class PostsPreview < ActionMailer::Preview/)
contain(/class PostsMailerPreview < ActionMailer::Preview/)
.and(contain(/def index/))
.and(contain(/PostsMailer.index/))
.and(contain(/def show/))
Expand Down

0 comments on commit 7e31bdf

Please sign in to comment.