Skip to content

Commit

Permalink
Update Blacklight::SolrResponse::FacetItem to use the same logic as S…
Browse files Browse the repository at this point in the history
…olr when calculating default values
  • Loading branch information
cbeer committed Aug 21, 2014
1 parent ab0168e commit c1e99b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
27 changes: 24 additions & 3 deletions lib/blacklight/solr_response/facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,37 @@ def initialize name, items, options = {}
end

def limit
@options[:limit]
@options[:limit] || solr_default_limit
end

def sort
@options[:sort] || 'index'
@options[:sort] || solr_default_sort
end

def offset
@options[:offset] || 0
@options[:offset] || solr_default_offset
end

private
# Per https://wiki.apache.org/solr/SimpleFacetParameters#facet.limit
def solr_default_limit
100
end

# Per https://wiki.apache.org/solr/SimpleFacetParameters#facet.sort
def solr_default_sort
if limit > 0
'count'
else
'index'
end
end

# Per https://wiki.apache.org/solr/SimpleFacetParameters#facet.offset
def solr_default_offset
0
end

end

# @response.facets.each do |facet|
Expand Down
17 changes: 11 additions & 6 deletions spec/lib/blacklight/solr_response/facets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
subject { Blacklight::SolrResponse::Facets::FacetField.new "my_field", [] }

its(:name) { should eq "my_field" }
its(:limit) { should eq nil }
its(:sort) { should eq 'index' }
its(:limit) { should eq 100 }
its(:sort) { should eq 'count' }
its(:offset) { should eq 0 }
end

Expand Down Expand Up @@ -40,8 +40,8 @@
expect(subject.facet_by_field_name('my_field').limit).to eq 15
end

it "should be nil if no value is found" do
expect(subject.facet_by_field_name('my_field').limit).to be_nil
it "should be the solr default limit if no value is found" do
expect(subject.facet_by_field_name('my_field').limit).to eq 100
end
end

Expand Down Expand Up @@ -74,9 +74,14 @@
expect(subject.facet_by_field_name('my_field').sort).to eq 'alpha'
end

it "should default to index if no value is found" do
it "should default to count if no value is found and the default limit is used" do
expect(subject.facet_by_field_name('my_field').sort).to eq 'count'
end

it "should default to index if no value is found and the limit is unlimited" do
request_params['facet.limit'] = -1
expect(subject.facet_by_field_name('my_field').sort).to eq 'index'
end
end
end
end
end

0 comments on commit c1e99b6

Please sign in to comment.