Skip to content

Commit 10d09ef

Browse files
committed
Inherit AuthenticationGenerator from ModelGenerator
1 parent 78075a6 commit 10d09ef

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

features/generators.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,21 @@ Feature:
9090
And I run `bundle exec rails generate model User name:string age:integer` with a clean environment
9191
Then the output should not contain "test/factories/users.rb"
9292
And the output should contain "test/fixtures/users.yml"
93+
94+
95+
@rails_8
96+
Scenario: The factory_bot_rails authentication generator, coupled with rspec-rails, creates a user factory file
97+
When I add "rspec-rails" as a dependency
98+
And I run `bundle install --verbose` with a clean environment
99+
Then the output should contain "rspec-rails"
100+
And I run `bundle exec rails generate authentication` with a clean environment
101+
Then the output should contain "spec/factories/users.rb"
102+
And the file "spec/factories/users.rb" should contain exactly:
103+
"""
104+
FactoryBot.define do
105+
factory :user do
106+
email_address { "[email protected]" }
107+
password { "password" }
108+
end
109+
end
110+
"""

features/support/env.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
setup_aruba
1212
end
1313

14+
Before("@rails_8") do
15+
rails_version = Gem::Version.new(
16+
Bundler.load.specs.find { |spec| spec.name == "rails" }&.version.to_s
17+
)
18+
required_version = Gem::Version.new("8.0.0")
19+
20+
if rails_version < required_version
21+
skip_this_scenario("Requires Rails 8.0 or higher (current: #{rails_version})")
22+
end
23+
end
24+
1425
if RUBY_PLATFORM == "java"
1526
Aruba.configure do |config|
1627
config.before_cmd do

gemfiles/rails8.0.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source "https://rubygems.org"
44

55
gem "appraisal"
66
gem "aruba"
7+
gem "bcrypt", "~> 3.1.7"
78
gem "brakeman"
89
gem "byebug"
910
gem "cucumber"

lib/generators/factory_bot/authentication/authentication_generator.rb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
# frozen_string_literal: true
2-
31
require "generators/factory_bot"
2+
require "generators/factory_bot/model/model_generator"
43
require "factory_bot_rails"
5-
require "fileutils"
64

75
module FactoryBot
86
module Generators
9-
class AuthenticationGenerator < Base
10-
def create_users_factory
11-
dir = factories_dir
12-
FileUtils.mkdir_p(dir)
7+
FixedAttribute = Struct.new(:name, :default)
138

14-
target = File.join(dir, "users.rb")
15-
if File.exist?(target)
16-
say_status :skip, "users.rb already exists at #{target}", :yellow
17-
else
18-
template "users.rb", target
19-
say_status :create, target, :green
20-
end
21-
end
9+
class AuthenticationGenerator < ModelGenerator
10+
source_paths << File.join(File.dirname(__FILE__), "../model/templates")
2211

2312
private
2413

25-
def factories_dir
26-
if Dir.exist?(Rails.root.join("spec"))
27-
Rails.root.join("spec", "factories").to_s
28-
else
29-
Rails.root.join("test", "factories").to_s
30-
end
14+
def attributes
15+
[
16+
FixedAttribute.new(:email_address, "[email protected]"),
17+
FixedAttribute.new(:password, "password")
18+
]
3119
end
3220
end
3321
end

0 commit comments

Comments
 (0)