lib/hightop.rb in hightop-0.1.2 vs lib/hightop.rb in hightop-0.1.3
- old
+ new
@@ -1,18 +1,17 @@
require "hightop/version"
require "active_record"
module Hightop
-
def top(column, limit = nil, options = {})
if limit.is_a?(Hash)
options = limit
limit = nil
end
order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
- relation = group(column).order("count_#{options[:uniq] || "all"} DESC, #{order_str}")
+ relation = group(column).order("count_#{options[:uniq] || 'all'} DESC, #{order_str}")
if limit
relation = relation.limit(limit)
end
unless options[:nil]
@@ -20,18 +19,17 @@
relation = relation.where("#{c} IS NOT NULL")
end
end
if options[:min]
- relation = relation.having("COUNT(*) >= #{options[:min].to_i}")
+ relation = relation.having("COUNT(#{options[:uniq] ? "DISTINCT #{options[:uniq]}" : '*'}) >= #{options[:min].to_i}")
end
if options[:uniq]
relation.uniq.count(options[:uniq])
else
relation.count
end
end
-
end
ActiveRecord::Base.send :extend, Hightop