Skip to content

Commit

Permalink
Enable configuration of the index tools partials
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 18, 2014
1 parent 0dea511 commit 94f553f
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 36 deletions.
8 changes: 5 additions & 3 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,19 @@ def render_index_doc_actions(document, options={})
wrapping_class = options.delete(:wrapping_class) || "index-document-functions"

content = []
content << render(:partial => 'catalog/bookmark_control', :locals => {:document=> document}.merge(options)) if render_bookmarks_control?
index_tool_partials.select { |config| evaluate_if_unless_configuration config, @document }.each do |config|
content << render(config.partial, { document: document }.merge(options))
end

content_tag("div", safe_join(content, "\n"), :class=> wrapping_class)
content_tag("div", safe_join(content, "\n"), class: wrapping_class)
end

##
# Render "docuemnt actions" for the item detail 'show' view.
# (this normally renders next to title)
#
# By default includes 'Bookmarks'
#
#
# @param [SolrDocument] document
# @param [Hash] options
# @option options [String] :wrapping_class
Expand Down
16 changes: 8 additions & 8 deletions app/views/catalog/_bookmark_control.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<% if current_or_guest_user %>
<%-
# Note these two forms are pretty similar but for different :methods, classes, and labels.
<%-
# Note these two forms are pretty similar but for different :methods, classes, and labels.
# but it was simpler to leave them seperate instead of DRYing them, got confusing trying that.
# the data-doc-id attribute is used by our JS that converts to a checkbox/label.
-%>
<% if current_bookmarks.find { |x| x.document_id == document.id and x.document_type == document.class }.blank? %>
<% if current_bookmarks.find { |x| x.document_id == document.id and x.document_type == document.class }.blank? %>
<%= form_tag( bookmark_path( document ), :method => :put, :class => "bookmark_toggle", "data-doc-id" => document.id, :'data-present' => t('blacklight.search.bookmarks.present'), :'data-absent' => t('blacklight.search.bookmarks.absent'), :'data-inprogress' => t('blacklight.search.bookmarks.inprogress')) do %>
<%= form_tag( bookmark_path( document ), :method => :put, :class => "bookmark_toggle", "data-doc-id" => document.id, :'data-present' => t('blacklight.search.bookmarks.present'), :'data-absent' => t('blacklight.search.bookmarks.absent'), :'data-inprogress' => t('blacklight.search.bookmarks.inprogress')) do %>
<%= submit_tag(t('blacklight.bookmarks.add.button'), :id => "bookmark_toggle_#{document.id.to_s.parameterize}", :class => "bookmark_add") %>
<% end %>
<% else %>
<%= form_tag( bookmark_path( document ), :method => :delete, :class => "bookmark_toggle", "data-doc-id" => document.id, :'data-present' => t('blacklight.search.bookmarks.present'), :'data-absent' => t('blacklight.search.bookmarks.absent'), :'data-inprogress' => t('blacklight.search.bookmarks.inprogress')) do %>
<%= form_tag( bookmark_path( document ), :method => :delete, :class => "bookmark_toggle", "data-doc-id" => document.id, :'data-present' => t('blacklight.search.bookmarks.present'), :'data-absent' => t('blacklight.search.bookmarks.absent'), :'data-inprogress' => t('blacklight.search.bookmarks.inprogress')) do %>
<%= submit_tag(t('blacklight.bookmarks.remove.button'), :id => "bookmark_toggle_#{document.id.to_s.parameterize}", :class => "bookmark_remove") %>
<% end %>
<% end %>
<% else %>
&nbsp;
Expand Down
9 changes: 4 additions & 5 deletions app/views/catalog/_index_header_default.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% # header bar for doc items in index view -%>
<div class="documentHeader row">
<%# main title container for doc partial view
<%# main title container for doc partial view
How many bootstrap columns need to be reserved
for bookmarks control depends on size.
for bookmarks control depends on size.
-%>
<h5 class="index_title col-sm-9 col-lg-10">
<% counter = document_counter_with_offset(document_counter) %>
Expand All @@ -17,4 +17,3 @@
<% # bookmark functions for items/docs -%>
<%= render_index_doc_actions document, :wrapping_class => "index-document-functions col-sm-3 col-lg-2" %>
</div>

4 changes: 4 additions & 0 deletions lib/blacklight/bookmarks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ module Blacklight::Bookmarks
include Blacklight::Configurable
include Blacklight::SolrHelper
include Blacklight::TokenBasedUser
include Blacklight::Catalog::IndexTools

copy_blacklight_config_from(CatalogController)

before_filter :verify_user

self.document_actions[:sms].if = false if self.document_actions[:sms]

# provided by Blacklight::Catalog::IndexTools
add_index_tools_partial('catalog/bookmark_control', if: :render_bookmarks_control?)
end

def action_documents
Expand Down
29 changes: 17 additions & 12 deletions lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- encoding : utf-8 -*-
module Blacklight::Catalog
module Blacklight::Catalog
extend ActiveSupport::Concern

require 'blacklight/catalog/document_actions'
require 'blacklight/catalog/index_tools'
require 'blacklight/catalog/search_context'

include Blacklight::Base

include Blacklight::Catalog::DocumentActions
include Blacklight::Catalog::IndexTools

SearchHistoryWindow = 100 # how many searches to save in session history

Expand All @@ -31,12 +33,15 @@ module Blacklight::Catalog
add_document_action(:citation)
add_document_action(:librarian_view, if: Proc.new { |ctx, config, doc| ctx.respond_to? :librarian_view_catalog_path and doc.respond_to?(:to_marc) })

# provided by Blacklight::Catalog::IndexTools
add_index_tools_partial('catalog/bookmark_control', if: :render_bookmarks_control?)

end

# get search results from the solr index
def index
(@response, @document_list) = get_search_results

