From 8205851fabc026968d82f9fbd50b7ae9f2b5b1e1 Mon Sep 17 00:00:00 2001 From: Leo Arnold Date: Fri, 17 Nov 2023 21:51:42 +0100 Subject: [PATCH 1/2] Wrap ActiveRecord configuration into initializer `Rails.application` is not available when the railties are collected, so we move the ActiveRecord configuration into the initializer phase. ``` NoMethodError: undefined method `config' for nil:NilClass (NoMethodError) application.config ^^^^^^^ /project/app/vendor/bundle/ruby/3.2.0/gems/railties-7.0.8/lib/rails.rb:47:in `configuration' /project/app/vendor/bundle/ruby/3.2.0/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:25:in `block in ' ``` --- lib/factory_bot_rails/railtie.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index 5512f23..de539cf 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -21,13 +21,15 @@ class Railtie < Rails::Railtie FactoryBot.definition_file_paths = definition_file_paths end - ActiveSupport.on_load :active_record do - config = Rails.configuration.factory_bot + initializer "factory_bot.reject_primary_key_attributes" do + ActiveSupport.on_load :active_record do + config = Rails.configuration.factory_bot - if config.reject_primary_key_attributes - require "factory_bot_rails/factory_validator/active_record_validator" + if config.reject_primary_key_attributes + require "factory_bot_rails/factory_validator/active_record_validator" - config.validator.add_validator FactoryValidator::ActiveRecordValidator.new + config.validator.add_validator FactoryValidator::ActiveRecordValidator.new + end end end From 618772f76673d9037c3da9b29e685a3584c6a96f Mon Sep 17 00:00:00 2001 From: Adif Sgaid <69080239+adifsgaid@users.noreply.github.com> Date: Thu, 23 Nov 2023 11:17:29 +0100 Subject: [PATCH 2/2] Wrap factory_bot configuration in initializer and add tests --- lib/factory_bot_rails/railtie.rb | 10 +++--- spec/factory_bot_rails/railtie_spec.rb | 48 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/factory_bot_rails/railtie.rb b/lib/factory_bot_rails/railtie.rb index de539cf..e47fbc3 100644 --- a/lib/factory_bot_rails/railtie.rb +++ b/lib/factory_bot_rails/railtie.rb @@ -8,10 +8,12 @@ module FactoryBotRails class Railtie < Rails::Railtie - config.factory_bot = ActiveSupport::OrderedOptions.new - config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths - config.factory_bot.reject_primary_key_attributes = true - config.factory_bot.validator = FactoryBotRails::FactoryValidator.new + initializer "factory_bot.configuration" do + config.factory_bot = ActiveSupport::OrderedOptions.new + config.factory_bot.definition_file_paths = FactoryBot.definition_file_paths + config.factory_bot.reject_primary_key_attributes = true + config.factory_bot.validator = FactoryBotRails::FactoryValidator.new + end initializer "factory_bot.set_fixture_replacement" do Generator.new(config).run diff --git a/spec/factory_bot_rails/railtie_spec.rb b/spec/factory_bot_rails/railtie_spec.rb index 807be70..c927b92 100644 --- a/spec/factory_bot_rails/railtie_spec.rb +++ b/spec/factory_bot_rails/railtie_spec.rb @@ -47,4 +47,52 @@ def wait_for_rails_to_reload sleep 0.01 end end + + describe "initializer 'factory_bot.reject_primary_key_attributes'" do + before do + Rails.application.config.factory_bot.validator = FactoryBotRails::FactoryValidator.new + end + + context "when config.reject_primary_key_attributes is true" do + before do + Rails.application.config.factory_bot.reject_primary_key_attributes = true + allow(Rails.application.config.factory_bot.validator).to receive(:add_validator) + end + + it "adds the ActiveRecordValidator to the validator list" do + run_reject_primary_key_attributes_initializer + + expect(Rails.application.config.factory_bot.validator).to have_received(:add_validator) + end + end + + context "when config.reject_primary_key_attributes is false" do + before do + Rails.application.config.factory_bot.reject_primary_key_attributes = false + allow(Rails.application.config.factory_bot.validator).to receive(:add_validator) + end + + it "does not add the ActiveRecordValidator to the validator list" do + run_reject_primary_key_attributes_initializer + + expect(Rails.application.config.factory_bot.validator).not_to have_received(:add_validator) + end + end + + def run_reject_primary_key_attributes_initializer + initializer = FactoryBotRails::Railtie.initializers.find do |i| + i.name == "factory_bot.reject_primary_key_attributes" + end + initializer.run(Rails.application) + end + end + + describe "initializer order" do + it "runs 'factory_bot.configuration' before any other initializer" do + initializers = FactoryBotRails::Railtie.initializers.map(&:name) + first_initializer = initializers.first + + expect(first_initializer).to eq("factory_bot.configuration") + end + end end