Skip to content

Commit

Permalink
refactor blacklight_config.document_index_view_types into blacklight_…
Browse files Browse the repository at this point in the history
…config.view, which can also override the blacklight_config.index configuration

Fixes #729
  • Loading branch information
cbeer committed Jan 31, 2014
1 parent 64d286d commit c12fab7
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 47 deletions.
10 changes: 5 additions & 5 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def sort_fields

# Used in the document list partial (search view) for creating a link to the document show action
def document_show_link_field document=nil
blacklight_config.index.show_link.to_sym
blacklight_config.view_config(document_index_view_type).show_link.to_sym
end

# Used in the search form partial for building a select tag
Expand Down Expand Up @@ -356,15 +356,15 @@ def field_value_separator
end

def document_index_view_type query_params=params
if blacklight_config.document_index_view_types.include? query_params[:view]
query_params[:view]
if query_params[:view] and blacklight_config.view.keys.include? query_params[:view].to_sym
query_params[:view].to_sym
else
default_document_index_view_type
end
end

def default_document_index_view_type
blacklight_config.document_index_view_types.first
blacklight_config.view.keys.first
end

def render_document_index documents = nil, locals = {}
Expand Down Expand Up @@ -486,7 +486,7 @@ def render_document_index_label doc, opts
# catalog_path accepts a HashWithIndifferentAccess object. The solr query params are stored in the session,
# so we only need the +counter+ param here. We also need to know if we are viewing to document as part of search results.
def link_to_document(doc, opts={:label=>nil, :counter => nil})
opts[:label] ||= blacklight_config.index.show_link.to_sym
opts[:label] ||= blacklight_config.view_config(document_index_view_type).show_link.to_sym
label = render_document_index_label doc, opts
link_to label, doc, search_session_params(opts[:counter]).merge(opts.reject { |k,v| [:label, :counter].include? k })
end
Expand Down
28 changes: 20 additions & 8 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def refworks_export_url(document = @document)
end

def render_document_class(document = @document)
'blacklight-' + document.get(blacklight_config.index.record_display_type).parameterize rescue nil
'blacklight-' + document.get(blacklight_config.view_config(document_index_view_type).record_display_type).parameterize rescue nil
end

def render_document_sidebar_partial(document = @document)
Expand All @@ -98,14 +98,14 @@ def should_autofocus_on_search_box?
end

def has_thumbnail? document
blacklight_config.index.thumbnail_method or
blacklight_config.index.thumbnail_field && document.has?(blacklight_config.index.thumbnail_field)
blacklight_config.view_config(document_index_view_type).thumbnail_method or
blacklight_config.view_config(document_index_view_type).thumbnail_field && document.has?(blacklight_config.view_config(document_index_view_type).thumbnail_field)
end

def render_thumbnail_tag document, image_options = {}, url_options = {}
value = if blacklight_config.index.thumbnail_method
send(blacklight_config.index.thumbnail_method, document, image_options)
elsif blacklight_config.index.thumbnail_field
value = if blacklight_config.view_config(document_index_view_type).thumbnail_method
send(blacklight_config.view_config(document_index_view_type).thumbnail_method, document, image_options)
elsif blacklight_config.view_config(document_index_view_type).thumbnail_field
image_tag thumbnail_url(document), image_options
end

Expand All @@ -115,12 +115,24 @@ def render_thumbnail_tag document, image_options = {}, url_options = {}
end

def thumbnail_url document
if document.has? blacklight_config.index.thumbnail_field
document.first(blacklight_config.index.thumbnail_field)
if document.has? blacklight_config.view_config(document_index_view_type).thumbnail_field
document.first(blacklight_config.view_config(document_index_view_type).thumbnail_field)
end
end

def add_group_facet_params_and_redirect group
add_facet_params_and_redirect(group.field, group.key)
end

def has_alternative_views?
blacklight_config.view.keys.length > 1
end

def render_view_type_group_icon view
content_tag :span, '', class: "glyphicon #{blacklight_config.view[view].icon_class || default_view_type_group_icon_classes(view) }"
end

