Skip to content

Commit

Permalink
The Navbar links should be dynamically configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 19, 2014
1 parent 463e9cb commit 7f7469c
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 54 deletions.
29 changes: 24 additions & 5 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,17 @@ def render_search_bar
end

##
# Render "docuemnt actions" area for search results view
# Render "document actions" area for navigation header
# (normally renders "Saved Searches", "History", "Bookmarks")
#
# @param [Hash] options
# @return [String]
def render_nav_actions(options={})
render_filtered_partials(blacklight_config.navbar.partials, options)
end

##
# Render "document actions" area for search results view
# (normally renders next to title in the list view)
#
# @param [SolrDocument] document
Expand All @@ -102,13 +112,16 @@ def render_search_bar
# @return [String]
def render_index_doc_actions(document, options={})
wrapping_class = options.delete(:wrapping_class) || "index-document-functions"
rendered = render_filtered_partials(blacklight_config.index.document_actions, { document: document }.merge(options))
content_tag("div", rendered, class: wrapping_class)
end

def render_filtered_partials(partials, options={})
content = []
index_tool_partials.select { |_, config| evaluate_if_unless_configuration config, @document }.each do |_, config|
content << render(config.partial, { document: document }.merge(options))
partials.select { |_, config| evaluate_if_unless_configuration config, @document }.each do |_, config|
content << render(config.partial, options)
end

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

##
Expand Down Expand Up @@ -575,6 +588,12 @@ def render_bookmarks_control?
has_user_authentication_provider? and current_or_guest_user.present?
end

##
# Determine whether to render the saved searches link
def render_saved_searches?
has_user_authentication_provider? and current_user
end

##
# Returns a document presenter for the given document
def presenter(document)
Expand Down
17 changes: 1 addition & 16 deletions app/views/_user_util_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
<div class="navbar-right">
<ul class="nav navbar-nav">
<% if render_bookmarks_control? %>
<li>
<%= link_to bookmarks_path, id:'bookmarks_nav' do %>
<%= t('blacklight.header_links.bookmarks') %>
(<span data-role='bookmark-counter'><%= current_or_guest_user.bookmarks.count %></span>)
<% end %>
</li>
<% end %>
<% if has_user_authentication_provider? and current_user %>
<li>
<%= link_to t('blacklight.header_links.saved_searches'), saved_searches_path %>
</li>
<% end %>
<li>
<%= link_to t('blacklight.header_links.search_history'), search_history_path %>
</li>
<%= render_nav_actions %>
</ul>

<% if has_user_authentication_provider? %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/blacklight/nav/_bookmark.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<li>
<%= link_to bookmarks_path, id:'bookmarks_nav' do %>
<%= t('blacklight.header_links.bookmarks') %>
(<span data-role='bookmark-counter'><%= current_or_guest_user.bookmarks.count %></span>)
<% end %>
</li>
3 changes: 3 additions & 0 deletions app/views/blacklight/nav/_saved_searches.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li>
<%= link_to t('blacklight.header_links.saved_searches'), saved_searches_path %>
</li>
3 changes: 3 additions & 0 deletions app/views/blacklight/nav/_search_history.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<li>
<%= link_to t('blacklight.header_links.search_history'), search_history_path %>
</li>
4 changes: 4 additions & 0 deletions lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module Blacklight::Catalog
# provided by Blacklight::Catalog::IndexTools
add_index_tools_partial(:bookmark, partial: 'bookmark_control', if: :render_bookmarks_control?)

add_nav_action(:bookmark, partial: 'blacklight/nav/bookmark', if: :render_bookmarks_control?)
add_nav_action(:saved_searches, partial: 'blacklight/nav/saved_searches', if: :render_saved_searches?)
add_nav_action(:search_history, partial: 'blacklight/nav/search_history')

end

# get search results from the solr index
Expand Down
45 changes: 26 additions & 19 deletions lib/blacklight/catalog/index_tools.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
module Blacklight
module Catalog::IndexTools
extend ActiveSupport::Concern
included do
class_attribute :inheritable_tool_partials
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 ||= (inheritable_tool_partials || Hash.new).deep_dup
end

