Skip to content

Commit 52f4a2a

Browse files
committed
Remove support for deprecated way of loading factories
The legacy way loads a file from Solidus core that has been deprecated and removed in v4.0. If any extension is still loading factories using require they should change to load_for or will fail.
1 parent 5ec5b6a commit 52f4a2a

File tree

3 files changed

+10
-80
lines changed

3 files changed

+10
-80
lines changed

lib/solidus_dev_support/templates/extension/README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ bundle exec rubocop
4242
```
4343

4444
When testing your application's integration with this extension you may use its factories.
45-
Simply add this require statement to your `spec/spec_helper.rb`:
46-
47-
```ruby
48-
require '<%= file_name %>/testing_support/factories'
49-
```
50-
51-
Or, if you are using `FactoryBot.definition_file_paths`, you can load Solidus core
52-
factories along with this extension's factories using this statement:
45+
You can load Solidus core factories along with this extension's factories using this statement:
5346

5447
```ruby
5548
SolidusDevSupport::TestingSupport::Factories.load_for(<%= class_name %>::Engine)

lib/solidus_dev_support/templates/extension/spec/spec_helper.rb.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ require 'solidus_dev_support/rspec/feature_helper'
1818
# in spec/support/ and its subdirectories.
1919
Dir["#{__dir__}/support/**/*.rb"].sort.each { |f| require f }
2020

21-
# Requires factories defined in lib/<%= file_name %>/testing_support/factories.rb
21+
# Requires factories defined in Solidus core and this extension.
22+
# See: lib/<%= file_name %>/testing_support/factories.rb
2223
SolidusDevSupport::TestingSupport::Factories.load_for(<%= class_name %>::Engine)
2324

2425
RSpec.configure do |config|

lib/solidus_dev_support/testing_support/factories.rb

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,22 @@
11
# frozen_string_literal: true
22

33
require 'factory_bot'
4-
5-
Spree::Deprecation.silence do
6-
require 'spree/testing_support/factories'
7-
puts <<~MSG
8-
We are transitioning to a new way of loading factories for extensions.
9-
Be sure this extension does not load factories using require but uses
10-
the load_for() method in its spec_helper.rb, eg:
11-
12-
SolidusDevSupport::TestingSupport::Factories.load_for(ExtensionName1::Engine, ExtensionName2::Engine)
13-
14-
This will load Solidus Core factories right before the ones defined in
15-
lib/extension_name/testing_support/factories/*_factory.rb or
16-
lib/extension_name/testing_support/factories.rb
17-
18-
This message will be removed when all extensions are updated.
19-
MSG
20-
end
21-
22-
begin
23-
require 'spree/testing_support/factory_bot'
24-
rescue LoadError
25-
# Do nothing, we are testing the extension against an old version of Solidus
26-
end
4+
require 'spree/testing_support/factory_bot'
275

286
module SolidusDevSupport
297
module TestingSupport
308
module Factories
319
def self.load_for(*engines)
3210
paths = engines.flat_map do |engine|
33-
# Check if the extension has a lib/*/factories.rb. If it does, we emit a
34-
# deprecation warning and just use it.
35-
obsolete_factories_file = engine.root.glob('lib/*/factories.rb').first # 'lib/*/factories/*_factory.rb'
36-
if obsolete_factories_file.present?
37-
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4)
38-
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading
39-
all factories present in #{obsolete_factories_file.dirname.to_s.gsub(engine.root.to_s, '')}/testing_support/factories/.
40-
Please move the content of #{obsolete_factories_file.to_s.gsub(engine.root.to_s, '')} to that directory.
41-
WARN
42-
43-
[obsolete_factories_file]
44-
else
45-
# If there are both a lib/*/testing_support/factories.rb and a lib/*/testing_support/factories/,
46-
# we assume that the factories.rb file is only used to load all factories prensent in the directory.
47-
# That file can be removed from the extension, in fact, we ignore it and emit a message asking to
48-
# remove it.
49-
factories_file_or_folder = engine.root.glob('lib/*/testing_support/factories{,.rb}')
50-
if factories_file_or_folder.size == 2
51-
folder, file = factories_file_or_folder.partition(&:directory?).map(&:first).map { |path| path.to_s.gsub(engine.root.to_s, '') }
52-
ActiveSupport::Deprecation.warn <<-WARN.squish, caller(4)
53-
SolidusDevSupport::TestingSupport::Factories.load_for() is automatically loading
54-
all factories present into #{folder}. You should now safely remove #{file} if it
55-
is only used to load ./factories content.
56-
WARN
57-
58-
engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb')
59-
elsif factories_file_or_folder.first.directory?
60-
engine.root.glob('lib/*/testing_support/factories/**/*_factory.rb')
61-
else
62-
factories_file_or_folder
63-
end
64-
end
11+
engine.root.glob('lib/*/testing_support/factories{,.rb}')
6512
end.map { |path| path.sub(/.rb\z/, '').to_s }
6613

67-
if using_factory_bot_definition_file_paths?
68-
FactoryBot.definition_file_paths = [
69-
Spree::TestingSupport::FactoryBot.definition_file_paths,
70-
paths,
71-
].flatten
72-
73-
FactoryBot.reload
74-
else
75-
FactoryBot.find_definitions
76-
77-
paths.each { |path| require path }
78-
end
79-
end
14+
FactoryBot.definition_file_paths = [
15+
Spree::TestingSupport::FactoryBot.definition_file_paths,
16+
paths,
17+
].flatten
8018

81-
def self.using_factory_bot_definition_file_paths?
82-
defined?(Spree::TestingSupport::FactoryBot) &&
83-
Spree::TestingSupport::FactoryBot.respond_to?(:definition_file_paths)
19+
FactoryBot.reload
8420
end
8521
end
8622
end

0 commit comments

Comments
 (0)