Skip to content

Add Metadata to Views, Presenter, and Solr

Bess Sadler edited this page Sep 1, 2017 · 41 revisions

Goals:

Add the field to the show view

Add a show feature

In part one of this tutorial, we started by writing a feature spec for making a new ETD. For part two, we're going to start by writing a feature spec for displaying an ETD.

  1. Copy this show_etd_spec.rb file to spec/features
  2. Run your test suite (bundle exec rake ci or rspec spec). Everything should pass.

Add the new metadata field to your show feature

  1. Add your new metadata field to the show spec you just created. You'll need to add it in three places:
    1. As a let statement, where you define the value:
      let(:degree) { ['Bachelor of Arts with Honors'] }
    2. In the Etd.new block
    3. In the expectations
      See this diff on github for what it should look like when you're done.
  2. Run your test suite again (bundle exec rake ci or rspec spec if you have left your test solr and fedora running). It should fail with the message Failure/Error: expect(page).to have_content etd.degree.first.

This tells us that our test setup ran successfully (an ETD was created with a degree field) but the show page doesn't display that field yet.

Modify the presenter for our ETD class

Recall that Rails uses an "MVC" or "model-view-controller" pattern. In addition to models, views, and controllers, we also have presenters, sometimes also referred to as a "Decorator" or "View-Model." The presenter is responsible for translating values from the model to a presentable form.

Example:

class BookPresenter
  def initialize(book)
    @book = book
  end

  delegate :title, :creator, to: :@book

  def glitter_title
    '★' + @book.title + '☆'
  end
end

Customize our work-specific presenter

As always, we want to write our test first.

  1. Hyrax generated app/presenters/hyrax/etd_presenter.rb and spec/presenters/hyrax/etd_presenter_spec.rb for you when you created your ETD work type. Open these files and take a look.
  2. Edit spec/presenters/hyrax/etd_presenter_spec.rb. It is only a stubbed spec right now. Replace it with the etd_presenter_spec.rb file on github. Note, our degree field has already been added to this file.
  3. Run your test suite (bundle exec rake ci). It will fail with the message Failure/Error: it { is_expected.to delegate_method(:degree).to(:solr_document) }
  4. Add this line to your etd_presenter.rb file:
  delegate :degree, to: :solr_document
  1. Run your test suite again, and notice that our etd_presenter_spec now passes. (Your show feature spec will still fail, because degree is not yet appearing on our show view.)

For discussion: What does it mean to say that the method has been delegated to solr_document?

Use our custom presenter

In order to use our custom presenter, our ETD class has to know about it. Normally, you would have to manually add the custom presenter to the relevant controller, in this case app/controllers/hyrax/etds_controller.rb. However, Hyrax did this for you when you ran the work type generator. Open the app/controllers/hyrax/etds_controller.rb file and note that it is configured to use Hyrax::EtdPresenter.

  self.show_presenter = Hyrax::EtdPresenter

Add a field to view partial and solr

Add your field to the solr index

  1. Edit app/model/etd.rb and add instructions for how the degree field should be indexed:
  property :degree, predicate: "http://vivoweb.org/ontology/core#AcademicDegree" do |index|
    index.as :stored_searchable
  end

Add the field to solr_document

  1. Edit app/models/solr_document.rb and add this method:
  def degree
    self[Solrizer.solr_name('degree')]
  end

Add the field to the view partial

  1. Copy Hyrax's app/views/hyrax/base/_attribute_rows.html.erb to the same directory structure in your app
  2. Edit your newly created local copy of _attribute_rows.html.erb to include your metadata field:
  <%= presenter.attribute_to_html(:degree) %>
  1. Run your test suite again (bundle exec rake ci) and your feature test should now pass

Note: You can see all the changes made during this exercise on the github repo.

Pair exercise

We added one metadata field here: degree. Choose either department or school and add a second metadata field. Or, define your own metadata field, as long as you pick something that can be a simple string.