Skip to content

Commit

Permalink
Allow thumbnail_field config to take an array of possible fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Aug 23, 2017
1 parent daa693c commit 75669b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/presenters/blacklight/thumbnail_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ def exists?
# @param [Hash] image_options to pass to the image tag
# @param [Hash] url_options to pass to #link_to_document
# @return [String]
# rubocop:disable Lint/AssignmentInCondition
def thumbnail_tag image_options = {}, url_options = {}
return unless value = thumbnail_value(image_options)
value = thumbnail_value(image_options)
return unless value

if url_options == false || url_options[:suppress_link]
value
else
view_context.link_to_document document, value, url_options
end
end
# rubocop:enable Lint/AssignmentInCondition

private

Expand All @@ -48,8 +48,8 @@ def thumbnail_value(image_options)
if thumbnail_method
view_context.send(thumbnail_method, document, image_options)
elsif thumbnail_field
url = document.first(thumbnail_field)
view_context.image_tag url, image_options if url.present?
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?
end
end
end
Expand Down
31 changes: 29 additions & 2 deletions spec/presenters/thumbnail_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
it { is_expected.to be true }
end

context "when thumbnail_field is configured" do
context "when thumbnail_field is configured as a single field" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(thumbnail_field: :xyz)
end
Expand All @@ -33,6 +33,18 @@
end
end

context "when thumbnail_field is configured as an array of fields" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(thumbnail_field: [:rst, :uvw, :xyz])
end

context "and the field exists in the document" do
let(:document) { SolrDocument.new('xyz' => 'image.png') }

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 @@ -88,7 +100,6 @@
end

it "creates an image tag from the given field" do
#allow(document).to receive(:has?).with(:xyz).and_return(true)
allow(document).to receive(:first).with(:xyz).and_return("http://example.com/some.jpg")
allow(view_context).to receive(:image_tag).with("http://example.com/some.jpg", {}).and_return('<img src="image.jpg">')
expect(view_context).to receive(:link_to_document).with(document, '<img src="image.jpg">', {})
Expand All @@ -101,6 +112,22 @@
end
end

context "when thumbnail_field is configured as an array of fields" do
let(:config) do
Blacklight::OpenStructWithHashAccess.new(thumbnail_field: [:rst, :uvw, :xyz])
end

context "and the field exists in the document" do
let(:document) { SolrDocument.new(xyz: 'http://example.com/some.jpg') }

it "creates an image tag from the given field" do
allow(view_context).to receive(:image_tag).with("http://example.com/some.jpg", {}).and_return('<img src="image.jpg">')
expect(view_context).to receive(:link_to_document).with(document, '<img src="image.jpg">', {}).and_return('<a><img></a>')
expect(presenter.thumbnail_tag).to eq '<a><img></a>'
end
end
end

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

0 comments on commit 75669b4

Please sign in to comment.