-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2811 from jeromedalbert/authentication-generator
Add Rails 8 authentication generator
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
lib/generators/rspec/authentication/authentication_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'generators/rspec' | ||
|
||
module Rspec | ||
module Generators | ||
# @private | ||
class AuthenticationGenerator < Base | ||
def initialize(args, *options) | ||
args.replace(['User']) | ||
super | ||
end | ||
|
||
def create_user_spec | ||
template 'user_spec.rb', target_path('models', 'user_spec.rb') | ||
end | ||
|
||
hook_for :fixture_replacement | ||
|
||
def create_fixture_file | ||
return if options[:fixture_replacement] | ||
|
||
template 'users.yml', target_path('fixtures', 'users.yml') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User, <%= type_metatag(:model) %> do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
<%% password_digest = BCrypt::Password.create("password") %> | ||
|
||
one: | ||
email_address: [email protected] | ||
password_digest: <%%= password_digest %> | ||
|
||
two: | ||
email_address: [email protected] | ||
password_digest: <%%= password_digest %> |
34 changes: 34 additions & 0 deletions
34
spec/generators/rspec/authentication/authentication_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generators are not automatically loaded by Rails | ||
require 'generators/rspec/authentication/authentication_generator' | ||
require 'support/generators' | ||
|
||
RSpec.describe Rspec::Generators::AuthenticationGenerator, type: :generator do | ||
setup_default_destination | ||
|
||
it 'runs both the model and fixture tasks' do | ||
gen = generator | ||
expect(gen).to receive :create_user_spec | ||
expect(gen).to receive :create_fixture_file | ||
gen.invoke_all | ||
end | ||
|
||
describe 'the generated files' do | ||
it 'creates the user spec' do | ||
run_generator | ||
|
||
expect(File.exist?(file('spec/models/user_spec.rb'))).to be true | ||
end | ||
|
||
describe 'with fixture replacement' do | ||
before do | ||
run_generator ['--fixture-replacement=factory_bot'] | ||
end | ||
|
||
describe 'the fixtures' do | ||
it "will skip the file" do | ||
expect(File.exist?(file('spec/fixtures/users.yml'))).to be false | ||
end | ||
end | ||
end | ||
end | ||
end |