Skip to content

Commit

Permalink
add tag/ex/single facets
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Sep 18, 2012
1 parent 93b8904 commit 8d8e47b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
9 changes: 9 additions & 0 deletions app/helpers/blacklight/facets_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ def render_facet_count(num)
# is suitable for a redirect. See
# add_facet_params_and_redirect
def add_facet_params(field, value)
facet_config = facet_configuration_for_field(field)



p = params.dup
p[:f] = (p[:f] || {}).dup # the command above is not deep in rails3, !@#$!@#$
p[:f][field] = (p[:f][field] || []).dup

if facet_config.single and not p[:f][field].empty?
p[:f][field] = []
end

p[:f][field].push(value)
p
end
Expand Down
5 changes: 5 additions & 0 deletions lib/blacklight/configuration/facet_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ module Blacklight
class Configuration::FacetField < Blacklight::Configuration::SolrField
def normalize! blacklight_config
self.query.stringify_keys! if self.query

if self.single and self.tag.blank? and self.ex.blank?
self.tag = "#{self.field}_single"
self.ex = "#{self.field}_single"
end
super
end
end
Expand Down
27 changes: 19 additions & 8 deletions lib/blacklight/solr_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,28 @@ def add_facet_fq_to_solr(solr_parameters, user_params)
def facet_value_to_fq_string(facet_field, value)
facet_config = blacklight_config.facet_fields[facet_field]

case
local_params = []
local_params << "tag=#{facet_config.tag}" if facet_config and facet_config.tag

prefix = ""
prefix = "{!#{local_params.join(" ")}}" unless local_params.empty?

fq = case
when (facet_config and facet_config.query)
facet_config.query[value][:fq]
when (value.is_a?(TrueClass) or value.is_a?(FalseClass) or value == 'true' or value == 'false')
"#{facet_field}:#{value}"
"#{prefix}#{facet_field}:#{value}"
when (value.is_a?(Integer) or (value.to_i.to_s == value if value.respond_to? :to_i))
"#{facet_field}:#{value}"
"#{prefix}#{facet_field}:#{value}"
when (value.is_a?(Float) or (value.to_f.to_s == value if value.respond_to? :to_f))
"#{facet_field}:#{value}"
"#{prefix}#{facet_field}:#{value}"
when value.is_a?(Range)
"#{facet_field}:[#{value.first} TO #{value.last}]"
"#{prefix}#{facet_field}:[#{value.first} TO #{value.last}]"
else
"{!raw f=#{facet_field}}#{value}"
"{!raw f=#{facet_field}#{(" " + local_params.join(" ")) unless local_params.empty?}}#{value}"
end


end

##
Expand Down Expand Up @@ -308,9 +316,12 @@ def add_facetting_to_solr(solr_parameters, user_params)
blacklight_config.facet_fields.each do |field_name, facet|

case
when facet[:query]
solr_parameters[:'facet.query'] += facet[:query].map { |k, x| x[:fq] } if blacklight_config.add_facet_fields_to_solr_request
when facet.query
solr_parameters[:'facet.query'] += facet.query.map { |k, x| x[:fq] } if blacklight_config.add_facet_fields_to_solr_request

when facet.ex
idx = solr_parameters[:'facet.field'].index(facet.field)
solr_parameters[:'facet.field'][idx] = "{!ex=#{facet.ex}}#{solr_parameters[:'facet.field'][idx]}" unless idx.nil?
end

# Support facet paging and 'more'
Expand Down
4 changes: 2 additions & 2 deletions lib/generators/blacklight/templates/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class CatalogController < ApplicationController
#
# :show may be set to false if you don't want the facet to be drawn in the
# facet bar
config.add_facet_field 'format', :label => 'Format'
config.add_facet_field 'pub_date', :label => 'Publication Year'
config.add_facet_field 'format', :label => 'Format'
config.add_facet_field 'pub_date', :label => 'Publication Year', :single => true
config.add_facet_field 'subject_topic_facet', :label => 'Topic', :limit => 20
config.add_facet_field 'language_facet', :label => 'Language', :limit => true
config.add_facet_field 'lc_1letter_facet', :label => 'Call Number'
Expand Down
17 changes: 17 additions & 0 deletions test_support/spec/helpers/facets_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe FacetsHelper do
let(:blacklight_config) { Blacklight::Configuration.new }

