Sha256: 88b50366bb99c570db18aeb81022f819048eac17a5faaf5d25a5312b9b345c5c

Contents?: true

Size: 830 Bytes

Versions: 3

Compression:

Stored size: 830 Bytes

Contents

module Hightop
  module Kicks
    def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil)
      distinct ||= uniq
      order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
      relation = group(column).order(["count_#{distinct || 'all'} DESC", order_str])
      if limit
        relation = relation.limit(limit)
      end

      # terribly named option
      unless binding.local_variable_get(:nil)
        (column.is_a?(Array) ? column : [column]).each do |c|
          relation = relation.where("#{c} IS NOT NULL")
        end
      end

      if min
        relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{min.to_i}")
      end

      if distinct
        relation.distinct.count(distinct)
      else
        relation.count
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hightop-0.2.3 lib/hightop/kicks.rb
hightop-0.2.2 lib/hightop/kicks.rb
hightop-0.2.1 lib/hightop/kicks.rb