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 f46ade2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ 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

##
Expand Down
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
7 changes: 6 additions & 1 deletion 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,6 +33,9 @@ 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
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
6 changes: 6 additions & 0 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 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 f46ade2

Please sign in to comment.