Skip to content

Commit

Permalink
Countless extra: simplified code, internal renaming of locals and doc…
Browse files Browse the repository at this point in the history
…s update
  • Loading branch information
ddnexus committed Oct 24, 2021
1 parent aebc553 commit e02aca9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/api/countless.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ The construction of the final `Pagy::Countless` object is split into 2 steps: th

The initial constructor takes the usual hash of variables, calculating only the requested `items` and the `offset`, useful to query the page of items.

### finalize(fetched)
### finalize(fetched_size)

The actual calculation of all the internal variables for the pagination is calculated using the number of `fetched` items. The method returns the finalized instance object.
The actual calculation of all the internal variables for the pagination is calculated using the size of the fetched items. The method returns the finalized instance object.
3 changes: 2 additions & 1 deletion lib/pagy/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ def pagy(collection, vars = {})
# Sub-method called only by #pagy: here for easy customization of variables by overriding
def pagy_get_vars(collection, vars)
pagy_set_items_from_params(vars) if defined?(ItemsExtra)
# You may need to override the count call for non AR collections
vars[:count] ||= (c = collection.count(:all)).is_a?(Hash) ? c.size : c
vars[:page] ||= params[vars[:page_param] || DEFAULT[:page_param]]
vars
end

# Sub-method called only by #pagy: here for easy customization of record-extraction by overriding
def pagy_get_items(collection, pagy)
# This should work with ActiveRecord, Sequel, Mongoid...
# You may need to override this method for collections without offset|limit
collection.offset(pagy.offset).limit(pagy.items)
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/pagy/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def initialize(vars = {}) # rubocop:disable Lint/MissingSuper
@offset = (@items * (@page - 1)) + @outset
end

# Finalize the instance variables based on the fetched items
def finalize(fetched)
raise OverflowError.new(self, :page, "to be < #{@page}") if fetched.zero? && @page > 1
# Finalize the instance variables based on the fetched size
def finalize(fetched_size)
raise OverflowError.new(self, :page, "to be < #{@page}") if fetched_size.zero? && @page > 1

@pages = @last = (fetched > @items ? @page + 1 : @page)
@in = [fetched, @items].min
@pages = @last = (fetched_size > @items ? @page + 1 : @page)
@in = [fetched_size, @items].min
@from = @in.zero? ? 0 : @offset - @outset + 1
@to = @offset - @outset + @in
@prev = (@page - 1 unless @page == 1)
Expand Down
11 changes: 4 additions & 7 deletions lib/pagy/extras/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ def pagy_countless_get_vars(_collection, vars)
end

# Sub-method called only by #pagy_countless: here for easy customization of record-extraction by overriding
# You may need to override this method for collections without offset|limit
def pagy_countless_get_items(collection, pagy)
# This should work with ActiveRecord, Sequel, Mongoid...
return collection.offset(pagy.offset).limit(pagy.items) if pagy.vars[:countless_minimal]

items = collection.offset(pagy.offset).limit(pagy.items + 1).to_a
items_size = items.size
items.pop if items_size == pagy.items + 1
# finalize may adjust pagy.items, so must be used after checking the size
pagy.finalize(items_size)
items
fetched = collection.offset(pagy.offset).limit(pagy.items + 1).to_a # eager load items + 1
pagy.finalize(fetched.size) # finalize the pagy object
fetched[0, pagy.items] # ignore eventual extra item
end
end
Backend.prepend CountlessExtra
Expand Down

0 comments on commit e02aca9

Please sign in to comment.