before(:each) do
helper.stub(:blacklight_config).and_return blacklight_config
end

describe "should_render_facet?" do
before do
@config = Blacklight::Configuration.new do |config|
Expand Down Expand Up @@ -212,6 +217,18 @@
end
end
end

it "should replace facets for facets configured as single" do
helper.should_receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(mock(:single => true))
params = { :f => { 'single_value_facet_field' => 'other_value'}}
helper.stub!(:params).and_return params

result_params = helper.add_facet_params('single_value_facet_field', 'my_value')


result_params[:f]['single_value_facet_field'].length.should == 1
result_params[:f]['single_value_facet_field'].first.should == 'my_value'
end
end

describe "add_facet_params_and_redirect" do
Expand Down
24 changes: 17 additions & 7 deletions test_support/spec/lib/solr_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def params
@produced_params[:rows].should == 10
end
it 'should have default facet fields' do
@produced_params[:"facet.field"].should == blacklight_config.facet_fields_to_add_to_solr
# remove local params from the facet.field
@produced_params[:"facet.field"].map { |x| x.gsub(/\{![^}]+\}/, '') }.should == blacklight_config.facet_fields_to_add_to_solr
end

it "should have default qt" do
Expand Down Expand Up @@ -116,7 +117,6 @@ def params

solr_params[:q].should be_blank
solr_params["spellcheck.q"].should be_blank
solr_params[:"facet.field"].should == blacklight_config.facet_fields_to_add_to_solr

@single_facet.each_value do |value|
solr_params[:fq].should include("{!raw f=#{@single_facet.keys[0]}}#{value}")
Expand Down Expand Up @@ -154,7 +154,12 @@ def params
end
end

describe "facet_value_to_fq_string", :focus => true do
describe "facet_value_to_fq_string" do

let :blacklight_config do
Blacklight::Configuration.new
end

it "should use the raw handler for strings" do
facet_value_to_fq_string("facet_name", "my value").should == "{!raw f=facet_name}my value"
end
Expand Down Expand Up @@ -186,6 +191,13 @@ def params
it "should handle range requests" do
facet_value_to_fq_string("facet_name", 1..5).should == "facet_name:[1 TO 5]"
end

it "should add tag local parameters" do
blacklight_config.facet_fields.stub(:[]).with('facet_name').and_return(mock(:query => nil, :tag => 'asdf'))

facet_value_to_fq_string("facet_name", true).should == "{!tag=asdf}facet_name:true"
facet_value_to_fq_string("facet_name", "my value").should == "{!raw f=facet_name tag=asdf}my value"
end
end

describe "solr parameters for a field search from config (subject)" do
Expand Down Expand Up @@ -213,9 +225,7 @@ def params
it "should include spellcheck.q, without LocalParams" do
@solr_params["spellcheck.q"].should == "wome"
end
it "should include facet.field from default_solr_params" do
@solr_params[:"facet.field"].should == blacklight_config.facet_fields_to_add_to_solr
end

it "should include spellcheck.dictionary from field def solr_parameters" do
@solr_params[:"spellcheck.dictionary"].should == "subject"
end
Expand Down Expand Up @@ -869,7 +879,7 @@ def blacklight_config
end
it "should handle no facet_limits in config" do
def blacklight_config
config = super.inheritable_copy
config = Blacklight::Configuration.new
config.facet_fields = {}
return config
end
Expand Down

0 comments on commit 8d8e47b

Please sign in to comment.