lib/hightop/kicks.rb in hightop-0.2.3 vs lib/hightop/kicks.rb in hightop-0.2.4
- old
+ new
@@ -1,23 +1,35 @@
module Hightop
module Kicks
def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil)
+ warn "[hightop] uniq is deprecated. Use distinct instead" if uniq
+
+ columns = column.is_a?(Array) ? column : [column]
+ columns.each { |c| Utils.validate_column(c) }
+
distinct ||= uniq
- order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
- relation = group(column).order(["count_#{distinct || 'all'} DESC", order_str])
+ 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)
- (column.is_a?(Array) ? column : [column]).each do |c|
+ columns.each do |c|
+ c = Utils.resolve_column(self, c)
relation = relation.where("#{c} IS NOT NULL")
end
end
if min
- relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{min.to_i}")
+ 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