Skip to content

Commit

Permalink
♻️ Favor method_missing over a raft of delegates
Browse files Browse the repository at this point in the history
It's like we're flooding a political convention with delegates.  Instead
of having all of those delegate methods, let's just implement method
missing and pass things along to the `SolrDocument`.

The benefit is that as we incorporate dynamic metadata, we don't need to
keep on adding `delegate :this_property, to: :solr_document`.
  • Loading branch information
jeremyf committed Mar 29, 2024
1 parent 289dc08 commit 1a2c4b8
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions app/presenters/hyrax/work_show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ class WorkShowPresenter
self.collection_presenter_class = CollectionPresenter
self.presenter_factory_class = MemberPresenterFactory

# Methods used by blacklight helpers
delegate :has?, :first, :fetch, :export_formats, :export_as, to: :solr_document

# delegate fields from Hyrax::Works::Metadata to solr_document
delegate :based_near_label, :related_url, :depositor, :identifier, :resource_type,
:keyword, :itemtype, :admin_set, :rights_notes, :access_right, :abstract, to: :solr_document

# @param [SolrDocument] solr_document
# @param [Ability] current_ability
# @param [ActionDispatch::Request] request the http request context. Used so
Expand All @@ -33,20 +26,14 @@ def initialize(solr_document, current_ability, request = nil)
@request = request
end

# We cannot rely on the method missing to catch this delegation. Because
# most all objects implicitly implicitly implement #to_s
delegate :to_s, to: :solr_document

def page_title
"#{human_readable_type} | #{title.first} | ID: #{id} | #{I18n.t('hyrax.product_name')}"
end

# CurationConcern methods
delegate :stringify_keys, :human_readable_type, :collection?, :to_s, :suppressed?,
to: :solr_document

# Metadata Methods
delegate :title, :date_created, :description,
:creator, :contributor, :subject, :publisher, :language, :embargo_release_date,
:lease_expiration_date, :license, :source, :rights_statement, :thumbnail_id, :representative_id,
:rendering_ids, :member_of_collection_ids, :alternative_title, :bibliographic_citation, to: :solr_document

def workflow
@workflow ||= WorkflowPresenter.new(solr_document, current_ability)
end
Expand Down Expand Up @@ -259,6 +246,15 @@ def valkyrie_presenter?

private

def method_missing(method_name, *args, &block)
return solr_document.public_send(method_name, *args, &block) if solr_document.respond_to?(method_name)
super
end

def respond_to_missing?(method_name, include_private = false)
solr_document.respond_to?(method_name, include_private) || super
end

# list of item ids to display is based on ordered_ids
def authorized_item_ids(filter_unreadable: Flipflop.hide_private_items?)
@member_item_list_ids ||=
Expand Down

0 comments on commit 1a2c4b8

Please sign in to comment.