Remove deprecated factory lookup by class (#1212)

Closes #1196

We deprecated looking up factories by class in #877. We introduced
`allow_class_lookup` option so that people could disable the behavior
entirely after fixing the deprecation warning.

In preparation for factory_bot 5, I am removing the deprecation warning
and the `allow_class_lookup` option. It is no longer possible to look
up factories by class. This has also made the ClassKeyHash decorator
unnecessary. The behavior is technically a little different now
that we are using HashWithIndifferentAccess
instead of calling to_sym on the key, but it
should behave identically for any standard,
documented factory_bot usage.
This commit is contained in:
Daniel Colson 2018-10-12 16:59:02 -04:00 committed by GitHub
parent 2995978188
commit 2e0b47639c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 9 additions and 54 deletions

View File

@ -39,7 +39,6 @@ require "factory_bot/find_definitions"
require "factory_bot/reload"
require "factory_bot/decorator"
require "factory_bot/decorator/attribute_hash"
require "factory_bot/decorator/class_key_hash"
require "factory_bot/decorator/disallows_duplicates_registry"
require "factory_bot/decorator/invocation_tracker"
require "factory_bot/decorator/new_constructor"
@ -80,8 +79,6 @@ module FactoryBot
:skip_create,
:initialize_with,
:constructor,
:allow_class_lookup,
:allow_class_lookup=,
:use_parent_strategy,
:use_parent_strategy=,
to: :configuration

View File

@ -3,7 +3,7 @@ module FactoryBot
class Configuration
attr_reader :factories, :sequences, :traits, :strategies, :callback_names
attr_accessor :allow_class_lookup, :use_parent_strategy
attr_accessor :use_parent_strategy
def initialize
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new("Factory"))
@ -13,8 +13,6 @@ module FactoryBot
@callback_names = Set.new
@definition = Definition.new(:configuration)
@allow_class_lookup = true
to_create(&:save!)
initialize_with { new }
end

View File

@ -1,28 +0,0 @@
module FactoryBot
class Decorator
class ClassKeyHash < Decorator
def [](key)
@component[symbolized_key key]
end
def []=(key, value)
@component[symbolized_key key] = value
end
def key?(key)
@component.key? symbolized_key(key)
end
private
def symbolized_key(key)
if key.respond_to?(:to_sym)
key.to_sym
elsif FactoryBot.allow_class_lookup
ActiveSupport::Deprecation.warn "Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryBot.allow_class_lookup = false", caller
key.to_s.underscore.to_sym
end
end
end
end
end

View File

@ -1,3 +1,5 @@
require "active_support/core_ext/hash/indifferent_access"
module FactoryBot
class Registry
include Enumerable
@ -6,7 +8,7 @@ module FactoryBot
def initialize(name)
@name = name
@items = Decorator::ClassKeyHash.new({})
@items = ActiveSupport::HashWithIndifferentAccess.new
end
def clear

View File

@ -5,27 +5,13 @@ describe "finding factories keyed by class instead of symbol" do
end
FactoryBot.define do
factory :user do
name { "John Doe" }
sequence(:email) { |n| "person#{n}@example.com" }
end
factory :user
end
end
context "when deprecated class lookup if allowed", :silence_deprecation do
it "allows interaction based on class name" do
user = FactoryBot.create User, email: "person@example.com"
expect(user.email).to eq "person@example.com"
expect(user.name).to eq "John Doe"
end
end
context "when class lookup is disallowed" do
it "doesn't find the factory" do
FactoryBot.allow_class_lookup = false
expect { FactoryBot.create(User) }.to(
raise_error(ArgumentError, "Factory not registered: User"),
)
end
it "doesn't find the factory" do
expect { FactoryBot.create(User) }.to(
raise_error(ArgumentError, "Factory not registered: User"),
)
end
end