-
Notifications
You must be signed in to change notification settings - Fork 1
Add Metadata to Views, Presenter, and Solr
In part one of this tutorial, we started by writing a feature spec for making a new Work. For part two, we're going to start by writing a feature spec for displaying a Work.
- Copy this
show_work_spec.rb
file tospec/features
- Run your test suite (
rails ci
orrspec spec
). Everything should pass.
- Add your new metadata field to the show spec you just created. You'll need
to add it in three places:
- As a let statement, where you define the value:
let(:year) { ['2010'] }
- In the Work.new block
- In the expectations See this diff on github for what it should look like when you're done.
- As a let statement, where you define the value:
- Run your test suite again (
rails ci
orrspec spec
if you have left your test solr and fedora running). It should fail with the messageFailure/Error: expect(page).to have_content work.year.first
.
This tells us that our test setup ran successfully (a Work was created with a year field) but the show page doesn't display that field yet.
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
As always, we want to write our test first.
- Hyrax generated
app/presenters/hyrax/work_presenter.rb
andspec/presenters/hyrax/work_presenter_spec.rb
for you when you created your Work work type. Open these files and take a look. - Edit
spec/presenters/hyrax/work_presenter_spec.rb
. It is only a stubbed spec right now. Replace it with thework_presenter_spec.rb
file on github. Note, ouryear
field has already been added to this file. - Run your test suite (
rails ci
). It will fail with the messageFailure/Error: it { is_expected.to delegate_method(:year).to(:solr_document) }
- Add this line to your work_presenter.rb file:
delegate :year, to: :solr_document
- Run your test suite again, and notice that our work_presenter_spec now passes. (Your show feature spec will still fail, because
year
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?
In order to use our custom presenter, our Work 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/works_controller.rb
. However, Hyrax did this for you when you ran the work type generator. Open the app/controllers/hyrax/works_controller.rb
file and note that it is configured to use Hyrax::WorkPresenter.
self.show_presenter = Hyrax::WorkPresenter
- Edit
app/models/work.rb
and add instructions for how theyear
field should be indexed:
property :year, predicate: "http://www.europeana.eu/schemas/edm/year" do |index|
index.as :stored_searchable
end
- Edit
app/models/solr_document.rb
and add this method:
def year
self[Solrizer.solr_name('year')]
end
- Copy Hyrax's
app/views/hyrax/base/_attribute_rows.html.erb
to the same directory structure in your app - Edit your newly created local copy of
_attribute_rows.html.erb
to include your metadata field:
<%= presenter.attribute_to_html(:year) %>
- Run your test suite again (
rails ci
) and your feature test should now pass
Note: You can see all the changes made during this exercise on the github repo.
We added one metadata field here: year. Choose either extent or references and add a second metadata field. Or, define your own metadata field, as long as you pick something that can be a simple string.