-
Notifications
You must be signed in to change notification settings - Fork 78
Customizing Metadata (Sufia 6)
Please note that this documentation applies to Sufia 6. See Customizing Metadata for the latest guide.
The GenericFile class is provided by 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_file.rb
class GenericFile < ActiveFedora::Base
include Sufia::GenericFile
end
Add the property declaration into the GenericFile
class (and pass a block to make sure the property is indexed in Solr):
property :alternative, predicate: ::RDF::DC.alternative do |index|
index.as :stored_searchable, :facetable
end
When you're done the file should look like this:
# app/models/generic_file.rb
class GenericFile < ActiveFedora::Base
include Sufia::GenericFile
property :alternative, predicate: ::RDF::DC.alternative do |index|
index.as :stored_searchable, :facetable
end
end
By default, properties are repeatable. If you want the property to be single-valued rather than repeatable, then you would add multiple: false
to its property definition, as follows:
property :alternative, predicate: ::RDF::DC.alternative, multiple: false do |index|
index.as :stored_searchable, :facetable
end
You can try using the new property at the rails console like this. I've shown the expected output after each command.
$ rails c
Loading development environment (Rails 4.1.8)
irb(main):001:0> f = GenericFile.new(id: 'test-1')
LocalAuthority Load (1.2ms) SELECT "local_authorities".* FROM "local_authorities" WHERE "local_authorities"."name" = 'lc_subjects' LIMIT 1
LocalAuthority Load (1.4ms) SELECT "local_authorities".* FROM "local_authorities" WHERE "local_authorities"."name" = 'lexvo_languages' LIMIT 1
LocalAuthority Load (1.3ms) SELECT "local_authorities".* FROM "local_authorities" WHERE "local_authorities"."name" = 'lc_genres' LIMIT 1
ActiveFedora: loading fedora config from /opt/sufia/config/fedora.yml
ActiveFedora: loading solr config from /opt/sufia/config/solr.yml
Attempted to init base path `dev`, but it already exists
=> #<GenericFile id: "test-1", mime_type: "", format_label: [""], file_size: [""], last_modified: [""], filename: [""], original_checksum: [""], rights_basis: [], copyright_basis: [], copyright_note: [], well_formed: [], valid: [""], status_message: [], file_title: [""], file_author: [""], page_count: [""], file_language: [], word_count: [], character_count: [], paragraph_count: [], line_count: [], table_count: [], graphics_count: [], byte_order: [], compression: [], color_space: [], profile_name: [], profile_version: [], orientation: [], color_map: [], image_producer: [], capture_device: [], scanning_software: [], exif_version: [], gps_timestamp: [], latitude: [], longitude: [], character_set: [], markup_basis: [], markup_language: [], bit_depth: [], channels: [], data_format: [], offset: [], frame_rate: [], label: nil, depositor: nil, relative_path: nil, import_url: nil, part_of: [], resource_type: [], title: [], creator: [], contributor: [], description: [], tag: [], rights: [], publisher: [], date_created: [], date_uploaded: nil, date_modified: nil, subject: [], language: [], identifier: [], based_near: [], related_url: [], bibliographic_citation: [], source: [], proxy_depositor: nil, on_behalf_of: nil, alternative: [], batch_id: nil>
irb(main):002:0> f.alternative = ["An alternative title"]
=> ["An alternative title"]
irb(main):003:0> puts f.resource.dump(:ttl)
<http://127.0.0.1:8983/fedora/rest/dev/te/st/-1/test-1> <http://purl.org/dc/terms/alternative> "An alternative title";
<info:fedora/fedora-system:def/model#hasModel> "GenericFile" .
=> nil
irb(main):004:0>
Sufia provides default presenter classes to control what properties of a model are display in your app, and in what order they appear. We create a subclass of Sufia's presenter adding alternative
to the list of terms.
# app/presenters/my_generic_file_presenter.rb
class MyGenericFilePresenter < Sufia::GenericFilePresenter
self.terms = [:resource_type, :title, :creator, :contributor, :description,
:tag, :rights, :publisher, :date_created, :subject, :language,
:identifier, :based_near, :related_url, :alternative]
end
Now we override Sufia's GenericFilesController so that it uses the MyGenericFilePresenter class that we defined rather than the default presenter in Sufia.
# app/controllers/generic_files_controller.rb
class GenericFilesController < ApplicationController
include Sufia::Controller
include Sufia::FilesControllerBehavior
self.presenter_class = MyGenericFilePresenter
end
The fields show up in the show view of our app, but we also want them on our edit form too. Let's create an edit form that has our new field.
Sufia also provides default form classes to control what properties of a model are editable in your app, and in what order they appear. Form classes extend presenter classes to include permissions and required fields, so we can base this work on what we already did with MyGenericFilePresenter
. We subclass our own custom presenter to pick up the changes we already made there.
# app/forms/my_file_edit_form.rb
class MyFileEditForm < MyGenericFilePresenter
include HydraEditor::Form
include HydraEditor::Form::Permissions
end
On initial upload, Sufia allows users to edit metadata in bulk using a batch edit form, so we need to create a second form to be used by the BatchController
.
# app/forms/my_batch_edit_form.rb
class MyBatchEditForm < MyFileEditForm
end
We can also give MyFileEditForm
a different set of required and non-required fields to override what it inherits from Sufia's GenericFilePresenter class:
# app/forms/my_file_edit_form.rb
class MyFileEditForm < MyGenericFilePresenter
include HydraEditor::Form
include HydraEditor::Form::Permissions
self.required_fields = [:title, :alternative, :creator, :tag, :rights]
end
In theory, we can give MyBatchEditForm
its own required_fields
value to override the value it inherits from MyFileEditForm
, but this might be something to think twice before doing.
Now we override Sufia's GenericFilesController and BatchController so that they use our custom edit form classes rather than the defaults in Sufia.
Add the following line to app/controllers/generic_files_controller.rb
self.edit_form_class = MyFileEditForm
The whole file should look like this:
# app/controllers/generic_files_controller.rb
class GenericFilesController < ApplicationController
include Sufia::Controller
include Sufia::FilesControllerBehavior
self.presenter_class = MyGenericFilePresenter
self.edit_form_class = MyFileEditForm
end
And add the following line to app/controllers/batch_controller.rb
self.edit_form_class = MyBatchEditForm
The whole file should look like this:
# app/controllers/batch_controller.rb
class BatchController < ApplicationController
include Sufia::BatchControllerBehavior
self.edit_form_class = MyBatchEditForm
end
The batch edits controller was not modified to allow you to set a form the way the batch controller was. (Batch edits are for editing from the dashboard; batch is about uploading groups of files together). So you have to override #terms directly. Add app/controllers/batch_edits_controller.rb
:
class BatchEditsController < ApplicationController
include Hydra::BatchEditBehavior
include GenericFileHelper
include Sufia::BatchEditsControllerBehavior
def terms
MyBatchEditForm.terms
end
end
It would be nice to make things more consistent here; note this may all be overhauled soon due to pcdm.
By default the label for the field in the form is taken from the property label, with the initial letter capitalised. Here is an example of customising it, and the associated help text:
# config/locales/sufia.en.yml
en:
simple_form:
labels:
generic_file:
alternative: "Alternative Title"
metadata_help:
generic_file:
alternative: "An alternative name for a file."