Module: Uchi::Pagy::OffsetPaginator

Defined in:
lib/uchi/pagy/toolbox/paginators/offset.rb

Class Method Summary collapse

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, options)
  return collection.count unless defined?(::ActiveRecord) && collection.is_a?(::ActiveRecord::Relation)

  count = if options[: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, **options)
  context.instance_eval do
    request = Request.new(options[:request] || self.request, options)
    options[:page]  ||= request.resolve_page(options)
    options[:limit]   = request.resolve_limit(options)
    options[:count] ||= collection.instance_of?(Array) ? collection.size : OffsetPaginator.get_count(collection, options)
    pagy = Offset.new(**options, request:)
    [pagy, collection.instance_of?(Array) ? collection[pagy.offset, pagy.limit] : pagy.records(collection)]
  end
end