Skip to content

Commit

Permalink
Split the DocumentPresenter into ShowPresenter and IndexPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 17, 2016
1 parent e50b27d commit 1589230
Show file tree
Hide file tree
Showing 22 changed files with 794 additions and 100 deletions.
51 changes: 39 additions & 12 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,17 @@ def render_index_field_label *args
# @param [String] field
# @param [Hash] opts
# @options opts [String] :value
# TODO: deprecate and use render_field_value
def render_index_field_value *args
render_field_value(*args)
end

def render_field_value(*args)
options = args.extract_options!
document = args.shift || options[:document]

field = args.shift || options[:field]
presenter(document).render_index_field_value field, options.except(:document, :field)
presenter(document).field_value field, options.except(:document, :field)
end

##
Expand Down Expand Up @@ -213,12 +218,9 @@ def render_document_show_field_label *args
# @param [String] field
# @param [Hash] opts
# @options opts [String] :value
# TODO: deprecate and use render_field_value
def render_document_show_field_value *args
options = args.extract_options!
document = args.shift || options[:document]

field = args.shift || options[:field]
presenter(document).render_document_show_field_value field, options.except(:document, :field)
render_field_value(*args)
end

##
Expand All @@ -229,7 +231,7 @@ def render_document_show_field_value *args
# @return [String]
def document_heading document=nil
document ||= @document
presenter(document).document_heading
presenter(document).heading
end

##
Expand All @@ -242,7 +244,7 @@ def document_heading document=nil
def document_show_html_title document=nil
document ||= @document

presenter(document).document_show_html_title
presenter(document).html_title
end

##
Expand All @@ -260,7 +262,7 @@ def render_document_heading(*args)
tag = options.fetch(:tag, :h4)
document ||= @document

content_tag(tag, presenter(document).document_heading, itemprop: "name")
content_tag(tag, presenter(document).heading, itemprop: "name")
end

##
Expand Down Expand Up @@ -322,15 +324,40 @@ def render_grouped_response? response = @response

##
# Returns a document presenter for the given document
# TODO: Move this to the controller. It can just pass a presenter or set of presenters.
def presenter(document)
presenter_class.new(document, self)
case action_name
when 'show', 'citation'
show_presenter(document)
when 'index'
index_presenter(document)
else
raise "Unable to determine presenter type for #{action_name} on #{controller_name}"
end
end

def show_presenter(document)
show_presenter_class(document).new(document, self)
end

def index_presenter(document)
index_presenter_class(document).new(document, self)
end

##
# Override this method if you want to use a different presenter class
def presenter_class
blacklight_config.document_presenter_class
end
deprecation_deprecate presenter_class: "replaced by show_presenter_class and index_presenter_class"

##
# Override this method if you want to use a different presenter class
def show_presenter_class(_document)
blacklight_config.show.document_presenter_class
end

def index_presenter_class(_document)
blacklight_config.index.document_presenter_class
end

##
# Open Search discovery tag for HTML <head> links
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def url_for_document(doc, options = {})
# Use the catalog_path RESTful route to create a link to the show page for a specific item.
# catalog_path accepts a hash. The solr query params are stored in the session,
# so we only need the +counter+ param here. We also need to know if we are viewing to document as part of search results.
# TODO: move this to the IndexPresenter
def link_to_document(doc, field_or_opts = nil, opts={:counter => nil})
if field_or_opts.is_a? Hash
opts = field_or_opts
Expand All @@ -24,7 +25,7 @@ def link_to_document(doc, field_or_opts = nil, opts={:counter => nil})
end

field ||= document_show_link_field(doc)
label = presenter(doc).render_document_index_label field, opts
label = index_presenter(doc).label field, opts
link_to label, url_for_document(doc), document_link_params(doc, opts)
end