respond_to do |format|
format.html { preferred_view }
format.rss { render :layout => false }
Expand All @@ -49,10 +54,10 @@ def index
document_export_formats(format)
end
end

# get single document from the solr index
def show
@response, @document = get_solr_response_for_doc_id
@response, @document = get_solr_response_for_doc_id

respond_to do |format|
format.html {setup_next_and_previous_documents}
Expand All @@ -63,10 +68,10 @@ def show
# export formats.
@document.export_formats.each_key do | format_name |
# It's important that the argument to send be a symbol;
# if it's a string, it makes Rails unhappy for unclear reasons.
# if it's a string, it makes Rails unhappy for unclear reasons.
format.send(format_name.to_sym) { render :text => @document.export_as(format_name), :layout => false }
end

end
end

Expand Down Expand Up @@ -94,14 +99,14 @@ def facet

respond_to do |format|
# Draw the facet selector for users who have javascript disabled:
format.html
format.html
format.json { render json: render_facet_list_as_json }

# Draw the partial for the "more" facet modal window:
format.js { render :layout => false }
end
end

# method to serve up XML OpenSearch description and JSON autocomplete response
def opensearch
respond_to do |format|
Expand All @@ -121,15 +126,15 @@ def action_documents
def action_success_redirect_path
catalog_path(params[:id])
end

##
# Check if any search parameters have been set
# @return [Boolean]
def has_search_parameters?
!params[:q].blank? or !params[:f].blank? or !params[:search_field].blank?
end
protected

protected
#
# non-routable methods ->
#
Expand Down
34 changes: 34 additions & 0 deletions lib/blacklight/catalog/index_tools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Blacklight
module Catalog::IndexTools
extend ActiveSupport::Concern
included do
helper_method :index_tool_partials
end

def index_tool_partials
self.class.index_tool_partials
end

module ClassMethods
def index_tool_partials
@index_tool_partials ||= []
end

##
# @param partial [String] the name of the document partial
# @param opts [Hash]
# @option opts [Symbol,Proc] :if render this action if the method identified by the symbol or the proc evaluates to true.
# The proc will receive the action configuration and the document or documents for the action.
# @option opts [Symbol,Proc] :unless render this action unless the method identified by the symbol or the proc evaluates to true
# The proc will receive the action configuration and the document or documents for the action.
def add_index_tools_partial partial, opts = {}
config = Blacklight::Configuration::ToolConfig.new({partial: partial}.merge(opts))

if block_given?
yield config
end
index_tool_partials << config
end
end
end
end
12 changes: 6 additions & 6 deletions spec/features/bookmarks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
click_button 'Remove bookmark'
expect(page).to have_content 'Successfully removed bookmark.'
end

it "should add and delete bookmarks from the show page" do
sign_in 'user1'
visit catalog_path('2007020969')
click_button 'Bookmark'
click_button 'Remove bookmark'
expect(page).to have_content 'Successfully removed bookmark.'
end

it "should add bookmarks after a user logs in" do
visit catalog_path('2007020969')
click_button 'Bookmark'
Expand All @@ -55,18 +55,18 @@
click_link 'Cite'
expect(page).to have_content 'Strong Medicine speaks'
end

it "should have an endnote export" do
visit catalog_path('2007020969')
click_button 'Bookmark'
visit "/bookmarks.endnote?q="
visit "/bookmarks.endnote?q="
expect(page).to have_content " %@ 9780743297790"
end

it "should have a refworks export" do
visit catalog_path('2007020969')
click_button 'Bookmark'
visit "/bookmarks.refworks_marc_txt?q="
visit "/bookmarks.refworks_marc_txt?q="
expect(page).to have_content "LEADER 01490cam a2200361 a 4500001 2007020969"
end
end
10 changes: 8 additions & 2 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def mock_document_app_helper_url *args
end

describe "with a config" do
let :index_tool_partials do
[Blacklight::Configuration::ToolConfig.new(partial: 'catalog/bookmark_control', if: :render_bookmarks_control?)]
end

before do
@config = Blacklight::Configuration.new.configure do |config|
config.index.title_field = 'title_display'
Expand All @@ -122,6 +126,8 @@ def mock_document_app_helper_url *args
allow(helper).to receive(:has_user_authentication_provider?).and_return(true)
allow(helper).to receive(:current_or_guest_user).and_return(User.new)
allow(helper).to receive_messages(current_bookmarks: [])

allow(helper).to receive(:index_tool_partials).and_return index_tool_partials
end
describe "render_index_doc_actions" do
it "should render partials" do
Expand All @@ -136,12 +142,12 @@ def mock_document_app_helper_url *args
end
end
end

describe "#should_render_index_field?" do
before do
allow(helper).to receive_messages(should_render_field?: true, document_has_value?: true)
end

it "should be true" do
expect(helper.should_render_index_field?(double, double)).to be true
end
Expand Down
5 changes: 5 additions & 0 deletions spec/views/catalog/_index_header_default.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
Blacklight::Configuration.new
end

let :index_tool_partials do
[Blacklight::Configuration::ToolConfig.new(partial: 'catalog/bookmark_control', if: :render_bookmarks_control?)]
end

it "should render the document header" do
assign :response, double(:params => {})
allow(view).to receive(:current_search_session).and_return nil
allow(view).to receive(:search_session).and_return({})
allow(view).to receive(:render_grouped_response?).and_return false
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
allow(view).to receive(:render_bookmarks_control?).and_return false
allow(view).to receive(:index_tool_partials).and_return index_tool_partials
render :partial => "catalog/index_header_default", :locals => {:document => document, :document_counter => 1}
expect(rendered).to have_selector('.document-counter', text: "2")
end
Expand Down

0 comments on commit 94f553f

Please sign in to comment.