Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/factory_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
18 changes: 0 additions & 18 deletions lib/factory_bot/aliases.rb

This file was deleted.

22 changes: 22 additions & 0 deletions lib/factory_bot/core/aliases.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions lib/factory_bot/core/find_definitions.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 0 additions & 25 deletions lib/factory_bot/find_definitions.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/factory_bot/reload.rb

This file was deleted.

7 changes: 0 additions & 7 deletions lib/factory_bot/syntax.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/factory_bot/syntax/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,4 @@ def self.run(block)
end
end
end

extend Syntax::Default
end
10 changes: 0 additions & 10 deletions spec/acceptance/reload_spec.rb

This file was deleted.

11 changes: 11 additions & 0 deletions spec/factory_bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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