Skip to content

Commit

Permalink
Use the DocumentMetadataComponent to render email + sms fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Jan 31, 2023
1 parent e4bd48b commit 745a9ce
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 196 deletions.
2 changes: 2 additions & 0 deletions app/components/blacklight/document_metadata_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DocumentMetadataComponent < Blacklight::Component
with_collection_parameter :fields

# @param fields [Enumerable<Blacklight::FieldPresenter>] 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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:disble 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
27 changes: 0 additions & 27 deletions app/models/concerns/blacklight/document/email.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/models/concerns/blacklight/document/sms.rb

This file was deleted.

25 changes: 16 additions & 9 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,31 @@ 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<String,Configuration::Field>] 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?

yield name, field_config, field_presenter
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

##
Expand Down Expand Up @@ -150,7 +152,12 @@ def field_presenter(field_config, options = {})
end

def field_presenter_options
{}
@field_presenter_options || {}
end

# Overridden by subclasses
def fields
[]
end
end
end
7 changes: 4 additions & 3 deletions app/views/record_mailer/email_record.text.erb
Original file line number Diff line number Diff line change
@@ -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) %>
8 changes: 4 additions & 4 deletions app/views/record_mailer/sms_record.text.erb
Original file line number Diff line number Diff line change
@@ -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 %>
4 changes: 3 additions & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions lib/generators/blacklight/templates/solr_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 0 additions & 57 deletions spec/models/blacklight/document/email_spec.rb

This file was deleted.

58 changes: 0 additions & 58 deletions spec/models/blacklight/document/sms_spec.rb

This file was deleted.

2 changes: 0 additions & 2 deletions spec/models/record_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 745a9ce

Please sign in to comment.