From e3ea92e86f972c3aaa6a704fb814a46b080c4973 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Sun, 27 Aug 2017 08:33:38 -0700 Subject: [PATCH] Add 'default_thumbnail' view configuration 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 --- .../blacklight/thumbnail_presenter.rb | 35 ++++++++++++--- spec/helpers/catalog_helper_spec.rb | 13 +++--- spec/presenters/thumbnail_presenter_spec.rb | 44 +++++++++++++++++++ 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/app/presenters/blacklight/thumbnail_presenter.rb b/app/presenters/blacklight/thumbnail_presenter.rb index d8c5847e0f..ea520fffed 100644 --- a/app/presenters/blacklight/thumbnail_presenter.rb +++ b/app/presenters/blacklight/thumbnail_presenter.rb @@ -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 ## @@ -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 diff --git a/spec/helpers/catalog_helper_spec.rb b/spec/helpers/catalog_helper_spec.rb index 375eb9e18a..940b4bce29 100644 --- a/spec/helpers/catalog_helper_spec.rb +++ b/spec/helpers/catalog_helper_spec.rb @@ -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 diff --git a/spec/presenters/thumbnail_presenter_spec.rb b/spec/presenters/thumbnail_presenter_spec.rb index 8fb4f1872f..f15231ee71 100644 --- a/spec/presenters/thumbnail_presenter_spec.rb +++ b/spec/presenters/thumbnail_presenter_spec.rb @@ -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 @@ -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('') + expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '' + 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('') + expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '' + end + end + + context "and is a proc" do + let(:config) do + Blacklight::OpenStructWithHashAccess.new(default_thumbnail: lambda { |_, _| '' }) + end + + it "calls that lambda" do + expect(presenter.thumbnail_tag({}, suppress_link: true)).to eq '' + end + end + end + context "when no thumbnail is configured" do it { is_expected.to be_nil } end