Module: Hightop::Kicks

Defined in:
lib/hightop/kicks.rb

Instance Method Summary collapse

Instance Method Details

#top(column, limit = nil, distinct: nil, min: nil, nil: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hightop/kicks.rb', line 3

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