Skip to content

Commit

Permalink
Allow facet paginator to handle more than two pages. Fixes a regressi…
Browse files Browse the repository at this point in the history
…on in 5.5.0
  • Loading branch information
jcoyne committed Jul 18, 2014
1 parent 14bca52 commit f39b7aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 0 additions & 1 deletion lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def facet
@response = get_facet_field_response(@facet.field, params)
@display_facet = @response.facets.first

# @pagination was deprecated in Blacklight 5.1
@pagination = facet_paginator(@facet, @display_facet)


Expand Down
29 changes: 17 additions & 12 deletions lib/blacklight/solr/facet_paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FacetPaginator
class << self; attr_accessor :request_keys end # create a class method
def request_keys ; self.class.request_keys ; end # shortcut

attr_reader :total_count, :items, :offset, :limit, :sort
attr_reader :offset, :limit, :sort

# all_facet_values is a list of facet value objects returned by solr,
# asking solr for n+1 facet values.
Expand All @@ -40,8 +40,17 @@ def initialize(all_facet_values, arguments)
@limit = arguments[:limit].to_s.to_i
# count is solr's default
@sort = arguments[:sort] || "count"
@total_count = all_facet_values.size
@items = items_for_limit(all_facet_values)

@all = all_facet_values
end

# The number of records solr gave us when we asked for limit + 1 records at the current offset
def total_count
@all.size
end

def items
items_for_limit(@all)
end

def prev_page
Expand Down Expand Up @@ -73,21 +82,13 @@ def has_previous?
deprecation_deprecate :has_next?

def last_page?
current_page >= total_pages
total_count <= limit
end

def first_page?
current_page == 1
end

def total_pages
if limit == 0 #check for divide by zero
1
else
(total_count.to_f / limit).ceil
end
end

# Pass in a desired solr facet solr key ('count' or 'index', see
# http://wiki.apache.org/solr/SimpleFacetParameters#facet.limit
# under facet.sort ), and your current request params.
Expand All @@ -99,6 +100,10 @@ def params_for_resort_url(sort_method, params)
params.merge(request_keys[:sort] => sort_method, request_keys[:page] => nil)
end

def as_json(_ = nil)
{ 'items' => items.as_json, 'limit' => limit, 'offset' => offset, 'sort' => sort }
end

private
# setting limit to 0 implies no limit
# @return an array of facets on the page
Expand Down
20 changes: 20 additions & 0 deletions spec/lib/blacklight/facet_paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@
end
end

context 'on the second page of three pages' do
subject { Blacklight::Solr::FacetPaginator.new(seven_facet_values, offset: 6, limit: limit) }
it { should_not be_first_page }
it { should_not be_last_page }
its(:current_page) { should eq 2 }
its(:prev_page) { should eq 1 }
its(:next_page) { should eq 3 }
it 'should limit items to limit, if limit is smaller than items.length' do
expect(subject.items.size).to eq 6
end
end

context 'on the first page of one page' do
subject { Blacklight::Solr::FacetPaginator.new(six_facet_values, offset: 0, limit: limit) }
it { should be_first_page }
Expand Down Expand Up @@ -65,4 +77,12 @@
end
end

describe "#as_json" do
subject { Blacklight::Solr::FacetPaginator.new([f1], offset: 0, limit: nil).as_json }
it "should be well structured" do
expect(subject).to eq("items" => [{"hits"=>"792", "value"=>"Book"}], "limit" => 0,
"offset" => 0, "sort" => "count")
end
end

end

0 comments on commit f39b7aa

Please sign in to comment.