-
-
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.
Add Rails 8 authentication generator
- Loading branch information
1 parent
6ca3a8f
commit 45a477b
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
require 'generators/rspec' | ||
|
||
module Rspec | ||
module Generators | ||
# @private | ||
class AuthenticationGenerator < Base | ||
attr_accessor :attributes | ||
|
||
def self.source_root | ||
File.expand_path(File.join(File.dirname(__FILE__), '..', 'model', 'templates')) | ||
end | ||
|
||
def initialize(args, *options) | ||
args.replace(['User']) | ||
attributes = %w(email password_digest) | ||
super | ||
end | ||
|
||
def create_model_spec | ||
template_file = target_path('models', 'user_spec.rb') | ||
template 'model_spec.rb', template_file | ||
end | ||
|
||
hook_for :fixture_replacement | ||
|
||
def create_fixture_file | ||
return if options[:fixture_replacement] | ||
|
||
template 'fixtures.yml', target_path('fixtures', 'users.yml') | ||
end | ||
end | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
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,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/posts.yml'))).to be false | ||
end | ||
end | ||
end | ||
end | ||
end |