Expand Down
95 changes: 32 additions & 63 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
module Blacklight
# @deprecated
class DocumentPresenter
extend Deprecation
self.deprecation_horizon = 'Blacklight version 7.0.0'
Expand All @@ -19,13 +20,11 @@ def initialize(document, controller, configuration = controller.blacklight_confi
#
# @param [SolrDocument] document
# @return [String]
# @deprecated use ShowPresenter#heading instead
def document_heading
fields = Array(@configuration.view_config(:show).title_field)
f = fields.find { |field| @document.has? field }

value = f.nil? ? @document.id : @document[f]
ValueRenderer.new(Array.wrap(value)).render
show_presenter.heading
end
deprecation_deprecate document_heading: "use ShowPresenter#heading instead"

##
# Create <link rel="alternate"> links from a documents dynamically
Expand All @@ -35,63 +34,48 @@ def document_heading
# @option options [Boolean] :unique ensures only one link is output for every
# content type, e.g. as required by atom
# @option options [Array<String>] :exclude array of format shortnames to not include in the output
# @deprecated moved to ShowPresenter#link_rel_alternates
def link_rel_alternates(options = {})
LinkAlternatePresenter.new(@controller, @document, options).render
show_presenter.link_rel_alternates(options)
end
deprecation_deprecate link_rel_alternates: "use ShowPresenter#link_rel_alternates instead"

##
# Get the document's "title" to display in the <title> element.
# (by default, use the #document_heading)
#
# @see #document_heading
# @return [String]
# @deprecated use ShowPresenter#html_title instead
def document_show_html_title
if @configuration.view_config(:show).html_title_field
fields = Array.wrap(@configuration.view_config(:show).html_title_field)
f = fields.find { |field| @document.has? field }
f ||= 'id'
field_values(show_field_config(f))
else
document_heading
end
show_presenter.html_title
end
deprecation_deprecate document_show_html_title: "use ShowPresenter#html_title instead"

##
# Render the document index heading
#
# @param [Symbol, Proc, String] field Render the given field or evaluate the proc or render the given string
# @param [Hash] opts
def render_document_index_label(field, opts = {})
label = case field
when Symbol
@document[field]
when Proc
field.call(@document, opts)
when String
field
end

label ||= @document.id
ValueRenderer.new(Array.wrap(label)).render
# @deprecated use IndexPresenter#label instead
def render_document_index_label(*args)
index_presenter.label(*args)
end
deprecation_deprecate render_document_index_label: "use IndexPresenter#label instead"

##
# Render the index field label for a document
#
# Allow an extention point where information in the document
# may drive the value of the field
# @param [String] field
# @param [Hash] opts
# @options opts [String] :value
def render_index_field_value field, options = {}
field_config = index_field_config(field)
if options[:value]
# TODO: Fold this into field_values
ValueRenderer.new(Array.wrap(options[:value]), field_config).render
else
field_values(field_config, options)
end
# Allow an extention point where information in the document
# may drive the value of the field
# @param [String] field
# @param [Hash] opts
# @options opts [String] :value
# @deprecated use IndexPresenter#field_value instead
def render_index_field_value *args
index_presenter.field_value(*args)
end
deprecation_deprecate render_index_field_value: "use IndexPresenter#field_value instead"

##
# Render the show field value for a document
Expand All @@ -101,16 +85,11 @@ def render_index_field_value field, options = {}
# @param [String] field
# @param [Hash] options
# @options opts [String] :value
def render_document_show_field_value field, options={}
field_config = show_field_config(field)
if options[:value]
# TODO: Fold this into field_values
ValueRenderer.new(Array.wrap(options[:value]), field_config).render
else
field_values(field_config, options)
end

# @deprecated use ShowPresenter#field_value
def render_document_show_field_value *args
show_presenter.field_value(*args)
end
deprecation_deprecate render_document_show_field_value: "use ShowPresenter#field_value instead"

##
# Get the value for a document's field, and prepare to render it.
Expand Down Expand Up @@ -144,6 +123,7 @@ def get_field_values _field, field_config, options = {}
def field_values(field_config, options={})
FieldPresenter.new(@controller, @document, field_config, options).render
end
deprecation_deprecate field_values: 'Use ShowPresenter or IndexPresenter field_values instead'

# @deprecated
def render_field_value(values, field_config = nil)
Expand All @@ -153,23 +133,12 @@ def render_field_value(values, field_config = nil)

private

def show_field_config(field)
field_config(@configuration.show_fields, field)
end

def index_field_config(field)
field_config(@configuration.index_fields, field)
end

def field_config(conf, field)
conf.fetch(field) { NilFieldConfig.new(field) }
def index_presenter
@controller.index_presenter(@document)
end

# Returned if no config is defined for the field in the Blacklight::Configuration
class NilFieldConfig < Blacklight::Configuration::Field
def initialize(field)
super(field: field)
end
def show_presenter
@controller.show_presenter(@document)
end
end
end
73 changes: 73 additions & 0 deletions app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true
module Blacklight
class IndexPresenter
# @param [SolrDocument] document
# @param [ActionController::Base] controller scope for linking and generating urls
# @param [Blacklight::Configuration] configuration
def initialize(document, controller, configuration = controller.blacklight_config)
@document = document
@configuration = configuration
@controller = controller
end

##
# Render the document index heading
#
# @param [Symbol, Proc, String] field Render the given field or evaluate the proc or render the given string
# @param [Hash] opts
# TODO: the default field should be `document_show_link_field(doc)'
def label(field, opts = {})
label = case field
when Symbol
@document[field]
when Proc
field.call(@document, opts)
when String
field
end

label ||= @document.id
ValueRenderer.new(Array.wrap(label)).render
end

##
# Render the index field label for a document
#
# Allow an extention point where information in the document
# may drive the value of the field
# @param [String] field
# @param [Hash] opts
# @options opts [String] :value
def field_value field, options = {}
field_config = field_config(field)
if options[:value]
# TODO: Fold this into field_values
ValueRenderer.new(Array.wrap(options[:value]), field_config).render
else
field_values(field_config, options)
end
end

private

##
# Get the value for a document's field, and prepare to render it.
# - highlight_field
# - accessor
# - solr field
#
# Rendering:
# - helper_method
# - link_to_search
# @param [Blacklight::Configuration::Field] solr field configuration
# @param [Hash] options additional options to pass to the rendering helpers
def field_values(field_config, options={})
FieldPresenter.new(@controller, @document, field_config, options).render
end

def field_config(field)
@configuration.index_fields.fetch(field) { Configuration::NullField.new(field) }
end
end
end

0 comments on commit 1589230

Please sign in to comment.