Skip to content

Commit

Permalink
Merge ceb8d6d into 2a23401
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 30, 2018
2 parents 2a23401 + ceb8d6d commit 9a77943
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
20 changes: 11 additions & 9 deletions app/presenters/blacklight/field_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
module Blacklight
# Renders a field and handles link_to_facet or helper_method if supplied
class FieldPresenter
# @param controller [Object] the context in which to execute helper methods
# @param document [SolrDocument] the document
# @param field_config [Blacklight::Configuration::Field] the field's configuration
# @param options [Hash]
# @option options [Object] :value when this is provided, we don't want the pipeline to deal with helper methods.
# this happens when drawing the label for the document
def initialize(controller, document, field_config, options)
@controller = controller
@document = document
Expand All @@ -13,15 +19,11 @@ def initialize(controller, document, field_config, options)
attr_reader :controller, :document, :field_config, :options

def 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)
return Rendering::Pipeline.render(retrieve_values, field_config, document, controller, options) unless options[:value]
values = Array.wrap(options[:value])
# Prevents helper methods from drawing.
steps = Rendering::Pipeline.operations - [Rendering::HelperMethod]
Rendering::Pipeline.new(values, field_config, document, controller, steps, options).render
end

private
Expand Down
3 changes: 2 additions & 1 deletion app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def initialize(document, view_context, configuration = view_context.blacklight_c
end

##
# Render the document index heading
# Render the document index heading. This is used when making a link to a
# document, where we don't want any HTML markup added from the pipeline.
#
# @param [Symbol, Proc, String] field_or_string_or_proc Render the given field or evaluate the proc or render the given string
# @param [Hash] opts
Expand Down
25 changes: 20 additions & 5 deletions app/presenters/blacklight/rendering/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@

module Blacklight
module Rendering
# The field rendering pipeline
# The field rendering pipeline.
# This takes a field and it's values and transforms them through a list of
# operations.
class Pipeline
class_attribute :operations
class_attribute :operations, instance_accessor: false

# The ordered list of pipeline operations
self.operations = [HelperMethod, LinkToFacet, Microdata, Join]

def initialize(values, config, document, context, options)
# @param values [Array] the values for the field
# @param config [Blacklight::Configuration::Field] the field's configuration
# @param document [SolrDocument] the document
# @param context [Object] an execution context, used to execute the helper method in.
# @param operations [Array<Class>] the list of operations in this Pipeline
# @param options [Hash] options to pass to the processors. Typically only `:value` is used
def initialize(values, config, document, context, operations, options)
@values = values
@config = config
@document = document
@context = context
@operations = operations
@options = options
end

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

# @param values [Array] the values for the field
# @param config [Blacklight::Configuration::Field] the field's configuration
# @param document [SolrDocument] the document
# @param context [Object] an execution context, used to execute the helper method in.
# @param options [Hash] options to pass to the processors. Typically only `:value` is used
def self.render(values, config, document, context, options)
new(values, config, document, context, options).render
new(values, config, document, context, operations, options).render
end

# Perform the processing by the pipeline
def render
first, *rest = *stack
first.new(values, config, document, context, options, rest).render
Expand Down
29 changes: 24 additions & 5 deletions spec/presenters/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
include Capybara::RSpecMatchers
let(:document) { instance_double(SolrDocument) }
let(:context) { double }
let(:options) { double }
let(:presenter) { described_class.new(values, field_config, document, context, options) }
let(:options) { double('options') }

describe "render" do
subject { presenter.render }
describe '.render' do
subject { described_class.render(values, field_config, document, context, options) }

let(:values) { %w[a b] }
let(:field_config) { Blacklight::Configuration::NullField.new }
Expand All @@ -28,9 +27,16 @@

it { is_expected.to have_selector("span[@itemprop='some-prop']", text: "a") }
end

it 'sets the operations on the instance as equal to the class variable' do
expect(described_class).to receive(:new)
.with(values, field_config, document, context, described_class.operations, options)
.and_return(instance_double(described_class, render: true))
subject
end
end

describe "#operations" do
describe '.operations' do
subject { described_class.operations }

it {
Expand All @@ -40,4 +46,17 @@
Blacklight::Rendering::Join]
}
end

describe '#operations' do
subject(:operations) { presenter.operations }

let(:presenter) { described_class.new(values, field_config, document, context, steps, options) }
let(:steps) { [Blacklight::Rendering::HelperMethod] }
let(:values) { ['a'] }
let(:field_config) { Blacklight::Configuration::NullField.new }

it 'sets the operations to the value passed to the initializer' do
expect(operations).to eq steps
end
end
end

0 comments on commit 9a77943

Please sign in to comment.