-
Notifications
You must be signed in to change notification settings - Fork 78
Customizing Metadata
Please note that this documentation applies to Sufia 7.
- Override the GenericWork model
- Create a vocabulary
- Create a service to load the vocabulary
- Create a view to display your field
- Update the form so that your new field will display
- Making a default Sufia field non-repeatable
- Make the field searchable
The GenericWork class is generated by default in Sufia, but we want to update the model with our own metadata so we define it in our app to override what Sufia provides. Since Rails finds the class in our local application, it won't load it from Sufia.
# app/models/generic_work.rb
class GenericWork < ActiveFedora::Base
include ::CurationConcerns::WorkBehavior
include ::CurationConcerns::BasicMetadata
include Sufia::WorkBehavior
self.human_readable_type = 'Work'
end
We'll add a select box of university departments, and make it a non-repeatable field. Add the property declaration into the GenericWork
class (and pass a block to make sure the property is indexed in Solr):
property :department, predicate: ::RDF::URI.new("http://lib.my.edu/departments"), multiple: false do |index|
index.as :stored_searchable, :facetable
end
If you'd like the field to be repeatable your block should look like this:
property :department, predicate: ::RDF::URI.new("http://lib.my.edu/departments") do |index|
index.as :stored_searchable, :facetable
end
When you're done the file should look like this:
class GenericWork < ActiveFedora::Base
include ::CurationConcerns::WorkBehavior
include ::CurationConcerns::BasicMetadata
include Sufia::WorkBehavior
self.human_readable_type = 'Work'
property :department, predicate: ::RDF::URI.new("http://lib.my.edu/departments"), multiple: false do |index|
index.as :stored_searchable, :facetable
end
end
## config/authorities/departments.yml
terms:
- id: English
term: English
- id: History
term: History
- id: Latin
term: Latin
- id: Zoology
term: Zoology
# services/departments_services.rb
module DepartmentsService
mattr_accessor :authority
self.authority = Qa::Authorities::Local.subauthority_for('departments')
def self.select_all_options
authority.all.map do |element|
[element[:label], element[:id]]
end
end
def self.label(id)
authority.find(id).fetch('term')
end
end
# records/edit_fields/_department.html.erb
<%= f.input :department, as: :select,
collection: DepartmentsService.select_all_options,
include_blank: true,
item_helper: method(:include_current_value),
input_html: { class: 'form-control' }
%>
# forms/curation_concerns/generic_work_form.rb
module CurationConcerns
class GenericWorkForm < Sufia::Forms::WorkForm
self.model_class = ::GenericWork
self.terms += [:department]
end
end
# forms/curation_concerns/generic_work_form.rb
module CurationConcerns
class GenericWorkForm < Sufia::Forms::WorkForm
self.model_class = ::GenericWork
self.terms += [:department]
# Add following line if you'd like to make your field required
self.required_fields += [:department]
end
end
By default all fields in Sufia are repeatable. If you'd like to change this behavior for a field that Sufia provides out of the box, the title field in this case, you can do the following:
# forms/curation_concerns/generic_work_form.rb
module CurationConcerns
class GenericWorkForm < Sufia::Forms::WorkForm
self.model_class = ::GenericWork
def self.multiple?(field)
if field.to_sym == :title
false
else
super
end
end
# cast title back to multivalued so it will actually deposit
def self.model_attributes(_)
attrs = super
attrs[:title] = Array(attrs[:title]) if attrs[:title]
attrs
end
end
end
Inside app/controllers/catalog_controller.rb, add the following:
config.add_show_field solr_name("department", :stored_searchable), label: "Department"
Add the field to app/models/solr_document.rb
def department
self[Solrizer.solr_name('department')]
end
Add the field to app/presenters/sufia/work_show_presenter.rb
delegate :department, to: :solr_document
Add the field to app/views/curation_concerns/base/_metadata.html.erb
<%= presenter.attribute_to_html(:department) %>
TBD