Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Favor method_missing over a raft of delegates #6763

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jeremyf marked this conversation as resolved.
Show resolved Hide resolved
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
31 changes: 16 additions & 15 deletions spec/presenters/hyrax/work_show_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
end

it { is_expected.to delegate_method(:to_s).to(:solr_document) }
it { is_expected.to delegate_method(:suppressed?).to(:solr_document) }
it { is_expected.to delegate_method(:human_readable_type).to(:solr_document) }
it { is_expected.to delegate_method(:date_created).to(:solr_document) }
it { is_expected.to delegate_method(:date_modified).to(:solr_document) }
it { is_expected.to delegate_method(:date_uploaded).to(:solr_document) }
it { is_expected.to delegate_method(:rights_statement).to(:solr_document) }
it { is_expected.to delegate_method(:rights_notes).to(:solr_document) }

it { is_expected.to delegate_method(:based_near_label).to(:solr_document) }
it { is_expected.to delegate_method(:related_url).to(:solr_document) }
it { is_expected.to delegate_method(:depositor).to(:solr_document) }
it { is_expected.to delegate_method(:identifier).to(:solr_document) }
it { is_expected.to delegate_method(:resource_type).to(:solr_document) }
it { is_expected.to delegate_method(:keyword).to(:solr_document) }
it { is_expected.to delegate_method(:itemtype).to(:solr_document) }

it { is_expected.to respond_to(:suppressed?) }
it { is_expected.to respond_to(:human_readable_type) }
it { is_expected.to respond_to(:date_created) }
it { is_expected.to respond_to(:date_modified) }
it { is_expected.to respond_to(:date_uploaded) }
it { is_expected.to respond_to(:rights_statement) }
it { is_expected.to respond_to(:rights_notes) }

it { is_expected.to respond_to(:based_near_label) }
it { is_expected.to respond_to(:related_url) }
it { is_expected.to respond_to(:depositor) }
it { is_expected.to respond_to(:identifier) }
it { is_expected.to respond_to(:resource_type) }
it { is_expected.to respond_to(:keyword) }
it { is_expected.to respond_to(:itemtype) }
it { is_expected.to delegate_method(:member_presenters).to(:member_presenter_factory) }
it { is_expected.to delegate_method(:ordered_ids).to(:member_presenter_factory) }

Expand Down