Skip to content

Commit e87af0f

Browse files
committed
Generate mailer files in auth generator only if ActionMailer is used
If an app does not use ActionMailer, then all of the mailer-related code is unnecessary. Also if the mailer files are generated, the PasswordMailer will inherit from an ApplicationMailer that doesn't exist, breaking the application in a way that is not obvious until the mailer is run or the app is deployed to an environment with eager class loading Fixes rails#54501
1 parent dc4f4e0 commit e87af0f

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

railties/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Skip generating mailer-related files in authentication generator if the application does
2+
not use ActionMailer
3+
4+
*Rami Massoud*
5+
16
* Introduce `bin/ci` for running your tests, style checks, and security audits locally or in the cloud.
27

38
The specific steps are defined by a new DSL in `config/ci.rb`.

railties/lib/rails/generators/rails/authentication/authentication_generator.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ def create_authentication_files
2525

2626
template "app/channels/application_cable/connection.rb" if defined?(ActionCable::Engine)
2727

28-
template "app/mailers/passwords_mailer.rb"
28+
if defined?(ActionMailer::Railtie)
29+
template "app/mailers/passwords_mailer.rb"
2930

30-
template "app/views/passwords_mailer/reset.html.erb"
31-
template "app/views/passwords_mailer/reset.text.erb"
31+
template "app/views/passwords_mailer/reset.html.erb"
32+
template "app/views/passwords_mailer/reset.text.erb"
33+
34+
template "test/mailers/previews/passwords_mailer_preview.rb"
35+
end
3236

33-
template "test/mailers/previews/passwords_mailer_preview.rb"
3437
template "test/test_helpers/session_test_helper.rb"
3538
end
3639

railties/lib/rails/generators/rails/authentication/templates/app/controllers/passwords_controller.rb.tt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
class PasswordsController < ApplicationController
22
allow_unauthenticated_access
33
before_action :set_user_by_token, only: %i[ edit update ]
4+
<% if defined?(ActionMailer::Railtie) -%>
45
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_password_path, alert: "Try again later." }
6+
<% end -%>
57

68
def new
79
end
10+
<% if defined?(ActionMailer::Railtie) -%>
811

912
def create
1013
if user = User.find_by(email_address: params[:email_address])
@@ -13,6 +16,7 @@ class PasswordsController < ApplicationController
1316

1417
redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
1518
end
19+
<% end -%>
1620

1721
def edit
1822
end

railties/test/generators/authentication_generator_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ def test_connection_class_skipped_without_action_cable
138138
ActionCable.const_set(:Engine, old_value)
139139
end
140140

141+
def test_authentication_generator_without_action_mailer
142+
old_value = ActionMailer.const_get(:Railtie)
143+
ActionMailer.send(:remove_const, :Railtie)
144+
generator([destination_root])
145+
run_generator_instance
146+
147+
assert_no_file "app/mailers/application_mailer.rb"
148+
assert_no_file "app/mailers/passwords_mailer.rb"
149+
assert_no_file "app/views/passwords_mailer/reset.html.erb"
150+
assert_no_file "app/views/passwords_mailer/reset.text.erb"
151+
assert_no_file "test/mailers/previews/passwords_mailer_preview.rb"
152+
153+
assert_file "app/controllers/passwords_controller.rb" do |content|
154+
assert_no_match(/def create\n end/, content)
155+
assert_no_match(/rate_limit/, content)
156+
end
157+
ensure
158+
ActionCable.const_set(:Railtie, old_value)
159+
end
160+
141161
private
142162
def run_generator_instance
143163
@bundle_commands = []

0 commit comments

Comments
 (0)