diff --git a/app/forms/hyrax/forms/pcdm_object_form.rb b/app/forms/hyrax/forms/pcdm_object_form.rb index 272420f18a..0a5c2de100 100644 --- a/app/forms/hyrax/forms/pcdm_object_form.rb +++ b/app/forms/hyrax/forms/pcdm_object_form.rb @@ -2,6 +2,27 @@ module Hyrax module Forms + ## + # @api public + # + # @example defining a form class using HydraEditor-like configuration + # class MonographForm < Hyrax::Forms::PcdmObjectForm(Monograph) + # self.required_fields = [:title, :creator, :rights_statement] + # # other WorkForm-like configuration here + # end + def self.PcdmObjectForm(work_class) + Class.new(Hyrax::Forms::PcdmObjectForm) do + self.model_class = work_class + + ## + # @return [String] + def self.inspect + return "Hyrax::Forms::PcdmObjectForm(#{model_class})" if name.blank? + super + end + end + end + ## # A form for PCDM objects: resources which have collection relationships and # generally resemble +Hyrax::Work+. diff --git a/app/forms/hyrax/forms/resource_form.rb b/app/forms/hyrax/forms/resource_form.rb index f2e8beb7c7..ceaf2167c5 100644 --- a/app/forms/hyrax/forms/resource_form.rb +++ b/app/forms/hyrax/forms/resource_form.rb @@ -5,29 +5,26 @@ module Forms ## # @api public # - # @example defining a form class using HydraEditor-like configuration - # class MonographForm < Hyrax::Forms::ResourceForm(Monograph) - # self.required_fields = [:title, :creator, :rights_statement] - # # other WorkForm-like configuration here - # end + # Returns the form class associated with a given model. # - # @note The returned class will extend +Hyrax::Forms::PcdmObjectForm+, not - # only +Hyrax::Forms::ResourceForm+. This is for backwards‐compatibility - # with existing Hyrax instances and satisfies the expected general use - # case (building forms for various PCDM object classes), but is *not* - # necessarily suitable for other kinds of Hyrax resource, like - # +Hyrax::FileSet+s. - def self.ResourceForm(work_class) - Class.new(Hyrax::Forms::PcdmObjectForm) do - self.model_class = work_class - - ## - # @return [String] - def self.inspect - return "Hyrax::Forms::ResourceForm(#{model_class})" if name.blank? - super + # @note The default case assumes that the provided model class is for a + # PCDM object and returns a +Hyrax::Forms::PcdmObjectForm+. This is for + # backwards‐compatibility with existing Hyrax instances. However, a + # different +Hyrax::Forms::ResourceForm+ subclass will be returned in + # some known cases where that is preferable. + def self.ResourceForm(model_class) + @resource_forms ||= {}.compare_by_identity + @resource_forms[model_class] ||= + if model_class <= Hyrax::AdministrativeSet + Hyrax::Forms::AdministrativeSetForm + elsif model_class <= Hyrax::FileSet + Hyrax::Forms::FileSetForm + elsif model_class <= Hyrax::PcdmCollection + Hyrax::Forms::PcdmCollectionForm + else + "Hyrax::Forms::PcdmObjectForm".constantize # autoload + Hyrax::Forms::PcdmObjectForm(model_class) end - end end ## @@ -122,19 +119,8 @@ class << self # change_set = Hyrax::Forms::ResourceForm.for(monograph) def for(resource) klass = "#{resource.class.name}Form".safe_constantize - - return klass.new(resource) if klass - case resource - when Hyrax::AdministrativeSet - Hyrax::Forms::AdministrativeSetForm.new(resource) - when Hyrax::FileSet - Hyrax::Forms::FileSetForm.new(resource) - when Hyrax::PcdmCollection - Hyrax::Forms::PcdmCollectionForm.new(resource) - else - # NOTE: This will create a +Hyrax::Forms::PcdmObjectForm+. - Hyrax::Forms::ResourceForm(resource.class).new(resource) - end + klass ||= Hyrax::Forms::ResourceForm(resource.class) + klass.new(resource) end ## diff --git a/lib/generators/hyrax/work_resource/templates/form.rb.erb b/lib/generators/hyrax/work_resource/templates/form.rb.erb index b621f77cb2..309e2196f8 100644 --- a/lib/generators/hyrax/work_resource/templates/form.rb.erb +++ b/lib/generators/hyrax/work_resource/templates/form.rb.erb @@ -5,7 +5,7 @@ # # @see https://github.com/samvera/hyrax/wiki/Hyrax-Valkyrie-Usage-Guide#forms # @see https://github.com/samvera/valkyrie/wiki/ChangeSets-and-Dirty-Tracking -class <%= class_name %>Form < Hyrax::Forms::ResourceForm(<%= class_name %>) +class <%= class_name %>Form < Hyrax::Forms::PcdmObjectForm(<%= class_name %>) include Hyrax::FormFields(:basic_metadata) include Hyrax::FormFields(:<%= file_name %>)