Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Blacklight::SolrResponse::FacetItem to use the same logic as Solr when calculating default values #977

Merged
merged 1 commit into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 27 additions & 1 deletion spec/features/facets_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'

describe "Facets" do
Expand All @@ -6,4 +7,29 @@
expect(page).to have_selector(".modal-title", :text => "Language")
expect(page).to have_selector(".facet_select", :text => "Tibetan")
end
end

it "should paginate through a facet's values" do
visit catalog_facet_path("subject_topic_facet")
expect(page).to have_selector '.facet-values li:first', text: "Japanese drama"
expect(page).to have_link "A-Z Sort"
expect(page).to have_selector '.sort_options .active', text: "Numerical Sort"
within ".modal-footer" do
click_on "Next »"
end
expect(page).to have_selector '.facet-values li:first', text: "Jewish law"
expect(page).to have_link "« Previous"
end

it "should be able to change the facet sort" do
visit catalog_facet_path("subject_topic_facet")
expect(page).to have_selector '.facet-values li:first', text: "Japanese drama"
within ".modal-footer" do
click_on "A-Z Sort"
end
expect(page).to have_selector '.facet-values li:first', text: "Accident insurance"
expect(page).to have_link "Numerical Sort"
expect(page).to have_selector '.sort_options .active', text: "A-Z Sort"


end
end
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