Sha256: dc7b4723b4e935dbf5e33c4d78b7ba5ec9bd3978c518eb5ebcda2994a1a86245

Contents?: true

Size: 1013 Bytes

Versions: 3

Compression:

Stored size: 1013 Bytes

Contents

module Hightop
  module Kicks
    def top(column, limit = nil, distinct: nil, min: nil, nil: nil)
      columns = column.is_a?(Array) ? column : [column]
      columns = columns.map { |c| Utils.validate_column(c) }

      distinct = Utils.validate_column(distinct) if distinct

      relation = group(*columns).order("1 DESC", *columns)
      if limit
        relation = relation.limit(limit)
      end

      # terribly named option
      unless binding.local_variable_get(:nil)
        columns.each do |c|
          c = Utils.resolve_column(self, c)
          relation = relation.where("#{c} IS NOT NULL")
        end
      end

      if min
        if distinct
          d = Utils.resolve_column(self, distinct)
          relation = relation.having("COUNT(DISTINCT #{d}) >= #{min.to_i}")
        else
          relation = relation.having("COUNT(*) >= #{min.to_i}")
        end
      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.5.0 lib/hightop/kicks.rb
hightop-0.4.0 lib/hightop/kicks.rb
hightop-0.3.0 lib/hightop/kicks.rb