Skip to content

Commit ca513ce

Browse files
committed
Add Rails 8 authentication generator
1 parent 6ca3a8f commit ca513ce

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'generators/rspec'
2+
3+
module Rspec
4+
module Generators
5+
# @private
6+
class AuthenticationGenerator < Base
7+
def initialize(args, *options)
8+
args.replace(['User'])
9+
super
10+
end
11+
12+
def create_model_spec
13+
template 'user_spec.rb', target_path('models', 'user_spec.rb')
14+
end
15+
16+
hook_for :fixture_replacement
17+
18+
def create_fixture_file
19+
return if options[:fixture_replacement]
20+
21+
template 'users.yml', target_path('fixtures', 'users.yml')
22+
end
23+
end
24+
end
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe User, <%= type_metatag(:model) %> do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
<%% password_digest = BCrypt::Password.create("password") %>
3+
4+
one:
5+
email_address: [email protected]
6+
password_digest: <%%= password_digest %>
7+
8+
two:
9+
email_address: [email protected]
10+
password_digest: <%%= password_digest %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Generators are not automatically loaded by Rails
2+
require 'generators/rspec/authentication/authentication_generator'
3+
require 'support/generators'
4+
5+
RSpec.describe Rspec::Generators::AuthenticationGenerator, type: :generator do
6+
setup_default_destination
7+
8+
it 'runs both the model and fixture tasks' do
9+
gen = generator
10+
expect(gen).to receive :create_model_spec
11+
expect(gen).to receive :create_fixture_file
12+
gen.invoke_all
13+
end
14+
15+
describe 'the generated files' do
16+
describe 'without fixtures' do
17+
before do
18+
run_generator
19+
end
20+
21+
describe 'the fixtures' do
22+
it "will skip the file" do
23+
expect(File.exist?(file('spec/fixtures/users.yml'))).to be false
24+
end
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)