Skip to content

Commit

Permalink
Replace ValueRenderer with a Rendering::Pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 18, 2016
1 parent f570be3 commit be80cf2
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 151 deletions.
6 changes: 3 additions & 3 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def field_values(field_config, options={})
deprecation_deprecate field_values: 'Use ShowPresenter or IndexPresenter field_values instead'

# @deprecated
def render_field_value(values, field_config = nil)
ValueRenderer.new(Array.wrap(values), field_config).render
def render_field_value(values, field_config = Configuration::NullField.new)
FieldPresenter.new(@controller, @document, field_config, value: values).render
end
deprecation_deprecate render_field_value: 'Use ValueRenderer instead'
deprecation_deprecate render_field_value: 'Use FieldPresenter instead'

private

Expand Down
42 changes: 8 additions & 34 deletions app/presenters/blacklight/field_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,23 @@ def initialize(controller, document, field_config, options)
end

attr_reader :controller, :document, :field_config, :options
delegate :field, to: :field_config

def render
# TODO: move the itemprop stuff here
case
when field_config.helper_method
render_helper
when field_config.link_to_search
link_to_search
else
ValueRenderer.new(retrieve_values, field_config).render
if options[:value]
# This prevents helper methods from drawing.
config = Configuration::NullField.new(field_config.to_h.except(:helper_method))
values = Array.wrap(options[:value])
else
config = field_config
values = retrieve_values
end
Rendering::Pipeline.render(values, config, document, controller, options)
end

private

def render_helper
controller.send(field_config.helper_method,
options.merge(document: document,
field: field,
config: field_config,
value: retrieve_values))
end

# This allows the link to wrap an itemprop
def link_to_search
return unless field
link_field = if field_config.link_to_search === true
field_config.key
else
field_config.link_to_search
end

links = retrieve_values.map do |v|
controller.link_to ValueRenderer.new([v], field_config).render,
controller.search_action_path(controller.search_state.reset.add_facet_params(link_field, v))
end
links.to_sentence.html_safe
end

def retrieve_values
FieldRetriever.new(document, field_config).fetch
end

end
end
23 changes: 10 additions & 13 deletions app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ def initialize(document, controller, configuration = controller.blacklight_confi
# @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
def label(field_or_string_or_proc, opts = {})
config = Configuration::NullField.new
value = case field_or_string_or_proc
when Symbol
@document[field]
config = field_config(field_or_string_or_proc)
@document[field_or_string_or_proc]
when Proc
field.call(@document, opts)
field_or_string_or_proc.call(@document, opts)
when String
field
field_or_string_or_proc
end

label ||= @document.id
ValueRenderer.new(Array.wrap(label)).render
value ||= @document.id
field_values(config, value: value)
end

##
Expand All @@ -40,12 +42,7 @@ def label(field, 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
field_values(field_config, options)
end

private
Expand Down
24 changes: 24 additions & 0 deletions app/presenters/blacklight/rendering/abstract_step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Blacklight
module Rendering
class AbstractStep
def initialize(values, config, document, context, options, stack)
@values = values
@config = config
@document = document
@context = context
@options = options
@stack = stack
end

attr_reader :values, :config, :document, :context, :options, :stack

protected

def next_step(output_values)
first, *rest = *stack
first.new(output_values, config, document, context, options, rest).render
end
end
end
end

23 changes: 23 additions & 0 deletions app/presenters/blacklight/rendering/helper_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Blacklight
module Rendering
class HelperMethod < AbstractStep
def render
return next_step(values) unless config.helper_method
return render_helper # short circut the rest of the steps
end

private

def render_helper
context.send(config.helper_method,
options.merge(document: document,
field: config.field,
config: config,
value: values))
end
end
end
end



16 changes: 16 additions & 0 deletions app/presenters/blacklight/rendering/join.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Blacklight
module Rendering
class Join < AbstractStep
def render
options = config.separator_options || {}
next_step(values.map { |x| html_escape(x) }.to_sentence(options).html_safe)
end

private

def html_escape(*args)
ERB::Util.html_escape(*args)
end
end
end
end
35 changes: 35 additions & 0 deletions app/presenters/blacklight/rendering/link_to_facet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Blacklight
module Rendering
class LinkToFacet < AbstractStep
def render
# TODO: We should rename the config variable, because it creates a link to a facet.
return next_step(values) unless config.link_to_search
next_step(render_link)
end

private

# This allows the link to wrap an itemprop
def render_link
values.map { |v| link(link_field, v) }
end

def link_field
return config.key if config.link_to_search === true
config.link_to_search
end

def link(field, v)
context.link_to v, search_path(field, v)
end

def search_path(field, v)
context.search_action_path(facet_params(field, v))
end

def facet_params(field, v)
context.search_state.reset.add_facet_params(field, v)
end
end
end
end
17 changes: 17 additions & 0 deletions app/presenters/blacklight/rendering/microdata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Blacklight
module Rendering
class Microdata < AbstractStep
include ActionView::Helpers::TagHelper
def render
return next_step(values) unless config.itemprop
next_step(values.map { |x| itemprop(x, config.itemprop) })
end

private

def itemprop(val, itemprop)
content_tag :span, val, itemprop: itemprop
end
end
end
end
32 changes: 32 additions & 0 deletions app/presenters/blacklight/rendering/pipeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Blacklight
module Rendering
# The field rendering pipeline
class Pipeline
def initialize(values, config, document, context, options)
@values = values
@config = config
@document = document
@context = context
@options = options
end

attr_reader :values, :config, :document, :context, :options

def self.render(values, config, document, context, options)
new(values, config, document, context, options).render
end

def render
first, *rest = *stack
first.new(values, config, document, context, options, rest).render
end

protected

# Ordered list of operations, Terminator must be at the end.
def stack
[HelperMethod, LinkToFacet, Microdata, Join, Terminator]
end
end
end
end
9 changes: 9 additions & 0 deletions app/presenters/blacklight/rendering/terminator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Blacklight
module Rendering
class Terminator < AbstractStep
def render
values
end
end
end
end
13 changes: 3 additions & 10 deletions app/presenters/blacklight/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ def html_title
def heading
fields = Array.wrap(view_config.title_field)
f = fields.detect { |field| @document.has? field }

value = f.nil? ? @document.id : @document[f]
ValueRenderer.new(Array.wrap(value)).render
f ||= @configuration.document_model.unique_key
field_values(field_config(f), value: @document[f])
end

##
Expand All @@ -63,13 +62,7 @@ def heading
# @param [Hash] options
# @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
field_values(field_config(field), options)
end

private
Expand Down
55 changes: 0 additions & 55 deletions app/presenters/blacklight/value_renderer.rb

This file was deleted.

9 changes: 7 additions & 2 deletions lib/blacklight/configuration/null_field.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module Blacklight
# Returned if no config is defined for the field in the Blacklight::Configuration
class Configuration::NullField < Blacklight::Configuration::Field
def initialize(field)
super(field: field)
def initialize(field_or_hash = nil)
case field_or_hash
when String, Symbol
super(field: field_or_hash)
else
super
end
end
end
end
Loading

0 comments on commit be80cf2

Please sign in to comment.