Sha256: 1dac3ff059cc6f05c50f2cb880a34fd6bfe14dad233407fe32f05b755d5ce7fb

Contents?: true

Size: 797 Bytes

Versions: 3

Compression:

Stored size: 797 Bytes

Contents

# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/arel
# frozen_string_literal: true

class Pagy # :nodoc:
  # Better performance of grouped ActiveRecord collections
  module ArelExtra
    private

    # Return Pagy object and paginated collection/results
    def pagy_arel(collection, **vars)
      vars[:count] ||= pagy_arel_count(collection)
      pagy(collection, **vars)
    end

    # Count using Arel when grouping
    def pagy_arel_count(collection)
      if collection.group_values.empty?
        # COUNT(*)
        collection.count(:all)
      else
        # COUNT(*) OVER ()
        sql = Arel.star.count.over(Arel::Nodes::Grouping.new([]))
        collection.unscope(:order).limit(1).pluck(sql).first.to_i
      end
    end
  end
  Backend.prepend ArelExtra
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagy-9.0.5 lib/pagy/extras/arel.rb
pagy-9.0.4 lib/pagy/extras/arel.rb
pagy-9.0.3 lib/pagy/extras/arel.rb