From 57ddb9d8dab05e0d97df328625779fb4f3e3edec Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Sun, 10 Nov 2024 18:51:55 -0800 Subject: [PATCH 1/2] Add Rails 8 authentication generator --- .../authentication/authentication_generator.rb | 12 ++++++++++++ .../factory_bot/authentication/templates/users.rb | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 lib/generators/factory_bot/authentication/authentication_generator.rb create mode 100644 lib/generators/factory_bot/authentication/templates/users.rb diff --git a/lib/generators/factory_bot/authentication/authentication_generator.rb b/lib/generators/factory_bot/authentication/authentication_generator.rb new file mode 100644 index 00000000..abaae92f --- /dev/null +++ b/lib/generators/factory_bot/authentication/authentication_generator.rb @@ -0,0 +1,12 @@ +require "generators/factory_bot" +require "factory_bot_rails" + +module FactoryBot + module Generators + class AuthenticationGenerator < Base + def create_fixture_file + template "users.rb", "test/factories/users.rb" + end + end + end +end diff --git a/lib/generators/factory_bot/authentication/templates/users.rb b/lib/generators/factory_bot/authentication/templates/users.rb new file mode 100644 index 00000000..5c2ac335 --- /dev/null +++ b/lib/generators/factory_bot/authentication/templates/users.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :user do + email_address { "john@example.com" } + password_digest { "MyString" } + end +end From ada47326a00aabe910714f5c9433ec33a0dc7c0f Mon Sep 17 00:00:00 2001 From: Jerome Dalbert Date: Sat, 8 Feb 2025 13:08:04 -0800 Subject: [PATCH 2/2] Add tentative test --- features/generators.feature | 16 ++++++++++++++++ features/step_definitions/rails_steps.rb | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/features/generators.feature b/features/generators.feature index d0e22c91..e7ddc2e4 100644 --- a/features/generators.feature +++ b/features/generators.feature @@ -90,3 +90,19 @@ Feature: And I run `bundle exec rails generate model User name:string age:integer` with a clean environment Then the output should not contain "test/factories/users.rb" And the output should contain "test/fixtures/users.yml" + + Scenario: The factory_bot_rails authentication generator, coupled with rspec-rails, creates a user factory file + When I add "rspec-rails" with options `github: "rspec/rspec-rails", branch: "main"` as a dependency + And I run `bundle install --verbose` with a clean environment + Then the output should contain "rspec-rails" + And I run `bundle exec rails generate authentication` with a clean environment + Then the output should contain "test/factories/users.rb" + And the file "test/factories/users.rb" should contain exactly: + """ + FactoryBot.define do + factory :user do + email_address { "john@example.com" } + password_digest { "MyString" } + end + end + """ diff --git a/features/step_definitions/rails_steps.rb b/features/step_definitions/rails_steps.rb index 3e63941a..f1274d47 100644 --- a/features/step_definitions/rails_steps.rb +++ b/features/step_definitions/rails_steps.rb @@ -28,6 +28,10 @@ append_to_file("Gemfile", %(gem "#{gem_name}"\n)) end +When(/^I add "([^"]+)" with options `(.+)` as a dependency$/) do |gem_name, options| + append_to_file("Gemfile", %(gem "#{gem_name}", #{options}\n)) +end + When(/^I print out "([^"]*)"$/) do |path| in_current_dir do File.open(path, "r") do |f|