Skip to content

Commit

Permalink
🧹 Make the appropriate link generate
Browse files Browse the repository at this point in the history
This commit will add a decorator to check the `human_readable_type` of
the given object is a Valkyrie migration object (which by convention
ends with 'resource') and adjust the generated link accordingly.  For
example, a GenericWorkResource should generate links like
`/concern/generic_work_resources/...` instead of
`/concern/generic_works/...`.

The models were updated to include the Hyrax::NestedWorks so the `Attach
Child` button would populate the appropriate work types.

Lastly, the `Gemfile.lock` was updated to include the latest version of
Hyrax `double_combo` branch.
  • Loading branch information
kirkkwang committed Jan 30, 2024
1 parent 44aa7f7 commit bb2ad8a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ GIT

GIT
remote: https://github.com/samvera/hyrax.git
revision: 74703481e1a9cd07831c439e788338fc6bd74c17
revision: db11f7fe3fe00b841bf0034bb0afe56c9806fadb
branch: double_combo
specs:
hyrax (5.0.0.rc2)
Expand Down
18 changes: 18 additions & 0 deletions app/models/concerns/hyrax/solr_document_behavior_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# OVERRIDE Hyrax 5.0.0rc2 to account for Valkyrie migration object that end in "Resource"

module Hyrax
module SolrDocumentBehaviorDecorator
def hydra_model(classifier: nil)
if human_readable_type&.downcase&.strip&.ends_with?('resource')
human_readable_type.titleize.delete(' ').camelize(:upper)&.safe_constantize ||
model_classifier(classifier).classifier(self).best_model
else
super
end
end
end
end

Hyrax::SolrDocumentBehavior.prepend(Hyrax::SolrDocumentBehaviorDecorator)
1 change: 1 addition & 0 deletions app/models/generic_work_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GenericWorkResource < Hyrax::Work
include Hyrax::Schema(:with_pdf_viewer)
include Hyrax::Schema(:with_video_embed)
include Hyrax::ArResource
include Hyrax::NestedWorks

Hyrax::ValkyrieLazyMigration.migrating(self, from: GenericWork)

Expand Down
1 change: 1 addition & 0 deletions app/models/image_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ImageResource < Hyrax::Work
include Hyrax::Schema(:with_pdf_viewer)
include Hyrax::Schema(:with_video_embed)
include Hyrax::ArResource
include Hyrax::NestedWorks

Hyrax::ValkyrieLazyMigration.migrating(self, from: Image)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

RSpec.describe Hyrax::SolrDocumentBehavior, type: :decorator do
subject(:solr_document) { solr_document_class.new(solr_hash) }
let(:solr_hash) { {} }

let(:solr_document_class) do
Class.new do
include Blacklight::Solr::Document
include Hyrax::SolrDocumentBehavior
end
end

describe '#to_partial_path' do
context 'with an ActiveFedora model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWork' } }

it 'resolves the correct model name' do
expect(solr_document.to_model.to_partial_path).to eq 'hyrax/generic_works/generic_work'
end
end

context 'with a Valkyrie model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWorkResource' } }

it 'resolves the correct model name' do
expect(solr_document.to_model.to_partial_path).to eq 'hyrax/generic_work_resources/generic_work_resource'
end
end

context 'with a Valkyrie migration model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWork', 'human_readable_type_tesim' => 'Generic work resource' } }

it 'resolves the correct model name' do
expect(solr_document.to_model.to_partial_path).to eq 'hyrax/generic_work_resources/generic_work_resource'
end
end
end

describe '#hydra_model' do
it 'gives ActiveFedora::Base by default' do
expect(solr_document.hydra_model).to eq ActiveFedora::Base
end

context 'with an ActiveFedora model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWork' } }

it 'resolves the correct model name' do
expect(solr_document.hydra_model).to eq GenericWork
end
end

context 'with a Valkyrie model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWorkResource' } }

it 'resolves the correct model name' do
expect(solr_document.hydra_model).to eq GenericWorkResource
end
end

context 'with a Valkyrie migration model name' do
let(:solr_hash) { { 'has_model_ssim' => 'GenericWork', 'human_readable_type_tesim' => 'Generic work resource' } }

it 'resolves the correct model name' do
expect(solr_document.hydra_model).to eq GenericWorkResource
end
end
end
end

0 comments on commit bb2ad8a

Please sign in to comment.