diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index de541d0e6..45de94c14 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/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" require "factory_bot/decorator" require "factory_bot/decorator/attribute_hash" require "factory_bot/decorator/disallows_duplicates_registry" @@ -51,6 +51,10 @@ require "factory_bot/version" module FactoryBot + extend Core::Aliases + extend Core::FindDefinitions + extend Syntax::Default + Deprecation = ActiveSupport::Deprecation.new("7.0", "factory_bot") mattr_accessor :use_parent_strategy, instance_accessor: false @@ -117,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/aliases.rb b/lib/factory_bot/aliases.rb deleted file mode 100644 index a14e645a8..000000000 --- a/lib/factory_bot/aliases.rb +++ /dev/null @@ -1,18 +0,0 @@ -module FactoryBot - class << self - attr_accessor :aliases - end - - self.aliases = [ - [/(.+)_id/, '\1'], - [/(.*)/, '\1_id'] - ] - - def self.aliases_for(attribute) - aliases.map { |(pattern, replace)| - if pattern.match?(attribute) - attribute.to_s.sub(pattern, replace).to_sym - end - }.compact << attribute - end -end diff --git a/lib/factory_bot/core/aliases.rb b/lib/factory_bot/core/aliases.rb new file mode 100644 index 000000000..ed9c1dd71 --- /dev/null +++ b/lib/factory_bot/core/aliases.rb @@ -0,0 +1,22 @@ +module FactoryBot + module Core + module Aliases + attr_writer :aliases + + def aliases + @aliases ||= [ + [/(.+)_id/, '\1'], + [/(.*)/, '\1_id'] + ] + end + + def aliases_for(attribute) + aliases.map { |(pattern, replace)| + if pattern.match?(attribute) + attribute.to_s.sub(pattern, replace).to_sym + end + }.compact << attribute + end + end + end +end diff --git a/lib/factory_bot/core/find_definitions.rb b/lib/factory_bot/core/find_definitions.rb new file mode 100644 index 000000000..5991d8483 --- /dev/null +++ b/lib/factory_bot/core/find_definitions.rb @@ -0,0 +1,29 @@ +module FactoryBot + 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 + + def definition_file_paths + @definition_file_paths ||= %w[factories test/factories spec/factories] + end + + 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") + + if File.directory? path + Dir[File.join(path, "**", "*.rb")].sort.each do |file| + load file + end + end + end + end + end + end +end diff --git a/lib/factory_bot/find_definitions.rb b/lib/factory_bot/find_definitions.rb deleted file mode 100644 index a56204f6f..000000000 --- a/lib/factory_bot/find_definitions.rb +++ /dev/null @@ -1,25 +0,0 @@ -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 - - self.definition_file_paths = %w[factories test/factories spec/factories] - - def self.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") - - if File.directory? path - Dir[File.join(path, "**", "*.rb")].sort.each do |file| - load file - end - end - end - end -end 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/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 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 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/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 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 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