Skip to content

Commit

Permalink
add #document_index_views for retrieving the available catalog#index …
Browse files Browse the repository at this point in the history
…views, and support if/unless constraints as part of the view configuration
  • Loading branch information
cbeer committed Apr 4, 2014
1 parent 3d1b5ea commit 65f9b2d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
16 changes: 12 additions & 4 deletions app/helpers/blacklight/configuration_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,22 @@ def solr_field_label label, *i18n_keys
t(first, default: rest)
end

def document_index_views
blacklight_config.view.select do |k, config|
should_render_field? config
end
end

##
# Get the default index view type
def default_document_index_view_type
blacklight_config.view.keys.first
document_index_views.select { |k,config| config.respond_to? :default and config.default }.keys.first || document_index_views.keys.first
end

##
# Check if there are alternative views configuration
def has_alternative_views?
blacklight_config.view.keys.length > 1
document_index_views.keys.length > 1
end

##
Expand Down Expand Up @@ -143,9 +149,11 @@ def per_page_options_for_select
# @param [Blacklight::Solr::Configuration::SolrField] solr_field
# @return [Boolean]
def should_render_field? field_config, *args
if_value = evaluate_configuration_conditional(field_config.if, field_config, *args)
return field_config if field_config === true or field_config === false

if_value = !field_config.respond_to?(:if) || field_config.if.nil? || evaluate_configuration_conditional(field_config.if, field_config, *args)

unless_value = field_config.unless.nil? || !evaluate_configuration_conditional(field_config.unless, field_config, *args)
unless_value = !field_config.respond_to?(:unless) || field_config.unless.nil? || !evaluate_configuration_conditional(field_config.unless, field_config, *args)

if_value && unless_value
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/catalog/_view_type_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<div class="view-type">
<span class="sr-only"><%= t('blacklight.search.view_title') %></span>
<div class="view-type-group btn-group">
<% blacklight_config.view.keys.each do |view| %>
<% document_index_views.each do |view, config| %>
<%= 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 %>
</div>
</div>
<% end -%>
<% end -%>
32 changes: 30 additions & 2 deletions lib/blacklight/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def [](key)
def to_h
@table
end

def select *args, &block
self.class.new to_h.select(*args, &block)
end

##
# Merge the values of this OpenStruct with another OpenStruct or Hash
Expand Down Expand Up @@ -54,7 +58,7 @@ def initialize klass, *args
hashes_and_keys = args.flatten
lazy_configs = hashes_and_keys.extract_options!

args.each do |v|
hashes_and_keys.each do |v|
if v.is_a? Hash
key = v.first
value = v[key]
Expand All @@ -66,7 +70,11 @@ def initialize klass, *args
end

lazy_configs.each do |k,v|
hash[k] = nested_class.new v
if v.is_a? nested_class
hash[k] = v
else
hash[k] = nested_class.new v
end
end

super hash
Expand Down Expand Up @@ -111,6 +119,26 @@ def marshal_load x
set_default_proc!
end

def select *args, &block
self.class.new nested_class, to_h.select(*args, &block).to_h
end

##
# Merge the values of this OpenStruct with another OpenStruct or Hash
# @param [Hash,#to_h]
# @return [OpenStructWithHashAccess] a new instance of an OpenStructWithHashAccess
def merge other_hash
self.class.new nested_class, to_h.merge((other_hash if other_hash.is_a? Hash) || other_hash.to_h)
end

##
# Merge the values of another OpenStruct or Hash into this object
# @param [Hash,#to_h]
# @return [OpenStructWithHashAccess] a new instance of an OpenStructWithHashAccess
def merge! other_hash
@table.merge!(nested_class, (other_hash if other_hash.is_a? Hash) || other_hash.to_h)
end

private
def set_default_proc!
self.default_proc = lambda do |hash, key|
Expand Down
28 changes: 25 additions & 3 deletions spec/helpers/configuration_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,31 @@
end

describe "#default_document_index_view_type" do
it "should be the first configured index view" do
blacklight_config.stub(view: { 'a' => true, 'b' => true})
expect(helper.default_document_index_view_type).to eq 'a'
it "should use the first view with default set to true" do
blacklight_config.view.a
blacklight_config.view.b
blacklight_config.view.b.default = true
expect(helper.default_document_index_view_type).to eq :b
end

it "should default to the first configured index view" do
blacklight_config.stub(view: { a: true, b: true})
expect(helper.default_document_index_view_type).to eq :a
end
end

describe "#document_index_views" do
before do
blacklight_config.view.abc = false
blacklight_config.view.def.if = false
blacklight_config.view.xyz.unless = true
end

it "should filter views using :if/:unless configuration" do
helper.document_index_views.should have_key :list
helper.document_index_views.should_not have_key :abc
helper.document_index_views.should_not have_key :def
helper.document_index_views.should_not have_key :xyz
end
end

Expand Down

0 comments on commit 65f9b2d

Please sign in to comment.