Skip to content

Commit

Permalink
Merge pull request #6317 from samvera/resource_form-refactor
Browse files Browse the repository at this point in the history
Enable access of form classes, not just instances
  • Loading branch information
dlpierce authored Sep 14, 2023
2 parents 2c39c3a + b52d5f7 commit 1c4bf79
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
21 changes: 21 additions & 0 deletions app/forms/hyrax/forms/pcdm_object_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
Expand Down
54 changes: 20 additions & 34 deletions app/forms/hyrax/forms/resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down Expand Up @@ -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

##
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/hyrax/work_resource/templates/form.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>)

Expand Down

0 comments on commit 1c4bf79

Please sign in to comment.