From 97257ae2b63b2b996963b6577e6f264899f1b200 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 1/7] refactor: make it more explicit that aliases.rb extends FactoryBot --- lib/factory_bot.rb | 2 ++ lib/factory_bot/aliases.rb | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index de541d0e6..62dfbc8dc 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -51,6 +51,8 @@ require "factory_bot/version" module FactoryBot + extend Core::Aliases + Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot") mattr_accessor :use_parent_strategy, instance_accessor: false diff --git a/lib/factory_bot/aliases.rb b/lib/factory_bot/aliases.rb index a14e645a8..ed9c1dd71 100644 --- a/lib/factory_bot/aliases.rb +++ b/lib/factory_bot/aliases.rb @@ -1,18 +1,22 @@ module FactoryBot - class << self - attr_accessor :aliases - end + module Core + module Aliases + attr_writer :aliases - self.aliases = [ - [/(.+)_id/, '\1'], - [/(.*)/, '\1_id'] - ] + def aliases + @aliases ||= [ + [/(.+)_id/, '\1'], + [/(.*)/, '\1_id'] + ] + end - def self.aliases_for(attribute) - aliases.map { |(pattern, replace)| - if pattern.match?(attribute) - attribute.to_s.sub(pattern, replace).to_sym + def aliases_for(attribute) + aliases.map { |(pattern, replace)| + if pattern.match?(attribute) + attribute.to_s.sub(pattern, replace).to_sym + end + }.compact << attribute end - }.compact << attribute + end end end From 339c0504ed160381cf24464c672e290929b49772 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 2/7] rebase: move files --- lib/factory_bot.rb | 2 +- lib/factory_bot/{ => core}/aliases.rb | 0 spec/factory_bot/{ => core}/aliases_spec.rb | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename lib/factory_bot/{ => core}/aliases.rb (100%) rename spec/factory_bot/{ => core}/aliases_spec.rb (100%) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 62dfbc8dc..60da7e337 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -34,7 +34,7 @@ require "factory_bot/attribute_list" require "factory_bot/trait" require "factory_bot/enum" -require "factory_bot/aliases" +require "factory_bot/core/aliases" require "factory_bot/definition" require "factory_bot/definition_proxy" require "factory_bot/syntax" diff --git a/lib/factory_bot/aliases.rb b/lib/factory_bot/core/aliases.rb similarity index 100% rename from lib/factory_bot/aliases.rb rename to lib/factory_bot/core/aliases.rb diff --git a/spec/factory_bot/aliases_spec.rb b/spec/factory_bot/core/aliases_spec.rb similarity index 100% rename from spec/factory_bot/aliases_spec.rb rename to spec/factory_bot/core/aliases_spec.rb From 4b939200cc821059835eb92231fda369c6391c01 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 3/7] refactor: remove unnecessary file --- lib/factory_bot.rb | 3 ++- lib/factory_bot/syntax.rb | 7 ------- 2 files changed, 2 insertions(+), 8 deletions(-) delete mode 100644 lib/factory_bot/syntax.rb diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 60da7e337..884d5400f 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -37,7 +37,8 @@ require "factory_bot/core/aliases" require "factory_bot/definition" require "factory_bot/definition_proxy" -require "factory_bot/syntax" +require "factory_bot/syntax/methods" +require "factory_bot/syntax/default" require "factory_bot/syntax_runner" require "factory_bot/find_definitions" require "factory_bot/reload" diff --git a/lib/factory_bot/syntax.rb b/lib/factory_bot/syntax.rb deleted file mode 100644 index 26c93828e..000000000 --- a/lib/factory_bot/syntax.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "factory_bot/syntax/methods" -require "factory_bot/syntax/default" - -module FactoryBot - module Syntax - end -end From d8181e987fcc9ca921aff07430ca8a1b2a960d9c Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 4/7] refactor: make it more explicit that find_definitions extends FactoryBot --- lib/factory_bot.rb | 1 + lib/factory_bot/find_definitions.rb | 34 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 884d5400f..a3379067e 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -53,6 +53,7 @@ module FactoryBot extend Core::Aliases + extend Core::FindDefinitions Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot") diff --git a/lib/factory_bot/find_definitions.rb b/lib/factory_bot/find_definitions.rb index a56204f6f..5991d8483 100644 --- a/lib/factory_bot/find_definitions.rb +++ b/lib/factory_bot/find_definitions.rb @@ -1,23 +1,27 @@ module FactoryBot - class << self - # An Array of strings specifying locations that should be searched for - # factory definitions. By default, factory_bot will attempt to require - # "factories.rb", "factories/**/*.rb", "test/factories.rb", - # "test/factories/**.rb", "spec/factories.rb", and "spec/factories/**.rb". - attr_accessor :definition_file_paths - end + module Core + module FindDefinitions + # An Array of strings specifying locations that should be searched for + # factory definitions. By default, factory_bot will attempt to require + # "factories.rb", "factories/**/*.rb", "test/factories.rb", + # "test/factories/**.rb", "spec/factories.rb", and "spec/factories/**.rb". + attr_writer :definition_file_paths - self.definition_file_paths = %w[factories test/factories spec/factories] + def definition_file_paths + @definition_file_paths ||= %w[factories test/factories spec/factories] + end - def self.find_definitions - absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) } + def find_definitions + absolute_definition_file_paths = definition_file_paths.map { |path| File.expand_path(path) } - absolute_definition_file_paths.uniq.each do |path| - load("#{path}.rb") if File.exist?("#{path}.rb") + absolute_definition_file_paths.uniq.each do |path| + load("#{path}.rb") if File.exist?("#{path}.rb") - if File.directory? path - Dir[File.join(path, "**", "*.rb")].sort.each do |file| - load file + if File.directory? path + Dir[File.join(path, "**", "*.rb")].sort.each do |file| + load file + end + end end end end From 41f12649e26682607732ff4a2578b81329b0e4f8 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 5/7] refactor: move files --- lib/factory_bot.rb | 4 ++-- lib/factory_bot/{ => core}/find_definitions.rb | 0 spec/factory_bot/{ => core}/find_definitions_spec.rb | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename lib/factory_bot/{ => core}/find_definitions.rb (100%) rename spec/factory_bot/{ => core}/find_definitions_spec.rb (100%) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index a3379067e..7b7223bfc 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -5,6 +5,8 @@ require "active_support/deprecation" require "active_support/notifications" +require "factory_bot/core/aliases" +require "factory_bot/core/find_definitions" require "factory_bot/internal" require "factory_bot/definition_hierarchy" require "factory_bot/configuration" @@ -34,13 +36,11 @@ require "factory_bot/attribute_list" require "factory_bot/trait" require "factory_bot/enum" -require "factory_bot/core/aliases" require "factory_bot/definition" require "factory_bot/definition_proxy" require "factory_bot/syntax/methods" require "factory_bot/syntax/default" require "factory_bot/syntax_runner" -require "factory_bot/find_definitions" require "factory_bot/reload" require "factory_bot/decorator" require "factory_bot/decorator/attribute_hash" diff --git a/lib/factory_bot/find_definitions.rb b/lib/factory_bot/core/find_definitions.rb similarity index 100% rename from lib/factory_bot/find_definitions.rb rename to lib/factory_bot/core/find_definitions.rb diff --git a/spec/factory_bot/find_definitions_spec.rb b/spec/factory_bot/core/find_definitions_spec.rb similarity index 100% rename from spec/factory_bot/find_definitions_spec.rb rename to spec/factory_bot/core/find_definitions_spec.rb From af8fd14954062bda3d9adfc7ffecf5f6d464e177 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 6/7] refactor: defrag FactoryBot.reload --- lib/factory_bot.rb | 6 ++++++ lib/factory_bot/reload.rb | 7 ------- spec/acceptance/reload_spec.rb | 10 ---------- spec/factory_bot_spec.rb | 11 +++++++++++ 4 files changed, 17 insertions(+), 17 deletions(-) delete mode 100644 lib/factory_bot/reload.rb delete mode 100644 spec/acceptance/reload_spec.rb diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 7b7223bfc..3a62f3fbb 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -121,6 +121,12 @@ class << self :strategy_by_name, to: Internal end + + def self.reload + Internal.reset_configuration + Internal.register_default_strategies + find_definitions + end end FactoryBot::Internal.register_default_strategies diff --git a/lib/factory_bot/reload.rb b/lib/factory_bot/reload.rb deleted file mode 100644 index 3ef871943..000000000 --- a/lib/factory_bot/reload.rb +++ /dev/null @@ -1,7 +0,0 @@ -module FactoryBot - def self.reload - Internal.reset_configuration - Internal.register_default_strategies - find_definitions - end -end diff --git a/spec/acceptance/reload_spec.rb b/spec/acceptance/reload_spec.rb deleted file mode 100644 index a09a25729..000000000 --- a/spec/acceptance/reload_spec.rb +++ /dev/null @@ -1,10 +0,0 @@ -describe "reload" do - it "does not reset the value of use_parent_strategy" do - custom_strategy = :custom_use_parent_strategy_value - - with_temporary_assignment(FactoryBot, :use_parent_strategy, custom_strategy) do - FactoryBot.reload - expect(FactoryBot.use_parent_strategy).to eq custom_strategy - end - end -end diff --git a/spec/factory_bot_spec.rb b/spec/factory_bot_spec.rb index 5da9f2dd7..f166df960 100644 --- a/spec/factory_bot_spec.rb +++ b/spec/factory_bot_spec.rb @@ -10,4 +10,15 @@ expect(FactoryBot.use_parent_strategy).to be true end end + + describe ".reload" do + it "does not reset the value of use_parent_strategy" do + custom_strategy = :custom_use_parent_strategy_value + + with_temporary_assignment(FactoryBot, :use_parent_strategy, custom_strategy) do + FactoryBot.reload + expect(FactoryBot.use_parent_strategy).to eq custom_strategy + end + end + end end From e15475f9d639ac648c9618486f97ae567580bb73 Mon Sep 17 00:00:00 2001 From: Valerie Burzynski Date: Tue, 30 Sep 2025 12:29:53 -0500 Subject: [PATCH 7/7] refactor: make it more explicit that FactoryBot is extended by Syntax::Default --- lib/factory_bot.rb | 2 +- lib/factory_bot/syntax/default.rb | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 3a62f3fbb..45de94c14 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -41,7 +41,6 @@ require "factory_bot/syntax/methods" require "factory_bot/syntax/default" require "factory_bot/syntax_runner" -require "factory_bot/reload" require "factory_bot/decorator" require "factory_bot/decorator/attribute_hash" require "factory_bot/decorator/disallows_duplicates_registry" @@ -54,6 +53,7 @@ module FactoryBot extend Core::Aliases extend Core::FindDefinitions + extend Syntax::Default Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot") diff --git a/lib/factory_bot/syntax/default.rb b/lib/factory_bot/syntax/default.rb index 66f4d2c73..0f7bbe34d 100644 --- a/lib/factory_bot/syntax/default.rb +++ b/lib/factory_bot/syntax/default.rb @@ -59,6 +59,4 @@ def self.run(block) end end end - - extend Syntax::Default end