Module: Uchi::Pagy::OffsetPaginator
- Defined in:
- lib/uchi/pagy/toolbox/paginators/offset.rb
Class Method Summary collapse
-
.get_count(collection, options) ⇒ Object
Get the collection count.
-
.paginate(context, collection, **options) ⇒ Object
Return instance and page of results.
Class Method Details
.get_count(collection, options) ⇒ Object
Get the collection count
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/uchi/pagy/toolbox/paginators/offset.rb', line 21 def get_count(collection, ) return collection.count unless defined?(::ActiveRecord) && collection.is_a?(::ActiveRecord::Relation) count = if [:count_over] && !collection.group_values.empty? # COUNT(*) OVER () sql = Arel.star.count.over(Arel::Nodes::Grouping.new([])) collection.unscope(:order).pick(sql).to_i else collection.count(:all) end count.is_a?(Hash) ? count.size : count end |
.paginate(context, collection, **options) ⇒ Object
Return instance and page of results
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/uchi/pagy/toolbox/paginators/offset.rb', line 9 def paginate(context, collection, **) context.instance_eval do request = Request.new([:request] || self.request, ) [:page] ||= request.resolve_page() [:limit] = request.resolve_limit() [:count] ||= collection.instance_of?(Array) ? collection.size : OffsetPaginator.get_count(collection, ) pagy = Offset.new(**, request:) [pagy, collection.instance_of?(Array) ? collection[pagy.offset, pagy.limit] : pagy.records(collection)] end end |