##
# Add a partial to the tools for each document in the search results.
# @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 name, opts = {}
config = Blacklight::Configuration::ToolConfig.new opts
config.name = name
add_action(blacklight_config.index.document_actions, name, opts)
end

if block_given?
yield config
end
##
# Add a partial to the header navbar
# @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_nav_action name, opts = {}
add_action(blacklight_config.navbar.partials, name, opts)
end

index_tool_partials[name] = config

self.inheritable_tool_partials = index_tool_partials
end
private

def add_action config_hash, name, opts
config = Blacklight::Configuration::ToolConfig.new opts
config.name = name

if block_given?
yield config
end

config_hash[name] = config
end
end
end
end
2 changes: 2 additions & 0 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def default_values
:display_type_field => 'format',
# partials to render for each document(see #render_document_partials)
:partials => [:index_header, :thumbnail, :index],
:document_actions => {},
# what field, if any, to use to render grouped results
:group => false,
# additional response formats for search results
Expand All @@ -72,6 +73,7 @@ def default_values
# partials to render for each document(see #render_document_partials)
partials: [:show_header, :show]
),
:navbar => OpenStructWithHashAccess.new(partials: { }),
# Configurations for specific types of index views
:view => NestedOpenStructWithHashAccess.new(ViewConfig, 'list'),
# Maxiumum number of spelling suggestions to offer
Expand Down
8 changes: 4 additions & 4 deletions spec/controllers/alternate_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
describe AlternateController do
describe "add_index_tools_partial" do
it "should inherit tools from CatalogController" do
expect(AlternateController.index_tool_partials).to have_key(:bookmark)
expect(AlternateController.blacklight_config.index.document_actions).to have_key(:bookmark)
end

context "when deleting partials from the AlternateController" do
before do
AlternateController.index_tool_partials.delete(:bookmark)
AlternateController.blacklight_config.index.document_actions.delete(:bookmark)
end
it "should not affect the CatalogController" do
expect(AlternateController.index_tool_partials).to be_empty
expect(CatalogController.index_tool_partials).to have_key(:bookmark)
expect(AlternateController.blacklight_config.index.document_actions).to be_empty
expect(CatalogController.blacklight_config.index.document_actions).to have_key(:bookmark)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ def mock_document_app_helper_url *args
@config = Blacklight::Configuration.new.configure do |config|
config.index.title_field = 'title_display'
config.index.display_type_field = 'format'
config.index.document_actions = index_tool_partials
end

@document = SolrDocument.new('title_display' => "A Fake Document", 'id'=>'8')
allow(helper).to receive(:blacklight_config).and_return(@config)
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
response = helper.render_index_doc_actions(@document)
Expand Down
9 changes: 6 additions & 3 deletions spec/views/_user_util_links.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
describe "_user_util_links" do

let :blacklight_config do
Blacklight::Configuration.new
Blacklight::Configuration.new.configure do |config|
config.navbar.partials = { bookmark: Blacklight::Configuration::ToolConfig.new(partial: 'blacklight/nav/bookmark', if: :render_bookmarks_control?) }
end

end

it "should render the correct bookmark count" do
count = rand(99)
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
allow(view).to receive(:render_bookmarks_control?).and_return true
allow(view).to receive(:has_user_authentication_provider?). and_return false
allow(view).to receive_message_chain(:current_or_guest_user, :bookmarks, :count).and_return(count)
allow(view).to receive(:has_user_authentication_provider?).and_return false
allow(view).to receive_message_chain(:current_or_guest_user, :bookmarks, :count).and_return(count)
render :partial => "user_util_links"
expect(rendered).to have_selector('#bookmarks_nav span[data-role=bookmark-counter]', text: "#{count}")
end
Expand Down
5 changes: 0 additions & 5 deletions spec/views/catalog/_index_header_default.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
Blacklight::Configuration.new
end

let :index_tool_partials do
{ bookmark: 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 7f7469c

Please sign in to comment.