Skip to content

Commit

Permalink
Add Rails 8 authentication generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedalbert committed Nov 11, 2024
1 parent 6ca3a8f commit ca513ce
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/generators/rspec/authentication/authentication_generator.rb
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_model_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
5 changes: 5 additions & 0 deletions lib/generators/rspec/authentication/templates/user_spec.rb
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
10 changes: 10 additions & 0 deletions lib/generators/rspec/authentication/templates/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
<%% password_digest = BCrypt::Password.create("password") %>

one:
email_address: one@example.com
password_digest: <%%= password_digest %>

two:
email_address: two@example.com
password_digest: <%%= password_digest %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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_model_spec
expect(gen).to receive :create_fixture_file
gen.invoke_all
end

describe 'the generated files' do
describe 'without fixtures' do
before do
run_generator
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

0 comments on commit ca513ce

Please sign in to comment.