Skip to content

Commit

Permalink
⚙️ Move Engine's to_prepare to an initializer
Browse files Browse the repository at this point in the history
This commit will resolve load ordering.  Previously, we were loading the
decorators from HykuKnapsack before we load the App's, which is standard
behavior, but we actually want the decorators from HykuKnapsack to load
last.
  • Loading branch information
kirkkwang committed Aug 14, 2024
1 parent 6bcad36 commit 68a5470
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

# OVERRIDE HYRAX 3.5.0
module Hyrax
module Adl
module CollectionPresenterDecorator
module ClassMethods
# Terms is the list of fields displayed by
# app/views/collections/_show_descriptions.html.erb
def terms
super + Collection.additional_terms
super - [:size] + Collection.additional_terms
end
end
end
end

Hyrax::CollectionPresenter.singleton_class.prepend(Hyrax::CollectionPresenterDecorator::ClassMethods)
Hyrax::CollectionPresenter.singleton_class.prepend(Adl::CollectionPresenterDecorator::ClassMethods)
48 changes: 48 additions & 0 deletions config/initializers/zz_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

# This is the opposite of what you usually want to do. Normally app views override engine
# views and app decorators override engine decorators but in our case things in the Knapsack
# override what is in the application.
#
# Furthermore we need to account for when the ApplicationController and it's descendants set
# their individual view_paths. By looping through all descendants, we ensure that we have
# the Knapsack views at the beginning of the list of view_paths.
#
# In the load sequence, when we load ApplicationController, we establish the view_path for all
# future descendants. When we then encounter a descendant, we copy the
# ApplicationController's view_path to the descendant; then later after we've encountered most
# all of the descendants we updated the ApplicationController's view_path, but that does not
# propogate to the descendants' copied view_path.
Rails.application.config.to_prepare do
HykuKnapsack::Engine.root.glob("app/**/*_decorator*.rb").sort.each do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end

HykuKnapsack::Engine.root.glob("lib/**/*_decorator*.rb").sort.each do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end

([::ApplicationController] + ::ApplicationController.descendants).each do |klass|
paths = klass.view_paths.collect(&:to_s)
paths = [HykuKnapsack::Engine.root.join('app', 'views').to_s] + paths
klass.view_paths = paths.uniq
end
::ApplicationController.send :helper, HykuKnapsack::Engine.helpers

GenericWorkResourceForm.include Hyrax::FormFields(:slug_metadata)
GenericWorkResourceForm.include(SlugBugValkyrie)
ImageResourceForm.include Hyrax::FormFields(:slug_metadata)
ImageResourceForm.include(SlugBugValkyrie)

Hyrax::Forms::CollectionForm.terms += ::Collection.additional_terms
::Collection.include ::Hyrax::CollectionBehavior
::Collection.include SlugMetadata
::Collection.include AdventistMetadata
::Collection.include DogBiscuits::JournalArticleMetadata
::Collection.include DogBiscuits::BibliographicCitation
::Collection.include DogBiscuits::DateIssued
::Collection.include DogBiscuits::Geo
::Collection.include DogBiscuits::PlaceOfPublication
::Collection.include DogBiscuits::RemoteUrl
Hyrax::CollectionPresenter.delegate(*::Collection.additional_terms, to: :solr_document)
end
45 changes: 0 additions & 45 deletions lib/hyku_knapsack/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,6 @@ def self.load_translations!
end
end

config.to_prepare do
HykuKnapsack::Engine.root.glob("app/**/*_decorator*.rb").sort.each do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end

HykuKnapsack::Engine.root.glob("lib/**/*_decorator*.rb").sort.each do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end

# This is the opposite of what you usually want to do. Normally app views override engine
# views but in our case things in the Knapsack override what is in the application.
# Furthermore we need to account for when the ApplicationController and it's descendants set
# their individual view_paths. By looping through all descendants, we ensure that we have
# the Knapsack views at the beginning of the list of view_paths.
#
# In the load sequence, when we load ApplicationController, we establish the view_path for all
# future descendants. When we then encounter a descendant, we copy the
# ApplicationController's view_path to the descendant; then later after we've encountered most
# all of the descendants we updated the ApplicationController's view_path, but that does not
# propogate to the descendants' copied view_path.
([::ApplicationController] + ::ApplicationController.descendants).each do |klass|
paths = klass.view_paths.collect(&:to_s)
paths = [HykuKnapsack::Engine.root.join('app', 'views').to_s] + paths
klass.view_paths = paths.uniq
end
::ApplicationController.send :helper, HykuKnapsack::Engine.helpers

GenericWorkResourceForm.include Hyrax::FormFields(:slug_metadata)
GenericWorkResourceForm.include(SlugBugValkyrie)
ImageResourceForm.include Hyrax::FormFields(:slug_metadata)
ImageResourceForm.include(SlugBugValkyrie)

Hyrax::Forms::CollectionForm.terms += ::Collection.additional_terms
::Collection.include ::Hyrax::CollectionBehavior
::Collection.include SlugMetadata
::Collection.include AdventistMetadata
::Collection.include DogBiscuits::JournalArticleMetadata
::Collection.include DogBiscuits::BibliographicCitation
::Collection.include DogBiscuits::DateIssued
::Collection.include DogBiscuits::Geo
::Collection.include DogBiscuits::PlaceOfPublication
::Collection.include DogBiscuits::RemoteUrl
Hyrax::CollectionPresenter.delegate(*::Collection.additional_terms, to: :solr_document)
end

config.before_initialize do
config.i18n.load_path += Dir["#{config.root}/config/locales/**/*.yml"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
describe '#terms_with_values' do
let(:instance) { described_class.new(document, nil) }

# TODO: this spec fails because Hyku has an override by the same name
xit 'includes values from collection' do
it 'includes values from collection' do
expect(instance.terms_with_values).to include(:official_url) # Which is a collection property.
end
end
Expand Down

0 comments on commit 68a5470

Please sign in to comment.