diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb index 57a0493a4b..356c39423e 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -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) diff --git a/lib/blacklight/solr/facet_paginator.rb b/lib/blacklight/solr/facet_paginator.rb index 21db7e488a..0b31d98922 100644 --- a/lib/blacklight/solr/facet_paginator.rb +++ b/lib/blacklight/solr/facet_paginator.rb @@ -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. @@ -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 @@ -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. @@ -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 diff --git a/spec/lib/blacklight/facet_paginator_spec.rb b/spec/lib/blacklight/facet_paginator_spec.rb index d2527a4e66..fea842e909 100644 --- a/spec/lib/blacklight/facet_paginator_spec.rb +++ b/spec/lib/blacklight/facet_paginator_spec.rb @@ -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 } @@ -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