From 2e0b47639c4c6ab1a498940619993aa45d0b83c6 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Fri, 12 Oct 2018 16:59:02 -0400 Subject: [PATCH] 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. --- lib/factory_bot.rb | 3 --- lib/factory_bot/configuration.rb | 4 +-- lib/factory_bot/decorator/class_key_hash.rb | 28 --------------------- lib/factory_bot/registry.rb | 4 ++- spec/acceptance/keyed_by_class_spec.rb | 24 ++++-------------- 5 files changed, 9 insertions(+), 54 deletions(-) delete mode 100644 lib/factory_bot/decorator/class_key_hash.rb diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 24f2100..75ed6a2 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -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 diff --git a/lib/factory_bot/configuration.rb b/lib/factory_bot/configuration.rb index 23d276c..67b5817 100644 --- a/lib/factory_bot/configuration.rb +++ b/lib/factory_bot/configuration.rb @@ -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 diff --git a/lib/factory_bot/decorator/class_key_hash.rb b/lib/factory_bot/decorator/class_key_hash.rb deleted file mode 100644 index afcf76e..0000000 --- a/lib/factory_bot/decorator/class_key_hash.rb +++ /dev/null @@ -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 diff --git a/lib/factory_bot/registry.rb b/lib/factory_bot/registry.rb index c7240fd..3a8dc19 100644 --- a/lib/factory_bot/registry.rb +++ b/lib/factory_bot/registry.rb @@ -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 diff --git a/spec/acceptance/keyed_by_class_spec.rb b/spec/acceptance/keyed_by_class_spec.rb index 24fba89..3b7cd81 100644 --- a/spec/acceptance/keyed_by_class_spec.rb +++ b/spec/acceptance/keyed_by_class_spec.rb @@ -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