Skip to content

Commit

Permalink
Update Blacklight::Facet to support both blacklight-keyed field names…
Browse files Browse the repository at this point in the history
… and solr-keyed field names
  • Loading branch information
cbeer committed Mar 16, 2015
1 parent 2894a98 commit cc5a86d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
22 changes: 11 additions & 11 deletions lib/blacklight/facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,37 @@ def facet_field_names
def facet_configuration_for_field(field)
f = blacklight_config.facet_fields[field]
f ||= begin
_, value = blacklight_config.facet_fields.find { |k,v| v.field == field }
_, value = blacklight_config.facet_fields.find { |k,v| v.field.to_s == field.to_s }
value
end
f ||= Blacklight::Configuration::FacetField.new(:field => field).normalize!
end


# Get a FacetField object from the @response
def facet_by_field_name field
case field
def facet_by_field_name field_or_field_name
case field_or_field_name
when String, Symbol
extract_facet_by_field_name(field)
facet_field = facet_configuration_for_field(field_or_field_name)
extract_facet_by_field(facet_field)
when Blacklight::Configuration::FacetField
extract_facet_by_field_name(field.key)
extract_facet_by_field(field_or_field_name)
else
field
field_or_field_name
end
end

private

# Get the solr response for the field :field
def extract_facet_by_field_name field_name
facet_field = facet_configuration_for_field(field_name)
def extract_facet_by_field facet_field
case
when (facet_field.respond_to?(:query) and facet_field.query)
create_facet_field_response_for_query_facet_field field_name, facet_field
create_facet_field_response_for_query_facet_field facet_field.key, facet_field
when (facet_field.respond_to?(:pivot) and facet_field.pivot)
create_facet_field_response_for_pivot_facet_field field_name, facet_field
create_facet_field_response_for_pivot_facet_field facet_field.key, facet_field
else
@response.facet_by_field_name(field_name)
@response.facet_by_field_name(facet_field.field)
end
end

Expand Down
7 changes: 4 additions & 3 deletions spec/helpers/facets_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@

describe "facet_by_field_name" do
it "should retrieve the facet from the response given a string" do
facet_config = double(:query => nil)
facet_config = double(:query => nil, field: 'a')
facet_field = double()
allow(helper).to receive(:facet_configuration_for_field).with(anything()).and_return(facet_config)

Expand All @@ -118,7 +118,7 @@
end

it "should also work for facet query fields" do
facet_config = double(:query => {})
facet_config = double(:query => {}, key: 'a_query_facet_field')
allow(helper).to receive(:facet_configuration_for_field).with('a_query_facet_field').and_return(facet_config)
allow(helper).to receive(:create_facet_field_response_for_query_facet_field).with('a_query_facet_field', facet_config)

Expand All @@ -128,6 +128,7 @@
describe "query facets" do
let(:facet_config) {
double(
key: 'my_query_facet_field',
:query => {
'a_simple_query' => { :fq => 'field:search', :label => 'A Human Readable label'},
'another_query' => { :fq => 'field:different_search', :label => 'Label'},
Expand Down Expand Up @@ -165,7 +166,7 @@

describe "pivot facets" do
let(:facet_config) {
double(:pivot => ['field_a', 'field_b'])
double(key: 'my_pivot_facet_field', pivot: ['field_a', 'field_b'])
}

before(:each) do
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/blacklight/facet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Blacklight::Facet do
subject do
Class.new do
include Blacklight::Facet
attr_reader :blacklight_config

def initialize blacklight_config
@blacklight_config = blacklight_config
end
end.new(blacklight_config)
end

let(:blacklight_config) { Blacklight::Configuration.new }

describe "#facet_configuration_for_field" do
it "should look up fields by key" do
blacklight_config.add_facet_field 'a'
expect(subject.facet_configuration_for_field('a')).to eq blacklight_config.facet_fields['a']
end

it "should look up fields by field name" do
blacklight_config.add_facet_field 'a', field: 'b'
expect(subject.facet_configuration_for_field('b')).to eq blacklight_config.facet_fields['a']
end

it "should support both strings and symbols" do
blacklight_config.add_facet_field 'a', field: :b
expect(subject.facet_configuration_for_field('b')).to eq blacklight_config.facet_fields['a']
end
end

end
2 changes: 1 addition & 1 deletion spec/views/catalog/_facets.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
:facet_limit_for => 10 )

@response = double()
allow(@response).to receive(:facet_by_field_name).with(:facet_field_1) { @mock_display_facet_1 }
allow(@response).to receive(:facet_by_field_name).with("facet_field_1") { @mock_display_facet_1 }
end

it "should have a header" do
Expand Down

0 comments on commit cc5a86d

Please sign in to comment.