diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6a47de7656..2a902e4cde 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -371,7 +371,9 @@ Rails/HelperInstanceVariable: Exclude: - 'app/helpers/blacklight/blacklight_helper_behavior.rb' - 'app/helpers/blacklight/catalog_helper_behavior.rb' + - 'app/helpers/blacklight/document_helper_behavior.rb' - 'app/helpers/blacklight/component_helper_behavior.rb' + - 'app/helpers/blacklight/layout_helper_behavior.rb' - 'app/helpers/blacklight/render_partials_helper_behavior.rb' # Offense count: 2 diff --git a/app/components/blacklight/document_metadata_component.html.erb b/app/components/blacklight/document_metadata_component.html.erb index f3f361aa67..137ab56de7 100644 --- a/app/components/blacklight/document_metadata_component.html.erb +++ b/app/components/blacklight/document_metadata_component.html.erb @@ -1,5 +1,7 @@ -
+<% content = capture do %> <% fields.each do |field| -%> <%= field %> <% end -%> -
+<% end %> + +<%= @tag.nil? ? content : tag.public_send(@tag, content, class: @classes, **@component_args) %> diff --git a/app/components/blacklight/document_metadata_component.rb b/app/components/blacklight/document_metadata_component.rb index a814e7156a..e68a49dd2d 100644 --- a/app/components/blacklight/document_metadata_component.rb +++ b/app/components/blacklight/document_metadata_component.rb @@ -8,17 +8,23 @@ class DocumentMetadataComponent < Blacklight::Component with_collection_parameter :fields # @param fields [Enumerable] Document field presenters - def initialize(fields: [], show: false, view_type: nil) + # 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 + @classes = classes @show = show @view_type = view_type + @field_layout = field_layout + @component_args = component_args end + # rubocop:enable Metrics/ParameterLists def before_render return unless fields @fields.each do |field| - with_field(component: field.component, field: field, show: @show, view_type: @view_type) + with_field(component: field.component, field: field, show: @show, view_type: @view_type, layout: @field_layout) end end diff --git a/app/components/blacklight/metadata_field_layout_component.rb b/app/components/blacklight/metadata_field_layout_component.rb index c7dcc9cf52..3cdce68ba0 100644 --- a/app/components/blacklight/metadata_field_layout_component.rb +++ b/app/components/blacklight/metadata_field_layout_component.rb @@ -5,18 +5,21 @@ class MetadataFieldLayoutComponent < Blacklight::Component with_collection_parameter :field renders_one :label renders_many :values, (lambda do |value: nil, &block| - if block - content_tag :dd, class: "#{@value_class} blacklight-#{@key}", &block + if @value_tag.nil? + block&.call || value + elsif block + content_tag @value_tag, class: "#{@value_class} blacklight-#{@key}", &block else - content_tag :dd, value, class: "#{@value_class} blacklight-#{@key}" + content_tag @value_tag, value, class: "#{@value_class} blacklight-#{@key}" end end) # @param field [Blacklight::FieldPresenter] - def initialize(field:, label_class: 'col-md-3', value_class: 'col-md-9') + def initialize(field:, value_tag: 'dd', label_class: 'col-md-3', value_class: 'col-md-9') @field = field @key = @field.key.parameterize @label_class = label_class + @value_tag = value_tag @value_class = value_class end end 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/helpers/blacklight/blacklight_helper_behavior.rb b/app/helpers/blacklight/blacklight_helper_behavior.rb index 54d2e0f82c..e83b6fbd2a 100644 --- a/app/helpers/blacklight/blacklight_helper_behavior.rb +++ b/app/helpers/blacklight/blacklight_helper_behavior.rb @@ -6,8 +6,6 @@ module Blacklight::BlacklightHelperBehavior include Blacklight::LayoutHelperBehavior include Blacklight::IconHelperBehavior - # @!group Layout helpers - ## # Get the name of this application from an i18n string # key: blacklight.application_name @@ -23,63 +21,6 @@ def application_name end end - ## - # Get the page's HTML title - # - # @return [String] - def render_page_title - (content_for(:page_title) if content_for?(:page_title)) || @page_title || application_name - end - - ## - # Create links from a documents dynamically - # provided export formats. - # - # Returns empty string if no links available. - # - # @param [SolrDocument] document - # @param [Hash] options - # @option options [Boolean] :unique ensures only one link is output for every - # content type, e.g. as required by atom - # @option options [Array] :exclude array of format shortnames to not include in the output - # @return [String] - def render_link_rel_alternates(document = @document, options = {}) - return if document.nil? - - document_presenter(document).link_rel_alternates(options) - end - - ## - # Render classes for the element - # @return [String] - def render_body_class - extra_body_classes.join " " - end - - ## - # List of classes to be applied to the element - # @see render_body_class - # @return [Array] - def extra_body_classes - @extra_body_classes ||= ["blacklight-#{controller.controller_name}", "blacklight-#{[controller.controller_name, controller.action_name].join('-')}"] - end - - ## - # Get the current "view type" (and ensure it is a valid type) - # - # @param [Hash] query_params the query parameters to check - # @return [Symbol] - def document_index_view_type query_params = params - view_param = query_params[:view] - view_param ||= session[:preferred_view] - if view_param && document_index_views.key?(view_param.to_sym) - view_param.to_sym - else - default_document_index_view_type - end - end - - # @!group Search result helpers ## # Render a partial of an arbitrary format inside a # template of a different format. (e.g. render an HTML @@ -97,45 +38,8 @@ def with_format(format) nil end - ## - # Should we render a grouped response (because the response - # contains a grouped response instead of the normal response) - # - # Default to false if there's no response object available (sometimes the case - # for tests, but might happen in other circumstances too..) - # @return [Boolean] - def render_grouped_response? response = @response - response&.grouped? - end - - ## - # Returns a document presenter for the given document - def document_presenter(document) - document_presenter_class(document).new(document, self) - end - - ## - # Override this method if you want to use a differnet presenter for your documents - # @param [Blacklight::Document] _document optional, here for extension + backwards compatibility only - def document_presenter_class(_document = nil) - case action_name - when 'show', 'citation' - blacklight_config.view_config(:show, action_name: action_name).document_presenter_class - else - blacklight_config.view_config(document_index_view_type, action_name: action_name).document_presenter_class - end - end - # @return [Class] def search_bar_presenter_class blacklight_config.view_config(action_name: :index).search_bar_presenter_class end - - # @!group Layout helpers - ## - # Open Search discovery tag for HTML links - # @return [String] - def opensearch_description_tag title, href - tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search" - end end diff --git a/app/helpers/blacklight/catalog_helper_behavior.rb b/app/helpers/blacklight/catalog_helper_behavior.rb index 6fca652b76..f9b234178e 100644 --- a/app/helpers/blacklight/catalog_helper_behavior.rb +++ b/app/helpers/blacklight/catalog_helper_behavior.rb @@ -4,6 +4,7 @@ module Blacklight::CatalogHelperBehavior include Blacklight::ConfigurationHelperBehavior include Blacklight::ComponentHelperBehavior + include Blacklight::DocumentHelperBehavior include Blacklight::FacetsHelperBehavior include Blacklight::RenderPartialsHelperBehavior @@ -113,40 +114,6 @@ def current_per_page (@response.rows if @response && @response.rows > 0) || params.fetch(:per_page, blacklight_config.default_per_page).to_i end - ## - # Get the classes to add to a document's div - # - # @param [Blacklight::Document] document - # @return [String] - def render_document_class(document = @document) - types = document_presenter(document).display_type - return if types.blank? - - Array(types).compact.map do |t| - "#{document_class_prefix}#{t.try(:parameterize) || t}" - end.join(' ') - end - - ## - # Return a prefix for the document classes infered from the document - # @see #render_document_class - # @return [String] - def document_class_prefix - 'blacklight-' - end - - ## - # Render the sidebar partial for a document - # This is used as an integration point by downstream apps to add to the - # default sidebar. - # See: https://github.com/geoblacklight/geoblacklight/blob/7d3c31c7af3362879b97e2c1351a2496c728c59c/app/helpers/blacklight_helper.rb#L7 - # - # @param [SolrDocument] document - # @return [String] - def render_document_sidebar_partial(document) - render 'show_sidebar', document: document - end - ## # Should we display the sort and per page widget? # @@ -167,26 +134,6 @@ def show_pagination? response = nil response.limit_value > 0 end - ## - # return the Bookmarks on a set of documents (all bookmarks on the page) - # @private - # @return [Enumerable] - def current_bookmarks - @current_bookmarks ||= begin - documents = @document.presence || @response.documents - current_or_guest_user.bookmarks_for_documents(Array(documents)).to_a - end - end - private :current_bookmarks - - ## - # Check if the document is in the user's bookmarks - # @param [Blacklight::Document] document - # @return [Boolean] - def bookmarked? document - current_bookmarks.any? { |x| x.document_id == document.id && x.document_type == document.class } - end - # Render an html appropriate string for a selected facet field and values # # @see #render_search_to_page_title @@ -233,6 +180,32 @@ def render_search_to_page_title(search_state_or_params) constraints.join(' / ') end + ## + # Should we render a grouped response (because the response + # contains a grouped response instead of the normal response) + # + # Default to false if there's no response object available (sometimes the case + # for tests, but might happen in other circumstances too..) + # @return [Boolean] + def render_grouped_response? response = @response + response&.grouped? + end + + ## + # Get the current "view type" (and ensure it is a valid type) + # + # @param [Hash] query_params the query parameters to check + # @return [Symbol] + def document_index_view_type query_params = params || {} + view_param = query_params[:view] + view_param ||= session[:preferred_view] if respond_to?(:session) + if view_param && document_index_views.key?(view_param.to_sym) + view_param.to_sym + else + default_document_index_view_type + end + end + private # @param [String] format diff --git a/app/helpers/blacklight/component_helper_behavior.rb b/app/helpers/blacklight/component_helper_behavior.rb index fd0a1af165..11c3f1408e 100644 --- a/app/helpers/blacklight/component_helper_behavior.rb +++ b/app/helpers/blacklight/component_helper_behavior.rb @@ -2,17 +2,6 @@ module Blacklight module ComponentHelperBehavior - ## - # Render "document actions" area for navigation header - # (normally renders "Saved Searches", "History", "Bookmarks") - # These things are added by add_nav_action - # - # @param [Hash] options - # @return [String] - def render_nav_actions(options = {}, &block) - render_filtered_partials(blacklight_config.navbar.partials, options, &block) - end - ## # Render "document actions" area for search results view # (normally renders next to title in the list view) diff --git a/app/helpers/blacklight/document_helper_behavior.rb b/app/helpers/blacklight/document_helper_behavior.rb new file mode 100644 index 0000000000..f4355a8939 --- /dev/null +++ b/app/helpers/blacklight/document_helper_behavior.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +# Helper methods for catalog-like controllers that work with documents +module Blacklight::DocumentHelperBehavior + ## + # Get the classes to add to a document's div + # + # @param [Blacklight::Document] document + # @return [String] + def render_document_class(document = @document) + types = document_presenter(document).display_type + return if types.blank? + + Array(types).compact.map do |t| + "#{document_class_prefix}#{t.try(:parameterize) || t}" + end.join(' ') + end + + ## + # Return a prefix for the document classes infered from the document + # @see #render_document_class + # @return [String] + def document_class_prefix + 'blacklight-' + end + + ## + # Render the sidebar partial for a document + # This is used as an integration point by downstream apps to add to the + # default sidebar. + # See: https://github.com/geoblacklight/geoblacklight/blob/7d3c31c7af3362879b97e2c1351a2496c728c59c/app/helpers/blacklight_helper.rb#L7 + # + # @param [SolrDocument] document + # @return [String] + def render_document_sidebar_partial(document) + render 'show_sidebar', document: document + end + + ## + # return the Bookmarks on a set of documents (all bookmarks on the page) + # @private + # @return [Enumerable<Bookmark>] + def current_bookmarks + @current_bookmarks ||= begin + documents = @document.presence || @response.documents + current_or_guest_user.bookmarks_for_documents(Array(documents)).to_a + end + end + private :current_bookmarks + + ## + # Check if the document is in the user's bookmarks + # @param [Blacklight::Document] document + # @return [Boolean] + def bookmarked? document + current_bookmarks.any? { |x| x.document_id == document.id && x.document_type == document.class } + end + + ## + # Returns a document presenter for the given document + def document_presenter(document) + document_presenter_class(document).new(document, self) + end + + ## + # Override this method if you want to use a differnet presenter for your documents + # @param [Blacklight::Document] _document optional, here for extension + backwards compatibility only + def document_presenter_class(_document = nil) + case action_name + when 'show', 'citation' + blacklight_config.view_config(:show, action_name: action_name).document_presenter_class + else + blacklight_config.view_config(document_index_view_type, action_name: action_name).document_presenter_class + end + end +end diff --git a/app/helpers/blacklight/layout_helper_behavior.rb b/app/helpers/blacklight/layout_helper_behavior.rb index b385345ded..f25209610c 100644 --- a/app/helpers/blacklight/layout_helper_behavior.rb +++ b/app/helpers/blacklight/layout_helper_behavior.rb @@ -47,5 +47,64 @@ def sidebar_classes def container_classes 'container' end + + ## + # Render "document actions" area for navigation header + # (normally renders "Saved Searches", "History", "Bookmarks") + # These things are added by add_nav_action + # + # @param [Hash] options + # @return [String] + def render_nav_actions(options = {}, &block) + render_filtered_partials(blacklight_config.navbar.partials, options, &block) + end + + ## + # Open Search discovery tag for HTML <head> links + # @return [String] + def opensearch_description_tag title, href + tag :link, href: href, title: title, type: "application/opensearchdescription+xml", rel: "search" + end + + ## + # Get the page's HTML title + # + # @return [String] + def render_page_title + (content_for(:page_title) if content_for?(:page_title)) || @page_title || application_name + end + + ## + # Create <link rel="alternate"> links from a documents dynamically + # provided export formats. + # + # Returns empty string if no links available. + # + # @param [SolrDocument] document + # @param [Hash] options + # @option options [Boolean] :unique ensures only one link is output for every + # content type, e.g. as required by atom + # @option options [Array<String>] :exclude array of format shortnames to not include in the output + # @return [String] + def render_link_rel_alternates(document = @document, options = {}) + return if document.nil? + + document_presenter(document).link_rel_alternates(options) + end + + ## + # Render classes for the <body> element + # @return [String] + def render_body_class + extra_body_classes.join " " + end + + ## + # List of classes to be applied to the <body> element + # @see render_body_class + # @return [Array<String>] + def extra_body_classes + @extra_body_classes ||= ["blacklight-#{controller.controller_name}", "blacklight-#{[controller.controller_name, controller.action_name].join('-')}"] + end 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/models/record_mailer.rb b/app/models/record_mailer.rb index 99c6a7625b..ab322477f9 100644 --- a/app/models/record_mailer.rb +++ b/app/models/record_mailer.rb @@ -2,27 +2,21 @@ # Only works for documents with a #to_marc right now. class RecordMailer < ApplicationMailer - def email_record(documents, details, url_gen_params) - title = begin - title_field = details[:config].email.title_field - if title_field - [documents.first[title_field]].flatten.first - else - documents.first.to_semantic_values[:title] - end - rescue - I18n.t('blacklight.email.text.default_title') - end - - subject = I18n.t('blacklight.email.text.subject', - count: documents.length, - title: Array(title).first) + helper CatalogHelper + helper_method :blacklight_config, :blacklight_configuration_context + def email_record(documents, details, url_gen_params) @documents = documents @message = details[:message] @config = details[:config] @url_gen_params = url_gen_params + title = view_context.document_presenter(documents.first).html_title || I18n.t('blacklight.email.text.default_title') + + subject = I18n.t('blacklight.email.text.subject', + count: documents.length, + title: Array(title).first) + mail(to: details[:to], subject: subject) end @@ -33,4 +27,14 @@ def sms_record(documents, details, url_gen_params) mail(to: details[:to], subject: "") # rubocop:disable Rails/I18nLocaleTexts end + + def blacklight_config + @config || Blacklight.default_configuration + end + + ## + # Context in which to evaluate blacklight configuration conditionals + def blacklight_configuration_context + @blacklight_configuration_context ||= Blacklight::Configuration::Context.new(self) + end end diff --git a/app/presenters/blacklight/document_presenter.rb b/app/presenters/blacklight/document_presenter.rb index 2914e8b2cb..bb3336ceb7 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<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? @@ -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 ## @@ -116,11 +118,11 @@ def link_rel_alternates(options = {}) end def view_config - show_view_config + @view_config ||= show_view_config end def show_view_config - configuration.view_config(:show) + configuration.view_config(:show, action_name: view_context.action_name) end def inspect @@ -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/presenters/blacklight/index_presenter.rb b/app/presenters/blacklight/index_presenter.rb index 7f3a280dbf..fbbecb784a 100644 --- a/app/presenters/blacklight/index_presenter.rb +++ b/app/presenters/blacklight/index_presenter.rb @@ -3,7 +3,7 @@ module Blacklight class IndexPresenter < DocumentPresenter def view_config - configuration.view_config(view_context.document_index_view_type) + @view_config ||= configuration.view_config(view_context.document_index_view_type, action_name: view_context.action_name) end end end diff --git a/app/presenters/blacklight/rendering/abstract_step.rb b/app/presenters/blacklight/rendering/abstract_step.rb index 8882be4325..06b9e31471 100644 --- a/app/presenters/blacklight/rendering/abstract_step.rb +++ b/app/presenters/blacklight/rendering/abstract_step.rb @@ -20,6 +20,10 @@ def next_step(output_values) first, *rest = *stack first.new(output_values, config, document, context, options, rest).render end + + def html? + options[:format].nil? || options[:format].to_s == 'html' + end end end end diff --git a/app/presenters/blacklight/rendering/join.rb b/app/presenters/blacklight/rendering/join.rb index 0db649ca28..8696052174 100644 --- a/app/presenters/blacklight/rendering/join.rb +++ b/app/presenters/blacklight/rendering/join.rb @@ -5,7 +5,13 @@ module Rendering class Join < AbstractStep def render options = config.separator_options || {} - next_step(values.map { |x| x.html_safe? ? x : html_escape(x) }.to_sentence(options).html_safe) + if values.one? || values.none? + next_step(values.first) + elsif !html? + next_step(values.to_sentence(options)) + else + next_step(values.map { |x| x.html_safe? ? x : html_escape(x) }.to_sentence(options).html_safe) + end end private diff --git a/app/presenters/blacklight/rendering/link_to_facet.rb b/app/presenters/blacklight/rendering/link_to_facet.rb index 65d88e4231..622a34641b 100644 --- a/app/presenters/blacklight/rendering/link_to_facet.rb +++ b/app/presenters/blacklight/rendering/link_to_facet.rb @@ -4,7 +4,7 @@ module Blacklight module Rendering class LinkToFacet < AbstractStep def render - return next_step(values) unless config.link_to_facet + return next_step(values) unless config.link_to_facet && html? next_step(render_link) end diff --git a/app/presenters/blacklight/rendering/microdata.rb b/app/presenters/blacklight/rendering/microdata.rb index 1319af8c9d..7d953ae583 100644 --- a/app/presenters/blacklight/rendering/microdata.rb +++ b/app/presenters/blacklight/rendering/microdata.rb @@ -5,7 +5,7 @@ module Rendering class Microdata < AbstractStep include ActionView::Helpers::TagHelper def render - return next_step(values) unless config.itemprop + return next_step(values) unless config.itemprop && html? next_step(values.map { |x| itemprop(x, config.itemprop) }) 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/config/locales/blacklight.ar.yml b/config/locales/blacklight.ar.yml index 99030d11f2..ba6d22672d 100644 --- a/config/locales/blacklight.ar.yml +++ b/config/locales/blacklight.ar.yml @@ -101,10 +101,6 @@ ar: submit: 'إرسال' text: default_title: 'لا يوجد' - title: 'العنوان: %{value}' - author: 'المؤلف: %{value}' - format: 'الصيغة: %{value}' - language: 'اللغة: %{value}' subject: zero: 'لا يوجد سجل للمادة' one: 'سجل المادة: %{title}' @@ -130,8 +126,6 @@ ar: carrier_prompt: 'يرجى تحديد شركة الاتصالات' submit: 'إرسال' text: - title: '%{value}' - author: ' بواسطة %{value}' url: 'الرابط: %{url}' success: "تم الإرسال كرسالة نصية قصيرة" errors: diff --git a/config/locales/blacklight.ca.yml b/config/locales/blacklight.ca.yml index a5e44138b3..540dde812e 100644 --- a/config/locales/blacklight.ca.yml +++ b/config/locales/blacklight.ca.yml @@ -96,10 +96,6 @@ ca: submit: 'Enviar-ho' text: default_title: 'N/A' - title: 'Títol: %{value}' - author: 'Autor: %{value}' - format: 'Format: %{value}' - language: 'Llengua: %{value}' subject: one: 'Registre: %{title}' other: 'Registres' @@ -120,8 +116,6 @@ ca: carrier_prompt: 'Heu de seleccionar un operador' submit: 'Enviar' text: - title: '%{value}' - author: ' de %{value}' url: 'URL: %{url}' success: "SMS enviat" errors: diff --git a/config/locales/blacklight.de.yml b/config/locales/blacklight.de.yml index 0956792b4f..9e9634c61d 100644 --- a/config/locales/blacklight.de.yml +++ b/config/locales/blacklight.de.yml @@ -96,10 +96,6 @@ de: submit: 'Senden' text: default_title: 'N/A' - title: 'Titel: %{value}' - author: 'Autor: %{value}' - format: 'Format: %{value}' - language: 'Sprache: %{value}' subject: one: 'Artikeldatensatz: %{title}' other: 'Artikeldatensätze' @@ -120,8 +116,6 @@ de: carrier_prompt: 'Bitte wählen Sie Ihren Netzbetreiber aus' submit: 'Schicken' text: - title: '%{value}' - author: ' von %{value}' url: 'Link: %{url}' success: "SMS verschickt" errors: diff --git a/config/locales/blacklight.en.yml b/config/locales/blacklight.en.yml index f27deb7929..d2014d1528 100644 --- a/config/locales/blacklight.en.yml +++ b/config/locales/blacklight.en.yml @@ -96,10 +96,6 @@ en: submit: 'Send' text: default_title: 'N/A' - title: 'Title: %{value}' - author: 'Author: %{value}' - format: 'Format: %{value}' - language: 'Language: %{value}' subject: one: 'Item Record: %{title}' other: 'Item records' @@ -120,8 +116,6 @@ en: carrier_prompt: 'Please select your carrier' submit: 'Send' text: - title: '%{value}' - author: ' by %{value}' url: 'Link: %{url}' success: "SMS Sent" errors: diff --git a/config/locales/blacklight.es.yml b/config/locales/blacklight.es.yml index f84519362f..e21bfd9c3b 100644 --- a/config/locales/blacklight.es.yml +++ b/config/locales/blacklight.es.yml @@ -96,10 +96,6 @@ es: submit: 'Enviar' text: default_title: 'N/A' - title: 'Titulo: %{value}' - author: 'Autor: %{value}' - format: 'Formato: %{value}' - language: 'Idioma: %{value}' subject: one: 'Ficha artículo: %{title}' other: 'Ficha artículos' @@ -120,8 +116,6 @@ es: carrier_prompt: 'Por favor, seleccione su compañía telefónica' submit: 'Enviar' text: - title: '%{value}' - author: 'por %{value}' url: 'Link: %{url}' success: "SMS enviado" errors: diff --git a/config/locales/blacklight.fr.yml b/config/locales/blacklight.fr.yml index 6e6b582603..f5bb0274ad 100755 --- a/config/locales/blacklight.fr.yml +++ b/config/locales/blacklight.fr.yml @@ -96,10 +96,6 @@ fr: submit: 'Envoyer' text: default_title: 'N/A' - title: 'Titre : %{value}' - author: 'Auteur : %{value}' - format: 'Format : %{value}' - language: 'Langue : %{value}' subject: one: 'Une référence : %{title}' other: 'Des références bibliographiques' @@ -121,8 +117,6 @@ fr: carrier_prompt: 'Veuillez choisir votre opérateur' submit: 'Envoyer' text: - title: '%{value}' - author: ' by %{value}' url: 'Lien : %{url}' success: "SMS envoyé" diff --git a/config/locales/blacklight.hu.yml b/config/locales/blacklight.hu.yml index bfd305cc55..099d399495 100644 --- a/config/locales/blacklight.hu.yml +++ b/config/locales/blacklight.hu.yml @@ -96,10 +96,6 @@ hu: submit: 'Küldés' text: default_title: 'N/A' - title: 'Cím: %{value}' - author: 'Szerző: %{value}' - format: 'Formátum: %{value}' - language: 'Nyelv: %{value}' subject: one: 'Adatrekord: %{title}' other: 'Adatrekordok' @@ -120,8 +116,6 @@ hu: carrier_prompt: 'Kérjük válassza ki a szolgáltatót' submit: 'Küldés' text: - title: '%{value}' - author: ' írta %{value}' url: 'Link: %{url}' success: "SMS elküldve" errors: diff --git a/config/locales/blacklight.it.yml b/config/locales/blacklight.it.yml index 0b6cf84d87..5a8975935c 100644 --- a/config/locales/blacklight.it.yml +++ b/config/locales/blacklight.it.yml @@ -96,10 +96,6 @@ it: submit: 'Invia' text: default_title: 'N/A' - title: 'Titolo: %{value}' - author: 'Autore: %{value}' - format: 'Formato: %{value}' - language: 'Lingua: %{value}' subject: one: 'Numero di scheda: %{title}' other: 'Numeri di scheda' @@ -120,8 +116,6 @@ it: carrier_prompt: "Indicare l'operatore" submit: 'Inviare' text: - title: '%{value}' - author: ' da %{value}' url: 'Link: %{url}' success: "SMS inviato" errors: diff --git a/config/locales/blacklight.nl.yml b/config/locales/blacklight.nl.yml index 8e158242e9..3960d2d4b0 100644 --- a/config/locales/blacklight.nl.yml +++ b/config/locales/blacklight.nl.yml @@ -96,10 +96,6 @@ nl: submit: 'Verzenden' text: default_title: 'N/A' - title: 'Titel: %{value}' - author: 'Auteur: %{value}' - format: 'Formaat: %{value}' - language: 'Taal: %{value}' subject: one: 'Item Record: %{title}' other: 'Item records' @@ -120,8 +116,6 @@ nl: carrier_prompt: 'Gelieve uw carrier op te geven.' submit: 'Verzenden' text: - title: '%{value}' - author: ' door %{value}' url: 'Link: %{url}' success: "SMS Verzonden" errors: diff --git a/config/locales/blacklight.pt-BR.yml b/config/locales/blacklight.pt-BR.yml index 991a8936ad..932cd3139a 100644 --- a/config/locales/blacklight.pt-BR.yml +++ b/config/locales/blacklight.pt-BR.yml @@ -97,10 +97,6 @@ pt-BR: submit: 'Enviar' text: default_title: 'N/A' - title: 'Título: %{value}' - author: 'Autor: %{value}' - format: 'Formato: %{value}' - language: 'Idioma: %{value}' subject: one: 'Item: %{title}' other: 'Itens' @@ -121,8 +117,6 @@ pt-BR: carrier_prompt: 'Selecione a Operadora' submit: 'Enviar' text: - title: '%{value}' - author: ' por %{value}' url: 'Link: %{url}' success: 'SMS Enviado' errors: diff --git a/config/locales/blacklight.sq.yml b/config/locales/blacklight.sq.yml index c863b55cde..2fc979c6e1 100644 --- a/config/locales/blacklight.sq.yml +++ b/config/locales/blacklight.sq.yml @@ -96,10 +96,6 @@ sq: submit: 'Dërgo' text: default_title: 'N/A' - title: 'Titulli: %{value}' - author: 'Autori: %{value}' - format: 'Formati: %{value}' - language: 'Gjuha: %{value}' subject: one: 'Item Record: %{title}' other: 'Item records' @@ -120,8 +116,6 @@ sq: carrier_prompt: 'Ju lutemi zgjedheni operatorin' submit: 'Dërgo' text: - title: '%{value}' - author: ' nga %{value}' url: 'Link: %{url}' success: "SMS-i u dëgua" errors: diff --git a/config/locales/blacklight.zh.yml b/config/locales/blacklight.zh.yml index 912af4866d..dba88d8579 100644 --- a/config/locales/blacklight.zh.yml +++ b/config/locales/blacklight.zh.yml @@ -96,10 +96,6 @@ zh: submit: '发送' text: default_title: '无' - title: '标题: %{value}' - author: '作者: %{value}' - format: '格式: %{value}' - language: '语言: %{value}' subject: one: '记录: %{title}' other: '记录' @@ -120,8 +116,6 @@ zh: carrier_prompt: '请选择运营商' submit: '发送' text: - title: '%{value}' - author: ' 来自 %{value}' url: '链接: %{url}' success: "已发送短信" errors: diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index 458127efc1..45c2495930 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_record: { top_level_config: :email }, + sms_record: { 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..30fdac7392 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -468,17 +468,14 @@ def export_as_mock end describe "email/sms" do + let(:mock_document) { SolrDocument.new } + 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/helpers/blacklight_helper_spec.rb b/spec/helpers/blacklight_helper_spec.rb index 5efb5f6bfc..5c2f80afa9 100644 --- a/spec/helpers/blacklight_helper_spec.rb +++ b/spec/helpers/blacklight_helper_spec.rb @@ -134,18 +134,6 @@ end end - describe "render_grouped_response?" do - it "checks if the response ivar contains grouped data" do - assign(:response, instance_double(Blacklight::Solr::Response, grouped?: true)) - expect(helper.render_grouped_response?).to be true - end - - it "checks if the response param contains grouped data" do - response = instance_double(Blacklight::Solr::Response, grouped?: true) - expect(helper.render_grouped_response?(response)).to be true - end - end - describe "#opensearch_description_tag" do subject { helper.opensearch_description_tag 'title', 'href' } @@ -210,48 +198,4 @@ def stub_template(hash) end end end - - describe "#document_index_view_type" do - it "defaults to the default view" do - allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) - allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) - expect(helper.document_index_view_type).to eq :xyz - end - - it "uses the query parameter" do - allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) - expect(helper.document_index_view_type(view: :a)).to eq :a - end - - it "uses the default view if the requested view is not available" do - allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) - allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) - expect(helper.document_index_view_type(view: :c)).to eq :xyz - end - - context "when they have a preferred view" do - before do - session[:preferred_view] = :b - end - - context "and no view is specified" do - it "uses the saved preference" do - allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) - expect(helper.document_index_view_type).to eq :b - end - - it "uses the default view if the preference is not available" do - allow(helper).to receive(:document_index_views).and_return(a: 1) - expect(helper.document_index_view_type).to eq :a - end - end - - context "and a view is specified" do - it "uses the query parameter" do - allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) - expect(helper.document_index_view_type(view: :c)).to eq :c - end - end - end - end end diff --git a/spec/helpers/catalog_helper_spec.rb b/spec/helpers/catalog_helper_spec.rb index 79f33a6117..fbb4689fec 100644 --- a/spec/helpers/catalog_helper_spec.rb +++ b/spec/helpers/catalog_helper_spec.rb @@ -284,4 +284,60 @@ def mock_response args it { is_expected.to eq "foobar / Format: Book" } end end + + describe "render_grouped_response?" do + it "checks if the response ivar contains grouped data" do + assign(:response, instance_double(Blacklight::Solr::Response, grouped?: true)) + expect(helper.render_grouped_response?).to be true + end + + it "checks if the response param contains grouped data" do + response = instance_double(Blacklight::Solr::Response, grouped?: true) + expect(helper.render_grouped_response?(response)).to be true + end + end + + describe "#document_index_view_type" do + it "defaults to the default view" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) + expect(helper.document_index_view_type).to eq :xyz + end + + it "uses the query parameter" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + expect(helper.document_index_view_type(view: :a)).to eq :a + end + + it "uses the default view if the requested view is not available" do + allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + expect(helper.document_index_view_type(view: :c)).to eq :xyz + end + + context "when they have a preferred view" do + before do + session[:preferred_view] = :b + end + + context "and no view is specified" do + it "uses the saved preference" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) + expect(helper.document_index_view_type).to eq :b + end + + it "uses the default view if the preference is not available" do + allow(helper).to receive(:document_index_views).and_return(a: 1) + expect(helper.document_index_view_type).to eq :a + end + end + + context "and a view is specified" do + it "uses the query parameter" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) + expect(helper.document_index_view_type(view: :c)).to eq :c + end + end + end + end end 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..1a144c2cd8 100644 --- a/spec/models/record_mailer_spec.rb +++ b/spec/models/record_mailer_spec.rb @@ -2,7 +2,16 @@ RSpec.describe RecordMailer do let(:config) do - Blacklight::Configuration.new + Blacklight::Configuration.new do |config| + config.email.title_field = 'title_tsim' + config.add_email_field 'title_tsim', label: 'Title' + config.add_email_field "author_tsim", label: 'Author' + config.add_email_field "language_ssim", label: 'Language' + config.add_email_field "format", label: 'Format' + + config.add_sms_field 'title_tsim', label: 'Title' + config.add_sms_field "author_tsim", label: 'Author' + end end let(:document) do @@ -11,37 +20,38 @@ 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 describe "email" do - before do - details = { to: 'test@test.com', message: "This is my message", config: config } - @email = described_class.email_record(@documents, details, host: 'projectblacklight.org', protocol: 'https') + subject(:email) do + described_class.email_record(@documents, details, host: 'projectblacklight.org', protocol: 'https') + end + + let(:details) do + { to: 'test@test.com', message: "This is my message", config: config } end it "receives the TO paramater and send the email to that address" do - expect(@email.to).to include 'test@test.com' + expect(email.to).to include 'test@test.com' end it "starts the subject w/ Item Record:" do - expect(@email.subject).to match /^Item Record:/ + expect(email.subject).to match /^Item Record:/ end it "puts the title of the item in the subject" do - expect(@email.subject).to match /The horn/ + expect(email.subject).to match /The horn/ end it "has the correct from address (w/o the port number)" do - expect(@email.from).to include "no-reply@projectblacklight.org" + expect(email.from).to include "no-reply@projectblacklight.org" end it "prints out the correct body" do - expect(@email.body).to match /Title: The horn/ - expect(@email.body).to match /Author: Janetzky, Kurt/ - expect(@email.body).to match /projectblacklight.org/ + expect(email.body).to match /Title: The horn/ + expect(email.body).to match /Author: Janetzky, Kurt/ + expect(email.body).to match /projectblacklight.org/ end it "uses https URLs when protocol is set" do @@ -53,28 +63,31 @@ context "email title_field is configured and multi valued" do let(:document) { SolrDocument.new(id: "123456", foo: ["Fizz Fizz", "Fuzz Fuzz"], format: ["book"], title_tsim: "The horn", language_ssim: "English", author_tsim: "Janetzky, Kurt") } - it "uses configured email title_field" do + before do config.email.title_field = "foo" - expect(@email.subject).to match("Fizz Fizz") - expect(@email.subject).not_to match("Fuzz Fuzz") - expect(@email.subject).not_to match("The horn") + end + + it "uses configured email title_field" do + expect(email.subject).to eq 'Item Record: Fizz Fizz and Fuzz Fuzz' end end context "email title_field is configured and single valued" do let(:document) { SolrDocument.new(id: "123456", foo: "Fizz Fizz", format: ["book"], title_tsim: "The horn", language_ssim: "English", author_tsim: "Janetzky, Kurt") } - it "uses configured email title_field" do + before do config.email.title_field = "foo" - expect(@email.subject).to match("Fizz Fizz") - expect(@email.subject).not_to match("The horn") + end + + it "uses configured email title_field" do + expect(email.subject).to eq 'Item Record: Fizz Fizz' end end end describe "SMS" do before do - details = { to: '5555555555@txt.att.net' } + details = { to: '5555555555@txt.att.net', config: config } @sms = described_class.sms_record(@documents, details, host: 'projectblacklight.org:3000') end @@ -91,8 +104,8 @@ end it "prints out the correct body" do - expect(@sms.body).to match /The horn/ - expect(@sms.body).to match /by Janetzky, Kurt/ + expect(@sms.body).to match /Title: The horn/ + expect(@sms.body).to match /Author: Janetzky, Kurt/ expect(@sms.body).to match /projectblacklight.org:3000/ end diff --git a/spec/presenters/blacklight/document_presenter_spec.rb b/spec/presenters/blacklight/document_presenter_spec.rb index 4be46a21a9..2ebab5e14a 100644 --- a/spec/presenters/blacklight/document_presenter_spec.rb +++ b/spec/presenters/blacklight/document_presenter_spec.rb @@ -12,6 +12,7 @@ before do allow(request_context).to receive(:search_state).and_return(search_state) + allow(request_context).to receive(:action_name).and_return(:show) end describe '#fields_to_render' do diff --git a/spec/presenters/blacklight/field_presenter_spec.rb b/spec/presenters/blacklight/field_presenter_spec.rb index 4ba173b5c7..ce340313c2 100644 --- a/spec/presenters/blacklight/field_presenter_spec.rb +++ b/spec/presenters/blacklight/field_presenter_spec.rb @@ -51,13 +51,13 @@ def render context 'when an explicit html value is provided' do let(:options) { { value: '<b>val1</b>' } } - it { is_expected.to eq '<b>val1</b>' } + it { is_expected.not_to be_html_safe } end context 'when an explicit array value with unsafe characters is provided' do let(:options) { { value: ['<a', 'b'] } } - it { is_expected.to eq '<a and b' } + it { is_expected.to eq('<a and b').and be_html_safe } end context 'when an explicit array value is provided' do diff --git a/spec/presenters/blacklight/index_presenter_spec.rb b/spec/presenters/blacklight/index_presenter_spec.rb index b31625a2ac..ffa8036f66 100644 --- a/spec/presenters/blacklight/index_presenter_spec.rb +++ b/spec/presenters/blacklight/index_presenter_spec.rb @@ -22,6 +22,7 @@ before do allow(request_context).to receive(:search_state).and_return(search_state) + allow(request_context).to receive(:action_name).and_return(:index) end describe '#fields' do diff --git a/spec/presenters/blacklight/show_presenter_spec.rb b/spec/presenters/blacklight/show_presenter_spec.rb index b447393019..0b42f6ea5f 100644 --- a/spec/presenters/blacklight/show_presenter_spec.rb +++ b/spec/presenters/blacklight/show_presenter_spec.rb @@ -19,6 +19,7 @@ before do allow(request_context).to receive(:search_state).and_return(search_state) + allow(request_context).to receive(:action_name).and_return(:show) end describe "link_rel_alternates" do diff --git a/spec/presenters/pipeline_spec.rb b/spec/presenters/pipeline_spec.rb index 1c1cc6b531..9014f5dbc9 100644 --- a/spec/presenters/pipeline_spec.rb +++ b/spec/presenters/pipeline_spec.rb @@ -4,10 +4,10 @@ include Capybara::RSpecMatchers let(:document) { instance_double(SolrDocument) } let(:context) { double } - let(:options) { double('options') } + let(:options) { {} } describe '.render' do - subject { described_class.render(values, field_config, document, context, options) } + subject(:rendered) { described_class.render(values, field_config, document, context, options) } let(:values) { %w[a b] } let(:field_config) { Blacklight::Configuration::NullField.new } @@ -35,6 +35,17 @@ expect(described_class).to have_received(:new) .with(values, field_config, document, context, described_class.operations, options) end + + context 'outside of an HTML context' do + let(:options) { { format: 'text' } } + + let(:values) { ['"blah"', "<notatag>"] } + let(:field_config) { Blacklight::Configuration::NullField.new itemprop: 'some-prop' } + + it 'does not HTML escape values or inject HTML tags' do + expect(rendered).to eq '"blah" and <notatag>' + end + end end describe '.operations' do