From 616ee85b57ccfd116663bac4e97d3c955bca126c Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Mon, 15 Aug 2022 17:55:23 -0700 Subject: [PATCH] Use the DocumentMetadataComponent to render email + sms fields --- .../blacklight/document_metadata_component.rb | 2 + ...adata_field_plain_text_layout_component.rb | 17 ++++++ .../concerns/blacklight/document/email.rb | 27 --------- .../concerns/blacklight/document/sms.rb | 25 -------- .../blacklight/document_presenter.rb | 20 ++++--- app/views/record_mailer/email_record.text.erb | 7 ++- app/views/record_mailer/sms_record.text.erb | 8 +-- lib/blacklight/configuration.rb | 4 +- .../blacklight/templates/solr_document.rb | 6 -- spec/controllers/catalog_controller_spec.rb | 4 -- spec/models/blacklight/document/email_spec.rb | 57 ------------------ spec/models/blacklight/document/sms_spec.rb | 58 ------------------- spec/models/record_mailer_spec.rb | 2 - 13 files changed, 41 insertions(+), 196 deletions(-) create mode 100644 app/components/blacklight/metadata_field_plain_text_layout_component.rb delete mode 100644 app/models/concerns/blacklight/document/email.rb delete mode 100644 app/models/concerns/blacklight/document/sms.rb delete mode 100644 spec/models/blacklight/document/email_spec.rb delete mode 100644 spec/models/blacklight/document/sms_spec.rb diff --git a/app/components/blacklight/document_metadata_component.rb b/app/components/blacklight/document_metadata_component.rb index c545752484..e68a49dd2d 100644 --- a/app/components/blacklight/document_metadata_component.rb +++ b/app/components/blacklight/document_metadata_component.rb @@ -8,6 +8,7 @@ class DocumentMetadataComponent < Blacklight::Component with_collection_parameter :fields # @param fields [Enumerable] Document field presenters + # rubocop:disable Metrics/ParameterLists def initialize(fields: [], tag: 'dl', classes: %w[document-metadata dl-invert row], show: false, view_type: nil, field_layout: nil, **component_args) @fields = fields @tag = tag @@ -17,6 +18,7 @@ def initialize(fields: [], tag: 'dl', classes: %w[document-metadata dl-invert ro @field_layout = field_layout @component_args = component_args end + # rubocop:enable Metrics/ParameterLists def before_render return unless fields diff --git a/app/components/blacklight/metadata_field_plain_text_layout_component.rb b/app/components/blacklight/metadata_field_plain_text_layout_component.rb new file mode 100644 index 0000000000..954de2b795 --- /dev/null +++ b/app/components/blacklight/metadata_field_plain_text_layout_component.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Blacklight + class MetadataFieldPlainTextLayoutComponent < Blacklight::MetadataFieldLayoutComponent + with_collection_parameter :field + + def initialize(field:, **kwargs) + super(field: field, **kwargs, value_tag: nil) + end + + # rubocop:disable Rails/OutputSafety + def call + [label.to_s.strip, helpers.strip_tags(CGI.unescape_html(safe_join(values, "\n")).strip)].compact.join(' ').html_safe + end + # rubocop:enable Rails/OutputSafety + end +end diff --git a/app/models/concerns/blacklight/document/email.rb b/app/models/concerns/blacklight/document/email.rb deleted file mode 100644 index 4c163c2659..0000000000 --- a/app/models/concerns/blacklight/document/email.rb +++ /dev/null @@ -1,27 +0,0 @@ -# frozen_string_literal: true - -# This module provides the body of an email export based on the document's semantic values -module Blacklight::Document::Email - # Return a text string that will be the body of the email - def to_email_text(config = nil) - body = [] - - if config - body = config.email_fields.map do |name, field| - values = [self[name]].flatten - "#{field.label} #{values.join(' ')}" if self[name].present? - end - end - - # Use to_semantic_values for backwards compatibility - if body.empty? - semantics = to_semantic_values - body << I18n.t('blacklight.email.text.title', value: semantics[:title].join(" ")) if semantics[:title].present? - body << I18n.t('blacklight.email.text.author', value: semantics[:author].join(" ")) if semantics[:author].present? - body << I18n.t('blacklight.email.text.format', value: semantics[:format].join(" ")) if semantics[:format].present? - body << I18n.t('blacklight.email.text.language', value: semantics[:language].join(" ")) if semantics[:language].present? - end - - return body.join("\n") unless body.empty? - end -end diff --git a/app/models/concerns/blacklight/document/sms.rb b/app/models/concerns/blacklight/document/sms.rb deleted file mode 100644 index a51874485b..0000000000 --- a/app/models/concerns/blacklight/document/sms.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -# This module provides the body of an email export based on the document's semantic values -module Blacklight::Document::Sms - # Return a text string that will be the body of the email - def to_sms_text(config = nil) - body = [] - - if config - body = config.sms_fields.map do |name, field| - values = [self[name]].flatten - "#{field.label} #{values.first}" if self[name].present? - end - end - - # Use to_semantic_values for backwards compatibility - if body.empty? - semantics = to_semantic_values - body << I18n.t('blacklight.sms.text.title', value: semantics[:title].first) if semantics[:title].present? - body << I18n.t('blacklight.sms.text.author', value: semantics[:author].first) if semantics[:author].present? - end - - return body.join unless body.empty? - end -end diff --git a/app/presenters/blacklight/document_presenter.rb b/app/presenters/blacklight/document_presenter.rb index 2914e8b2cb..1bb87ed3d1 100644 --- a/app/presenters/blacklight/document_presenter.rb +++ b/app/presenters/blacklight/document_presenter.rb @@ -11,18 +11,20 @@ class DocumentPresenter # @param [SolrDocument] document # @param [ActionView::Base] view_context scope for linking and generating urls # @param [Blacklight::Configuration] configuration - def initialize(document, view_context, configuration = view_context.blacklight_config) + def initialize(document, view_context, configuration = view_context.blacklight_config, view_config: nil, field_presenter_options: {}) @document = document @view_context = view_context @configuration = configuration + @view_config = view_config + @field_presenter_options = field_presenter_options end # @return [Hash] all the fields for this index view that should be rendered - def fields_to_render - return to_enum(:fields_to_render) unless block_given? + def fields_to_render(document_fields = fields, **kwargs) + return to_enum(:fields_to_render, document_fields, **kwargs) unless block_given? - fields.each do |name, field_config| - field_presenter = field_presenter(field_config) + document_fields.each do |name, field_config| + field_presenter = field_presenter(field_config, kwargs) next unless field_presenter.render_field? && field_presenter.any? @@ -30,10 +32,10 @@ def fields_to_render end end - def field_presenters - return to_enum(:field_presenters) unless block_given? + def field_presenters(document_fields = fields, **kwargs) + return to_enum(:field_presenters, document_fields, **kwargs) unless block_given? - fields_to_render.each { |_, _, config| yield config } + fields_to_render(document_fields, **kwargs).each { |_, _, config| yield config } end ## @@ -150,7 +152,7 @@ def field_presenter(field_config, options = {}) end def field_presenter_options - {} + @field_presenter_options || {} end end end diff --git a/app/views/record_mailer/email_record.text.erb b/app/views/record_mailer/email_record.text.erb index e030647fdb..683e3c24e6 100644 --- a/app/views/record_mailer/email_record.text.erb +++ b/app/views/record_mailer/email_record.text.erb @@ -1,6 +1,7 @@ +<%= t('blacklight.email.text.message', message: @message) %> + <% @documents.each do |document| %> -<%= document.to_email_text %> -<%= t('blacklight.email.text.url', :url =>polymorphic_url(document, @url_gen_params)) %> + <%= render Blacklight::DocumentMetadataComponent.new(fields: document_presenter(document).field_presenters(blacklight_config.email_fields), tag: nil, field_layout: Blacklight::MetadataFieldPlainTextLayoutComponent, field_presenter_options: { format: 'text' }) %> + <%= t('blacklight.email.text.url', url: polymorphic_url(document, @url_gen_params)) %> <% end %> -<%= t('blacklight.email.text.message', :message => @message) %> diff --git a/app/views/record_mailer/sms_record.text.erb b/app/views/record_mailer/sms_record.text.erb index a5e263b2d9..b86b8be1bc 100644 --- a/app/views/record_mailer/sms_record.text.erb +++ b/app/views/record_mailer/sms_record.text.erb @@ -1,4 +1,4 @@ -<% @documents.each do |document| %> -<%= document.to_sms_text(@config) %> -<%= t('blacklight.sms.text.url', :url => polymorphic_url(document, @url_gen_params) ) %> -<% end %> +<% @documents.each do |document| %> + <%= render Blacklight::DocumentMetadataComponent.new(fields: document_presenter(document).field_presenters(blacklight_config.sms_fields), tag: nil, field_layout: Blacklight::MetadataFieldPlainTextLayoutComponent, field_presenter_options: { format: 'text' }) %> + <%= t('blacklight.sms.text.url', url: polymorphic_url(document, @url_gen_params)) %> +<% end %> diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index 458127efc1..d52083bcfa 100644 --- a/lib/blacklight/configuration.rb +++ b/lib/blacklight/configuration.rb @@ -200,7 +200,9 @@ def initialized_default_configuration? ViewConfig, default: { top_level_config: :index }, show: { top_level_config: :show }, - citation: { parent_config: :show } + citation: { parent_config: :show }, + email_action: { top_level_config: :email }, + sms_action: { top_level_config: :sms } ) # @!attribute sms diff --git a/lib/generators/blacklight/templates/solr_document.rb b/lib/generators/blacklight/templates/solr_document.rb index f089cd9fba..49801ce2ab 100644 --- a/lib/generators/blacklight/templates/solr_document.rb +++ b/lib/generators/blacklight/templates/solr_document.rb @@ -6,12 +6,6 @@ class <%= model_name.classify %> # self.unique_key = 'id' - # Email uses the semantic field mappings below to generate the body of an email. - SolrDocument.use_extension(Blacklight::Document::Email) - - # SMS uses the semantic field mappings below to generate the body of an SMS email. - SolrDocument.use_extension(Blacklight::Document::Sms) - # DublinCore uses the semantic field mappings below to assemble an OAI-compliant Dublin Core document # Semantic mappings of solr stored fields. Fields may be multi or # single valued. See Blacklight::Document::SemanticFields#field_semantics diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 14439e4640..c9b8b16a04 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -469,16 +469,12 @@ def export_as_mock describe "email/sms" do before do - mock_document.extend(Blacklight::Document::Sms) - mock_document.extend(Blacklight::Document::Email) allow(mock_document).to receive(:to_semantic_values).and_return({}) allow(mock_document).to receive(:to_model).and_return(SolrDocument.new(id: 'my_fake_doc')) allow(controller).to receive(:search_service).and_return(search_service) expect(search_service).to receive(:fetch).and_return([mock_document]) request.env["HTTP_REFERER"] = "/catalog/#{doc_id}" - SolrDocument.use_extension(Blacklight::Document::Email) - SolrDocument.use_extension(Blacklight::Document::Sms) end describe "email", api: false do diff --git a/spec/models/blacklight/document/email_spec.rb b/spec/models/blacklight/document/email_spec.rb deleted file mode 100644 index 68c2a784e6..0000000000 --- a/spec/models/blacklight/document/email_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe "Blacklight::Document::Email" do - let(:config) do - Blacklight::Configuration.new - end - - before(:all) do - SolrDocument.use_extension(Blacklight::Document::Email) - end - - it "onlies return values that are available in the field semantics" do - doc = SolrDocument.new(id: "1234", title_tsim: "My Title") - email_body = doc.to_email_text - expect(email_body).to match(/Title: My Title/) - expect(email_body).not_to match(/Author/) - end - - it "handles multi-values fields correctly" do - doc = SolrDocument.new(id: "1234", title_tsim: ["My Title", "My Alt. Title"]) - email_body = doc.to_email_text - expect(email_body).to match(/Title: My Title My Alt. Title/) - end - - it "returns nil if there are no valid field semantics to build the email body from" do - doc = SolrDocument.new(id: "1234") - expect(doc.to_email_text).to be_nil - end - - context "we pass in configuration with email fields set" do - it "uses the email fields for to_email_text" do - config.add_email_field("foo", label: "Foo:") - config.add_email_field("bar", label: "Bar:") - doc = SolrDocument.new(id: "1234", foo: ["Fuzz Fuzz", "Fizz Fizz"], bar: ["Buzz Buzz", "Bizz Bizz"]) - - expect(doc.to_email_text(config)).to eq("Foo: Fuzz Fuzz Fizz Fizz\nBar: Buzz Buzz Bizz Bizz") - end - end - - context "we pass in configuration with email fields no set" do - it "falls back on default semantics setup" do - doc = SolrDocument.new(id: "1234", title_tsim: ["My Title", "My Alt. Title"]) - email_body = doc.to_email_text(config) - expect(email_body).to match(/Title: My Title/) - end - end - - context "document field is single valued" do - it "handles the single value field correctly" do - config.add_email_field("foo", label: "Foo:") - config.add_email_field("bar", label: "Bar:") - doc = SolrDocument.new(id: "1234", foo: "Fuzz Fuzz", bar: ["Buzz Buzz", "Bizz Bizz"]) - - expect(doc.to_email_text(config)).to eq("Foo: Fuzz Fuzz\nBar: Buzz Buzz Bizz Bizz") - end - end -end diff --git a/spec/models/blacklight/document/sms_spec.rb b/spec/models/blacklight/document/sms_spec.rb deleted file mode 100644 index 5e20b86db9..0000000000 --- a/spec/models/blacklight/document/sms_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe "Blacklight::Document::Email" do - let(:config) do - Blacklight::Configuration.new - end - - before(:all) do - SolrDocument.use_extension(Blacklight::Document::Sms) - end - - it "onlies return values that are available in the field semantics" do - doc = SolrDocument.new(id: "1234", title_tsim: "My Title", author_tsim: "Joe Schmoe") - sms_text = doc.to_sms_text - expect(sms_text).to match(/My Title by Joe Schmoe/) - end - - it "handles multi-values fields correctly and only take the first" do - doc = SolrDocument.new(id: "1234", title_tsim: ["My Title", "My Alt. Title"]) - sms_text = doc.to_sms_text - expect(sms_text).to match(/My Title/) - expect(sms_text).not_to match(/My Alt\. Title/) - end - - it "returns nil if there are no valid field semantics to build the email body from" do - doc = SolrDocument.new(id: "1234") - expect(doc.to_sms_text).to be_nil - end - - context "we pass in configuration with sms fields set" do - it "uses the sms fields for to_sms_text" do - config.add_sms_field("foo", label: "Foo:") - config.add_sms_field("bar", label: " by") - doc = SolrDocument.new(id: "1234", foo: ["Fuzz Fuzz", "Fizz Fizz"], bar: ["Buzz Buzz", "Bizz Bizz"]) - - expect(doc.to_sms_text(config)).to eq("Foo: Fuzz Fuzz by Buzz Buzz") - end - end - - context "we pass in configuration with sms fields no set" do - it "falls back on default semantics setup" do - doc = SolrDocument.new(id: "1234", title_tsim: ["My Title", "My Alt. Title"]) - sms_text = doc.to_sms_text(config) - expect(sms_text).to match(/My Title/) - expect(sms_text).not_to match(/My Alt\. Title/) - end - end - - context "document field is single valued" do - it "handles the single value field correctly" do - config.add_sms_field("foo", label: "Foo:") - config.add_sms_field("bar", label: " by") - doc = SolrDocument.new(id: "1234", foo: "Fuzz Fuzz", bar: ["Buzz Buzz", "Bizz Bizz"]) - - expect(doc.to_sms_text(config)).to eq("Foo: Fuzz Fuzz by Buzz Buzz") - end - end -end diff --git a/spec/models/record_mailer_spec.rb b/spec/models/record_mailer_spec.rb index 50820b61a2..195221de61 100644 --- a/spec/models/record_mailer_spec.rb +++ b/spec/models/record_mailer_spec.rb @@ -11,8 +11,6 @@ before do allow(described_class).to receive(:default).and_return(from: 'no-reply@projectblacklight.org') - SolrDocument.use_extension(Blacklight::Document::Email) - SolrDocument.use_extension(Blacklight::Document::Sms) @documents = [document] end