Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading factories in extensions after the last changes in Solidus #169

Merged
merged 1 commit into from
Feb 18, 2021
Merged
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
38 changes: 31 additions & 7 deletions lib/solidus_dev_support/testing_support/factories.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
# frozen_string_literal: true

require 'spree/testing_support/factories'
require 'factory_bot'
begin
require 'spree/testing_support/factory_bot'
rescue LoadError
require 'factory_bot'
require 'spree/testing_support/factories'
end

module SolidusDevSupport
module TestingSupport
module Factories
def self.load_for(*engines)
paths = engines.flat_map do |engine|
engine.root.glob('lib/**/factories.rb')
factories_file_or_folder = engine.root.glob('lib/*/testing_support/factories{,.rb}')

if factories_file_or_folder.size == 2 && using_factory_bot_definition_file_paths?
folder, file = factories_file_or_folder.partition(&:directory?).map(&:first).map { |path| path.to_s.gsub(engine.root.to_s, '') }
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4)
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading
all factories present into #{folder}. You should now safely remove #{file} if it
is only used to load ./factories content.
WARN

engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb')
else
factories_file_or_folder
end.map { |path| path.sub(/.rb\z/, '').to_s }
end

if Spree::TestingSupport.respond_to? :load_all_factories
FactoryBot.definition_file_paths.concat(
paths.map { |path| path.sub(/.rb\z/, '').to_s }
)
if using_factory_bot_definition_file_paths?
FactoryBot.definition_file_paths = [
Spree::TestingSupport::FactoryBot.definition_file_paths,
paths,
].flatten

FactoryBot.reload
else
FactoryBot.find_definitions

paths.each { |path| require path }
end
end

def self.using_factory_bot_definition_file_paths?
defined?(Spree::TestingSupport::FactoryBot) &&
Spree::TestingSupport::FactoryBot.respond_to?(:definition_file_paths)
end
end
end
end