Class: Blacklight::FacetPaginator
- Inherits:
-
Object
- Object
- Blacklight::FacetPaginator
- Defined in:
- app/models/blacklight/facet_paginator.rb
Overview
Pagination for facet values – works by setting the limit to max displayable. You have to ask Solr for limit+1, to get enough results to see if ‘more’ are available’. That is, the all_facet_values arg in constructor should be the result of asking solr for limit+1 values. This is a workaround for the fact that Solr itself can’t compute the total values for a given facet field, so we cannot know how many “pages” there are.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Instance Method Summary collapse
- #as_json(_ = nil) ⇒ Object
- #current_page ⇒ Object
- #first_page? ⇒ Boolean
-
#initialize(all_facet_values, arguments = {}) ⇒ FacetPaginator
constructor
all_facet_values is a list of facet value objects returned by solr, asking solr for n+1 facet values.
- #items ⇒ Object
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
-
#params_for_resort_url(sort_method, params = {}) ⇒ Object
Pass in a desired solr facet solr key (‘count’ or ‘index’, see wiki.apache.org/solr/SimpleFacetParameters#facet.limit under facet.sort ), and your current request params.
- #prev_page ⇒ Object
-
#total_count ⇒ Object
The number of records solr gave us when we asked for limit + 1 records at the current offset.
-
#total_pages ⇒ Object
We’re implementing total_pages so that this matches the API from kaminari, even though we can’t know the total number of pages.
Constructor Details
#initialize(all_facet_values, arguments = {}) ⇒ FacetPaginator
all_facet_values is a list of facet value objects returned by solr, asking solr for n+1 facet values. options: :limit => number to display per page, or (default) ?. Nil means
display all with no previous or next.
:offset => current item offset, default 0 :sort => ‘count’ or ‘index’, solr tokens for facet value sorting, default ‘count’.
31 32 33 34 35 36 37 38 39 |
# File 'app/models/blacklight/facet_paginator.rb', line 31 def initialize(all_facet_values, arguments = {}) # to_s.to_i will conveniently default to 0 if nil @offset = arguments[:offset].to_s.to_i @limit = arguments[:limit] @sort = arguments[:sort] @prefix = arguments[:prefix] @all = all_facet_values end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
22 23 24 |
# File 'app/models/blacklight/facet_paginator.rb', line 22 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
22 23 24 |
# File 'app/models/blacklight/facet_paginator.rb', line 22 def offset @offset end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
22 23 24 |
# File 'app/models/blacklight/facet_paginator.rb', line 22 def prefix @prefix end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
22 23 24 |
# File 'app/models/blacklight/facet_paginator.rb', line 22 def sort @sort end |
Instance Method Details
#as_json(_ = nil) ⇒ Object
101 102 103 |
# File 'app/models/blacklight/facet_paginator.rb', line 101 def as_json(_ = nil) { 'items' => items.as_json, 'limit' => limit, 'offset' => offset, 'sort' => sort } end |
#current_page ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'app/models/blacklight/facet_paginator.rb', line 54 def current_page # A nil limit is unlimited, thus only one page. if limit.nil? || limit.zero? # check for divide by zero 1 else (@offset / limit) + 1 end end |
#first_page? ⇒ Boolean
71 72 73 |
# File 'app/models/blacklight/facet_paginator.rb', line 71 def first_page? current_page == 1 end |
#items ⇒ Object
46 47 48 |
# File 'app/models/blacklight/facet_paginator.rb', line 46 def items items_for_limit(@all) end |
#last_page? ⇒ Boolean
67 68 69 |
# File 'app/models/blacklight/facet_paginator.rb', line 67 def last_page? limit.nil? || total_count <= limit end |
#next_page ⇒ Object
63 64 65 |
# File 'app/models/blacklight/facet_paginator.rb', line 63 def next_page current_page + 1 unless last_page? end |
#params_for_resort_url(sort_method, params = {}) ⇒ Object
Pass in a desired solr facet solr key (‘count’ or ‘index’, see wiki.apache.org/solr/SimpleFacetParameters#facet.limit under facet.sort ), and your current request params. Get back params suitable to passing to an ActionHelper method for creating a url, to resort by that method.
89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/blacklight/facet_paginator.rb', line 89 def params_for_resort_url(sort_method, params = {}) # When resorting, we've got to reset the offset to start at beginning, # no way to make it make sense otherwise. resort_params = params.merge(request_keys[:sort] => sort_method, request_keys[:page] => nil) if sort_method == 'count' resort_params.except!(request_keys[:prefix]) end resort_params end |
#prev_page ⇒ Object
50 51 52 |
# File 'app/models/blacklight/facet_paginator.rb', line 50 def prev_page current_page - 1 unless first_page? end |
#total_count ⇒ Object
The number of records solr gave us when we asked for limit + 1 records at the current offset
42 43 44 |
# File 'app/models/blacklight/facet_paginator.rb', line 42 def total_count @all.size end |
#total_pages ⇒ Object
We’re implementing total_pages so that this matches the API from kaminari, even though we can’t know the total number of pages. github.com/amatsuda/kaminari/blob/v0.16.1/lib/kaminari/models/page_scope_methods.rb#L21
78 79 80 |
# File 'app/models/blacklight/facet_paginator.rb', line 78 def total_pages -1 end |