Skip to content

Commit

Permalink
Reorganizae helper methods into CatalogHelperBehavior + DocumentHelpe…
Browse files Browse the repository at this point in the history
…rBehavior
  • Loading branch information
cbeer committed Feb 1, 2023
1 parent f995028 commit e95e2cf
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 155 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ 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/render_partials_helper_behavior.rb'

Expand Down
44 changes: 0 additions & 44 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,6 @@ 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 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

# @!group Search result helpers
##
# Render a partial of an arbitrary format inside a
Expand All @@ -97,35 +82,6 @@ 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
Expand Down
81 changes: 27 additions & 54 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Blacklight::CatalogHelperBehavior
include Blacklight::ConfigurationHelperBehavior
include Blacklight::ComponentHelperBehavior
include Blacklight::DocumentHelperBehavior
include Blacklight::FacetsHelperBehavior
include Blacklight::RenderPartialsHelperBehavior

Expand Down Expand Up @@ -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?
#
Expand All @@ -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<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

# Render an html <title> appropriate string for a selected facet field and values
#
# @see #render_search_to_page_title
Expand Down Expand Up @@ -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
Expand Down
76 changes: 76 additions & 0 deletions app/helpers/blacklight/document_helper_behavior.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion app/models/record_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Only works for documents with a #to_marc right now.
class RecordMailer < ApplicationMailer
helper BlacklightHelper
helper CatalogHelper
helper_method :blacklight_config, :blacklight_configuration_context

Expand Down
56 changes: 0 additions & 56 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }

Expand Down Expand Up @@ -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
56 changes: 56 additions & 0 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e95e2cf

Please sign in to comment.