def default_view_type_group_icon_classes view
"glyphicon-#{view.to_s.parameterize } view-icon-#{view.to_s.parameterize}"
end
end
2 changes: 1 addition & 1 deletion app/views/catalog/_document.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% # container for a single doc -%>
<div class="document <%= render_document_class document %>" itemscope itemtype="<%= document.itemtype %>">
<%= render_document_partials document, blacklight_config.index.partials, :document_counter => document_counter %>
<%= render_document_partials document, blacklight_config.view_config(document_index_view_type).partials, :document_counter => document_counter %>
</div>
8 changes: 4 additions & 4 deletions app/views/catalog/_view_type_group.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% if blacklight_config.document_index_view_types.length > 1 -%>
<% if has_alternative_views? -%>
<div class="view-type">
<span class="sr-only"><%= t('blacklight.search.view_title') %></span>
<div class="view-type-group btn-group">
<% blacklight_config.document_index_view_types.each do |view| %>
<%= link_to url_for(params.merge(:view => view)), :title => t("blacklight.search.view_title.#{view}", default: t("blacklight.search.view.#{view}", default: "")), :class => "btn btn-default view-type-#{ view.parameterize } #{"active" if document_index_view_type == view}" do %>
<span class="glyphicon glyphicon-<%= view.parameterize %> view-icon-<%= view.parameterize %>"></span>
<% blacklight_config.view.keys.each do |view| %>
<%= link_to url_for(params.merge(:view => view)), :title => t("blacklight.search.view_title.#{view}", default: t("blacklight.search.view.#{view}", default: blacklight_config.view[view].title)), :class => "btn btn-default view-type-#{ view.to_s.parameterize } #{"active" if document_index_view_type == view}" do %>
<%= render_view_type_group_icon view %>
<span class="caption"><%= t("blacklight.search.view.#{view}") %></span>
<% end %>
<% end %>
Expand Down
8 changes: 7 additions & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Blacklight
# fields to display, facets to show, sort options, and search fields.
class Configuration < OpenStructWithHashAccess

require 'blacklight/configuration/view_config'

# Set up Blacklight::Configuration.default_values to contain
# the basic, required Blacklight fields
class << self
Expand All @@ -19,11 +21,11 @@ def default_values
:default_document_solr_params => {},
:show => OpenStructWithHashAccess.new(:partials => [:show_header, :show], :html_title => unique_key, :heading => unique_key),
:index => OpenStructWithHashAccess.new(:partials => [:index_header, :thumbnail, :index], :show_link => unique_key, :record_display_type => 'format', :group => false),
:view => NestedOpenStructWithHashAccess.new(ViewConfig, 'list'),
:spell_max => 5,
:max_per_page => 100,
:per_page => [10,20,50,100],
:search_history_window => Blacklight::Catalog::SearchHistoryWindow,
:document_index_view_types => ['list'],
:add_facet_fields_to_solr_request => false,
:add_field_configuration_to_solr_request => false,
:http_method => :get
Expand Down Expand Up @@ -114,5 +116,9 @@ def configure
yield self if block_given?
self
end

def view_config view_type
self.index.merge view.fetch(view_type, {})
end
end
end
5 changes: 5 additions & 0 deletions lib/blacklight/configuration/view_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Blacklight::Configuration
class ViewConfig < Blacklight::OpenStructWithHashAccess

end
end
2 changes: 1 addition & 1 deletion lib/blacklight/solr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def get_previous_and_next_documents_for_search(index, request_params, extra_cont
def solr_opensearch_params(field=nil)
solr_params = solr_search_params
solr_params[:per_page] = 10
solr_params[:fl] = blacklight_config.index.show_link
solr_params[:fl] = blacklight_config.view_config('opensearch').show_link
solr_params
end

Expand Down
64 changes: 63 additions & 1 deletion lib/blacklight/utils.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'ostruct'
module Blacklight
class OpenStructWithHashAccess < OpenStruct
delegate :keys, :reject!, :select!, :include, :fetch, :to => :to_h
delegate :keys, :delete, :length, :reject!, :select!, :include, :fetch, :to => :to_h

def []=(key, value)
send "#{key}=", value
Expand All @@ -23,4 +23,66 @@ def merge! other_hash
@table.merge! other_hash.to_h
end
end

class NestedOpenStructWithHashAccess < OpenStructWithHashAccess
attr_reader :nested_class
delegate :default_proc=, :to => :to_h

def initialize klass, *args
@nested_class = klass
hash = {}

hashes_and_keys = args.flatten
lazy_configs = hashes_and_keys.extract_options!

args.each do |v|
if v.is_a? Hash
key = v.first
value = v[key]

hash[key] = nested_class.new value
else
hash[v] = nested_class.new
end
end

lazy_configs.each do |k,v|
hash[k] = nested_class.new v
end

super hash
set_default_proc!
end

def << key
@table[key]
end

def []=(key, value)
if value.is_a? Hash
send "#{key}=", nested_class.new(value)
else
send "#{key}=", value
end
end

def marshal_dump
h = to_h
h.default_proc = nil
[nested_class, h]
end

def marshal_load x
@nested_class = x.first
super x.last
set_default_proc!
end

private
def set_default_proc!
self.default_proc = lambda do |hash, key|
hash[key] = self.nested_class.new
end
end
end
end
16 changes: 8 additions & 8 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,34 +316,34 @@ def params

describe "document_index_view_type" do
it "should default to 'list'" do
expect(document_index_view_type).to eq 'list'
expect(document_index_view_type).to eq :list
end

it "should pluck values out of params" do
blacklight_config.stub(:document_index_view_types) { ['list', 'asdf'] }
blacklight_config.stub(:view) { { list: nil, asdf: nil} }
params[:view] = 'asdf'
expect(document_index_view_type).to eq 'asdf'
expect(document_index_view_type).to eq :asdf

params[:view] = 'not_in_list'
expect(document_index_view_type).to eq 'list'
expect(document_index_view_type).to eq :list
end

it "should pluck values from supplied params" do
blacklight_config.stub(:document_index_view_types) { ['list', 'asdf'] }
blacklight_config.stub(:view) { { list: nil, asdf: nil} }
params[:view] = 'asdf'
expect(document_index_view_type(:view => 'list')).to eq 'list'
expect(document_index_view_type(:view => 'list')).to eq :list
end
end

describe "start_over_path" do
it 'should be the catalog path with the current view type' do
blacklight_config.stub(:document_index_view_types) { ['list', 'abc'] }
blacklight_config.stub(:view) { { list: nil, abc: nil} }
helper.stub(:blacklight_config => blacklight_config)
expect(helper.start_over_path(:view => 'abc')).to eq catalog_index_url(:view => 'abc')
end

it 'should not include the current view type if it is the default' do
blacklight_config.stub(:document_index_view_types) { ['list', 'abc'] }
blacklight_config.stub(:view) { { list: nil, asdf: nil} }
helper.stub(:blacklight_config => blacklight_config)
expect(helper.start_over_path(:view => 'list')).to eq catalog_index_url
end
Expand Down
26 changes: 13 additions & 13 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,32 @@ def render_grouped_response?

describe "has_thumbnail?" do
it "should have a thumbnail if a thumbnail_method is configured" do
helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_method => :xyz) ))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
document = double()
expect(helper.has_thumbnail? document).to be_true
end

