Module: Pagy::Countable

Defined in:
lib/pagy/modules/abilities/countable.rb

Overview

Provide the helpers to count a collection

Class Method Summary collapse

Class Method Details

.get_count(collection, options) ⇒ Object

Get the collection count



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pagy/modules/abilities/countable.rb', line 9

def get_count(collection, options)
  return collection.size if collection.instance_of?(Array)
  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