samvera-deprecated / sufia

[DEPRECATED] Sufia: a fully featured, flexible Samvera repository front-end.
http://sufia.io/
Other
111 stars 78 forks source link

Documentation: How do I add another field #904

Closed jcoyne closed 9 years ago

jcoyne commented 9 years ago

This would be a nice tutorial.

jcoyne commented 9 years ago

Create the GenericFile model

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

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

Extend the presenter

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

Set the controller to use our presenter

# 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

Create forms

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

Set the controller to use our form

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
jeremyf commented 9 years ago

:+1: Good directions, though as @jcoyne suggested, injecting the classes would be nice.

jcoyne commented 9 years ago

Updated so it'll work with #906

weiweishi commented 9 years ago

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
mjgiarlo commented 9 years ago

@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?

weiweishi commented 9 years ago

Yes @mjgiarlo. Thanks for the correction.

mjgiarlo commented 9 years ago

And thank you, @weiweishi, for helping make this better! This is going to be so useful for Sufia adopters.

scherztc commented 9 years ago

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.

mjgiarlo commented 9 years ago

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.)

weiweishi commented 9 years ago

@mjgiarlo that would be great! I'm still looking for the place where I can customize fields to be non-repeatable.

escowles commented 9 years ago

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.

awead commented 9 years ago

:+1: to the generator idea, if that's feasible.

mjgiarlo commented 9 years ago

I dig it -- like RDF metadata scaffolding. :)

mephillips-durham commented 9 years ago

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?

weiweishi commented 9 years ago

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.

mjgiarlo commented 9 years ago

I stepped through this myself to add one field and here's that commit:

https://bitbucket.org/uwlib/druw/commits/5b9124dd884f00a15b82d3e60aaf0840aae47dea#chg-config/locales/sufia.en.yml

The link is to the locales file which is where you can customize labels and helps.

mjgiarlo commented 9 years ago

Thanks for walking through that, @mephillips-durham! I've added that correction to the instructions above.

mjgiarlo commented 9 years ago

@weiweishi Have you solved your repeatability concerns or are you working on that yet?

mjgiarlo commented 9 years ago

I'm moving this to a GitHub wiki page, btw, and linking it to the README.

mjgiarlo commented 9 years ago

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 ?