Skip to content

Commit

Permalink
Add 'default_thumbnail' view configuration
Browse files Browse the repository at this point in the history
The default thumbnail can be:

- an image asset
- the name of a helper method (as a symbol) that returns the markup for a thumbnail
- a lambda, which will receive the document and returns the thumbnail markup
  • Loading branch information
cbeer committed Aug 27, 2017
1 parent f892bf3 commit e3ea92e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
35 changes: 28 additions & 7 deletions app/presenters/blacklight/thumbnail_presenter.rb
Expand Up @@ -18,7 +18,9 @@ def initialize(document, view_context, view_config)
#
# @return [Boolean]
def exists?
thumbnail_method.present? || thumbnail_field && document.has?(thumbnail_field)
thumbnail_method.present? ||
(thumbnail_field && thumbnail_value_from_document(document).present?) ||
default_thumbnail.present?
end

##
Expand All @@ -36,16 +38,35 @@ def thumbnail_tag image_options = {}, url_options = {}

private

delegate :thumbnail_field, :thumbnail_method, to: :view_config
delegate :thumbnail_field, :thumbnail_method, :default_thumbnail, to: :view_config

# @param [Hash] image_options to pass to the image tag
def thumbnail_value(image_options)
if thumbnail_method
view_context.send(thumbnail_method, document, image_options)
elsif thumbnail_field
image_url = Array(thumbnail_field).lazy.map { |field| document.first(field) }.reject(&:blank?).first
view_context.image_tag image_url, image_options if image_url.present?
value = if thumbnail_method
view_context.send(thumbnail_method, document, image_options)
elsif thumbnail_field
image_url = thumbnail_value_from_document(document)
view_context.image_tag image_url, image_options if image_url.present?
end

value || default_thumbnail_value(image_options)
end

def default_thumbnail_value(image_options)
return unless default_thumbnail

case default_thumbnail
when Symbol
view_context.send(default_thumbnail, document, image_options)
when Proc
default_thumbnail.call(document, image_options)
else
view_context.image_tag default_thumbnail, image_options
end
end

def thumbnail_value_from_document(document)
Array(thumbnail_field).lazy.map { |field| document.first(field) }.reject(&:blank?).first
end
end
end
13 changes: 8 additions & 5 deletions spec/helpers/catalog_helper_spec.rb
Expand Up @@ -200,17 +200,20 @@ def render_grouped_response?
expect(Deprecation).to receive(:warn)
end

let(:document) { instance_double(SolrDocument) }
let(:document) { SolrDocument.new(data) }
let(:data) { {} }

it "has a thumbnail if a thumbnail_method is configured" do
allow(helper).to receive_messages(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz, document_presenter_class: Blacklight::IndexPresenter) ))
expect(helper.has_thumbnail? document).to be true
end

it "has a thumbnail if a thumbnail_field is configured and it exists in the document" do
allow(helper).to receive_messages(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz, document_presenter_class: Blacklight::IndexPresenter) ))
allow(document).to receive_messages(has?: true)
expect(helper.has_thumbnail? document).to be true
context "if a thumbnail_field is configured and it exists in the document" do
let(:data) { { xyz: 'abc' } }
it "has a thumbnail" do
allow(helper).to receive_messages(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz, document_presenter_class: Blacklight::IndexPresenter) ))
expect(helper.has_thumbnail? document).to be true
end
end

it "does not have a thumbnail if the thumbnail_field is missing from the document" do
Expand Down
44 changes: 44 additions & 0 deletions spec/presenters/thumbnail_presenter_spec.rb
Expand Up @@ -45,6 +45,16 @@
end
end

context "when default_thumbnail is configured" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(default_thumbnail: 'image.png')
end

context "and the field exists in the document" do
it { is_expected.to be true }
end
end

context "without any configured options" do
it { is_expected.to be_falsey }
end
Expand Down Expand Up @@ -120,6 +130,40 @@
end
end

context "when default_thumbnail is configured" do
context "and is a string" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(default_thumbnail: 'image.png')
end

it "creates an image tag for the given asset" do
allow(view_context).to receive(:image_tag).with('image.png', {}).and_return('<img src="image.jpg">')
expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '<img src="image.jpg">'
end
end

context "and is a symbol" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(default_thumbnail: :get_a_default_thumbnail)
end

it "calls that helper method" do
allow(view_context).to receive(:get_a_default_thumbnail).with(document, {}).and_return('<img src="image.jpg">')
expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '<img src="image.jpg">'
end
end

context "and is a proc" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(default_thumbnail: lambda { |_, _| '<img src="image.jpg">' })
end

it "calls that lambda" do
expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '<img src="image.jpg">'
end
end
end

context "when no thumbnail is configured" do
it { is_expected.to be_nil }
end
Expand Down

0 comments on commit e3ea92e

Please sign in to comment.