Closed jcoyne closed 9 years ago
the GenericFile class is typically provided by Sufia, but we want to update the model, so we define it in our app instead. 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:
property :alternative, predicate: ::RDF::DC.alternative
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
end
We create a subclass of the 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
# app/controllers/generic_files_controller.rb
class GenericFilesController < ApplicationController
include Sufia::Controller
include Sufia::FilesControllerBehavior
self.presenter_class = MyGenericFilePresenter
end
Now the fields show up in the show view, but we also want them on our edit form too. Let's create an edit form that has our field
This form will extend the presenter we already created.
# app/forms/my_file_edit_form.rb
class MyFileEditForm < MyGenericFilePresenter
include HydraEditor::Form
include HydraEditor::Form::Permissions
self.required_fields = [:title, :creator, :tag, :rights]
end
Create a batch form
# app/forms/my_batch_edit_form.rb
class MyBatchEditForm < MyFileEditForm
end
Add the following line to app/controllers/generic_files_controller.rb
self.edit_form_class = MyFileEditForm
The whole file should look like this:
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 = MyFileEditForm
The whole file should look like this:
# app/controllers/batch_controller.rb
class BatchController < ApplicationController
include Sufia::BatchControllerBehavior
self.edit_form_class = MyBatchEditForm
end
:+1: Good directions, though as @jcoyne suggested, injecting the classes would be nice.
Updated so it'll work with #906
The following should be added to the tutorial:
Create a batch form
# app/forms/my_batch_edit_form.rb
module Sufia
module Forms
class MyBatchEditForm < MyFileEditForm
end
end
end
And add the following line to app/controllers/batch_controller.rb
self. edit_form_class = MyFileEditForm
The whole file should look like this:
class BatchController < ApplicationController
include Sufia::BatchControllerBehavior
self.edit_form_class = MyBatchEditForm
end
@weiweishi I have merged your changes into the document above. Though I made a small change: I removed the module declarations from the batch form. Is that OK?
Yes @mjgiarlo. Thanks for the correction.
And thank you, @weiweishi, for helping make this better! This is going to be so useful for Sufia adopters.
Thanks for the function based tutorials/documentation. Reading through the class based injections is really helpful for seeing how good coding patterns gets applied in an application.
Should this tutorial also include how to indicate that fields are repeatable and required, and how to customize labels and "helps?" That would be a formidable tutorial indeed, and I think it'd save us some time judging by the interest and needs of folks as expressed in messages sent to hydra-tech over the past couple months. The new configuration with forms, presenters, and inputs is probably new to a bunch of folks. (I know I probably have an outdated understanding of how to do this stuff.)
@mjgiarlo that would be great! I'm still looking for the place where I can customize fields to be non-repeatable.
These instructions are great -- but the process is involved enough that I'm wondering whether we should work on a generator to handle this and/or add an option to the sufia:install generator to do the one-time steps.
:+1: to the generator idea, if that's feasible.
I dig it -- like RDF metadata scaffolding. :)
In case anyone else is a real newbie, like me, the MyGenericFilePresenter needs to go in the file app/presenters/my_generic_file_presenter.rb
Great instructions: worked first time! Where do you go if you want to customise the field labels on the form?
Mattthew, the labelling is handled in the locale files. i.e.: config/locale/sufia.en.yml file.
On Thu, Mar 12, 2015 at 10:59 AM, Matthew Phillips <notifications@github.com
wrote:
In case anyone else is a real newbie, like me, the MyGenericFilePresenter needs to go in the file app/presenters/my_generic_file_presenter.rb
Great instructions: worked first time! Where do you go if you want to customise the field labels on the form?
— Reply to this email directly or view it on GitHub https://github.com/projecthydra/sufia/issues/904#issuecomment-78527470.
I stepped through this myself to add one field and here's that commit:
The link is to the locales file which is where you can customize labels and helps.
Thanks for walking through that, @mephillips-durham! I've added that correction to the instructions above.
@weiweishi Have you solved your repeatability concerns or are you working on that yet?
I'm moving this to a GitHub wiki page, btw, and linking it to the README.
I recommend we close this issue now that this is created:
https://github.com/projecthydra/sufia/wiki/Customizing-Metadata
And we either create new issues for gaps/opportunities in that document, or folks can feel free to edit it in place (since GitHub wiki pages are under version control).
@jcoyne ?
This would be a nice tutorial.