Skip to content

Commit eaa4052

Browse files
authored
Authentication generator supports an --api flag (rails#52442)
and the sessions view template has been moved into an erb generator so that gems like tailwindcss-rails can provide a specialized template.
1 parent 4294d71 commit eaa4052

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/generators/erb"
4+
5+
module Erb # :nodoc:
6+
module Generators # :nodoc:
7+
class AuthenticationGenerator < Rails::Generators::Base # :nodoc:
8+
def create_files
9+
template "views/sessions/new.html.erb", File.join("app/views/sessions/new.html.erb")
10+
end
11+
end
12+
end
13+
end

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
module Rails
44
module Generators
55
class AuthenticationGenerator < Base # :nodoc:
6+
class_option :api, type: :boolean,
7+
desc: "Generate API-only controllers and models, with no view templates"
8+
9+
hook_for :template_engine, as: :authentication do |template_engine|
10+
invoke template_engine unless options.api?
11+
end
12+
613
def create_files
714
template "models/session.rb", File.join("app/models/session.rb")
815
template "models/user.rb", File.join("app/models/user.rb")
916
template "models/current.rb", File.join("app/models/current.rb")
1017

1118
template "controllers/sessions_controller.rb", File.join("app/controllers/sessions_controller.rb")
1219
template "controllers/concerns/authentication.rb", File.join("app/controllers/concerns/authentication.rb")
13-
14-
template "views/sessions/new.html.erb", File.join("app/views/sessions/new.html.erb")
1520
end
1621

1722
def configure_application

railties/test/generators/authentication_generator_test.rb

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ class AuthenticationGeneratorTest < Rails::Generators::TestCase
1010
def setup
1111
Rails.application = TestApp::Application
1212
Rails.application.config.root = Pathname(destination_root)
13+
14+
self.class.tests Rails::Generators::AppGenerator
15+
run_generator([destination_root])
16+
17+
self.class.tests Rails::Generators::AuthenticationGenerator
1318
end
1419

1520
def teardown
1621
Rails.application = Rails.application.instance
1722
end
1823

1924
def test_authentication_generator
20-
self.class.tests Rails::Generators::AppGenerator
21-
run_generator([destination_root])
22-
23-
self.class.tests Rails::Generators::AuthenticationGenerator
2425
run_generator
2526

2627
assert_file "app/models/user.rb"
@@ -50,4 +51,35 @@ def test_authentication_generator
5051
assert_match(/t.string :password_digest, null: false/, content)
5152
end
5253
end
54+
55+
def test_authentication_generator_with_api_flag
56+
run_generator(["--api"])
57+
58+
assert_file "app/models/user.rb"
59+
assert_file "app/models/current.rb"
60+
assert_file "app/models/session.rb"
61+
assert_file "app/controllers/sessions_controller.rb"
62+
assert_file "app/controllers/concerns/authentication.rb"
63+
assert_no_file "app/views/sessions/new.html.erb"
64+
65+
assert_file "app/controllers/application_controller.rb" do |content|
66+
assert_match(/include Authentication/, content)
67+
end
68+
69+
assert_file "Gemfile" do |content|
70+
assert_match(/\ngem "bcrypt"/, content)
71+
end
72+
73+
assert_file "config/routes.rb" do |content|
74+
assert_match(/resource :session/, content)
75+
end
76+
77+
assert_migration "db/migrate/create_sessions.rb" do |content|
78+
assert_match(/t.references :user, null: false, foreign_key: true/, content)
79+
end
80+
81+
assert_migration "db/migrate/create_users.rb" do |content|
82+
assert_match(/t.string :password_digest, null: false/, content)
83+
end
84+
end
5385
end

0 commit comments

Comments
 (0)