From cbb5b4f7ddd333308c7c71dc49fe28a8e31aa578 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Tue, 20 Feb 2024 08:51:39 -0500 Subject: [PATCH] Enable class names for factories (#6700) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ☑️ Better allow downstream apps to reuse factories Prior to this commit, a downstream implementation that re-uses a factory cannot change it's inheritance. In my experiments, I am unable to modify a factory's parent via FactoryBot's current implementation. This means that in a Hyku application that has it's own AdminSet, FileSet, and Collection models, I cannot re-use the `:hyrax_admin_set`, `:hyrax_collection`, and `:hyrax_file_set` factories. The problem emerges when I re-use a Hyrax factory that then calls a Hyrax factory (e.g. the permission_template factory can create a `:hyrax_admin_set`). With this commit, I downstream applications can more easily re-use factories. I need the antics of sniffing out ancestry because of Hyrax configurations under test. Namely when we're testing wings the Hyrax.admin_set_model is `"AdminSet"`; which breaks all sorts of things. * ☑️ Remove hyrax/specs/factories In my downstream testing this was creating absolute mayhem with auto-loading. Once I've settled on the pattern, I'll document how to do this. * 💄 endless and ever appeasing of the coppers --- lib/hyrax/specs/factories.rb | 19 ------------------- spec/factories/administrative_sets.rb | 12 +++++++++++- spec/factories/hyrax_collection.rb | 10 +++++++++- spec/factories/hyrax_file_set.rb | 2 -- 4 files changed, 20 insertions(+), 23 deletions(-) delete mode 100644 lib/hyrax/specs/factories.rb diff --git a/lib/hyrax/specs/factories.rb b/lib/hyrax/specs/factories.rb deleted file mode 100644 index 4e0a7419c4..0000000000 --- a/lib/hyrax/specs/factories.rb +++ /dev/null @@ -1,19 +0,0 @@ -# Given that Hyrax is a large Engine; lots of controllers and models, it makes sense to expose the -# factories of the test suite to the downstream application (e.g. Hyku). -# -# That way, we can create extensions of those factories in the downstream application. - -[ - "spec/support/simple_work" -].each do |partial| - require Hyrax::Engine.root.join(partial).to_s -end - -Hyrax::Engine.root.glob("spec/factories/**/*.rb").each do |path| - begin - require path.to_s - rescue FactoryBot::DuplicateDefinitionError => e - # It's alright maybe downstream defined these - Rails.logger.warn(e.message) - end -end diff --git a/spec/factories/administrative_sets.rb b/spec/factories/administrative_sets.rb index 443a51e4a4..34a47d3101 100644 --- a/spec/factories/administrative_sets.rb +++ b/spec/factories/administrative_sets.rb @@ -1,6 +1,16 @@ # frozen_string_literal: true + FactoryBot.define do - factory :hyrax_admin_set, class: 'Hyrax::AdministrativeSet' do + ## + # This factory creates a Valkyrized adminstrative set; by default a Hyrax::AdministrativeSet + # + # Why the antics around the class? Because of the Hyrax needs and potential downstream + # applciation needs. + # + # Downstream applications might implement a different # administrative set and the downstream + # application might leverage other Hyrax factories that create a `:hyrax_admin_set` + # (e.g. `:permission_template`) + factory :hyrax_admin_set, class: (Hyrax.config.admin_set_class < Valkyrie::Resource ? Hyrax.config.admin_set_class : Hyrax::AdministrativeSet) do title { ['My Admin Set'] } transient do diff --git a/spec/factories/hyrax_collection.rb b/spec/factories/hyrax_collection.rb index 08b62c0fa1..9cfddaa5c2 100644 --- a/spec/factories/hyrax_collection.rb +++ b/spec/factories/hyrax_collection.rb @@ -2,8 +2,16 @@ ## # Use this factory for generic Hyrax/HydraWorks Collections in valkyrie. +# +# This factory creates a Valkyrized collection set; by default a Hyrax::PcdmCollection +# +# Why the antics around the class? Because of the Hyrax needs and potential downstream +# applciation needs. +# +# Downstream applications might implement a different collection class and the downstream +# application might leverage other Hyrax factories that create a `:hyrax_collection` FactoryBot.define do - factory :hyrax_collection, class: 'CollectionResource', aliases: [:collection_resource] do + factory :hyrax_collection, class: (Hyrax.config.collection_class < Valkyrie::Resource ? Hyrax.config.collection_class : 'CollectionResource'), aliases: [:collection_resource] do sequence(:title) { |n| ["The Tove Jansson Collection #{n}"] } collection_type_gid { Hyrax::CollectionType.find_or_create_default_collection_type.to_global_id.to_s } diff --git a/spec/factories/hyrax_file_set.rb b/spec/factories/hyrax_file_set.rb index 3269767a68..02fd343597 100644 --- a/spec/factories/hyrax_file_set.rb +++ b/spec/factories/hyrax_file_set.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -## -# Use this factory for generic Hyrax/HydraWorks FileSets in valkyrie. FactoryBot.define do factory :hyrax_file_set, class: 'Hyrax::FileSet' do transient do