lib/hightop.rb in hightop-0.0.3 vs lib/hightop.rb in hightop-0.0.4
- old
+ new
@@ -1,11 +1,24 @@
require "hightop/version"
module Hightop
- def top(column, limit = nil)
- column = connection.quote_table_name(column) if column.is_a?(Symbol)
- group(column).where("#{column} IS NOT NULL").order("count_all DESC, #{column}").limit(limit).count
+ 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).limit(limit).order("count_all DESC, #{order_str}")
+
+ unless options[:nil]
+ (column.is_a?(Array) ? column : [column]).each do |c|
+ relation = relation.where("#{c} IS NOT NULL")
+ end
+ end
+
+ relation.count
end
end
ActiveRecord::Base.send :extend, Hightop