it "should have a thumbnail if a thumbnail_field is configured and it exists in the document" do
helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_field => :xyz) ))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
document = double(:has? => true)
expect(helper.has_thumbnail? document).to be_true
end

it "should not have a thumbnail if the thumbnail_field is missing from the document" do
helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_field => :xyz) ))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
document = double(:has? => false)
expect(helper.has_thumbnail? document).to be_false
end

it "should not have a thumbnail if none of the fields are configured" do
helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new()))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new() ))
expect(helper.has_thumbnail? double()).to be_false
end
end

describe "render_thumbnail_tag" do
it "should call the provided thumbnail method" do
helper.stub(:blacklight_config => double(:index => double(:thumbnail_method => :xyz)))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
document = double()
helper.stub(:xyz => "some-thumbnail")

Expand All @@ -187,7 +187,7 @@ def render_grouped_response?
end

it "should create an image tag from the given field" do
helper.stub(:blacklight_config => double(:index => OpenStruct.new(:thumbnail_field => :xyz)))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
document = double()

document.stub(:has?).with(:xyz).and_return(true)
Expand All @@ -198,14 +198,14 @@ def render_grouped_response?
end

it "should return nil if no thumbnail is available" do
helper.stub(:blacklight_config => double(:index => OpenStruct.new()))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new() ))

document = double()
expect(helper.render_thumbnail_tag document).to be_nil
end

it "should return nil if no thumbnail is returned from the thumbnail method" do
helper.stub(:blacklight_config => double(:index => OpenStruct.new(:thumbnail_method => :xyz)))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
helper.stub(:xyz => nil)
document = double()

Expand All @@ -215,17 +215,17 @@ def render_grouped_response?

describe "thumbnail_url" do
it "should pull the configured thumbnail field out of the document" do
helper.stub(:blacklight_config => double(:index => double(:thumbnail_field => "xyz")))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
document = double()
document.stub(:has?).with("xyz").and_return(true)
document.stub(:first).with("xyz").and_return("asdf")
document.stub(:has?).with(:xyz).and_return(true)
document.stub(:first).with(:xyz).and_return("asdf")
expect(helper.thumbnail_url document).to eq("asdf")
end

it "should return nil if the thumbnail field doesn't exist" do
helper.stub(:blacklight_config => double(:index => double(:thumbnail_field => "xyz")))
helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
document = double()
document.stub(:has?).with("xyz").and_return(false)
document.stub(:has?).with(:xyz).and_return(false)
expect(helper.thumbnail_url document).to be_nil
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/views/catalog/_constraints.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe "catalog/constraints" do
let :blacklight_config do
Blacklight::Configuration.new :document_index_view_types => ['list', 'xyz']
Blacklight::Configuration.new do |config|
config.view.xyz
end
end

it "should render nothing if no constraints are set" do
Expand All @@ -21,7 +23,7 @@
end

it "should render a start over link with the current view type" do
view.should_receive(:search_action_url).with(view: 'xyz').and_return('http://xyz?view=xyz')
view.should_receive(:search_action_url).with(view: :xyz).and_return('http://xyz?view=xyz')
view.stub(query_has_constraints?: true)
params[:view] = 'xyz'
view.stub(:blacklight_config).and_return(blacklight_config)
Expand Down
Loading

0 comments on commit c12fab7

Please sign in to comment.