lib/hightop.rb in hightop-0.1.3 vs lib/hightop.rb in hightop-0.1.4

- old
+ new

@@ -6,12 +6,13 @@ if limit.is_a?(Hash) options = limit limit = nil end + distinct = options[:distinct] || options[:uniq] 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_#{distinct || 'all'} DESC, #{order_str}") if limit relation = relation.limit(limit) end unless options[:nil] @@ -19,14 +20,19 @@ relation = relation.where("#{c} IS NOT NULL") end end if options[:min] - relation = relation.having("COUNT(#{options[:uniq] ? "DISTINCT #{options[:uniq]}" : '*'}) >= #{options[:min].to_i}") + relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{options[:min].to_i}") end - if options[:uniq] - relation.uniq.count(options[:uniq]) + if distinct + # since relation.respond_to?(:distinct) can't be used + if ActiveRecord::VERSION::MAJOR > 3 + relation.distinct.count(distinct) + else + relation.uniq.count(distinct) + end else relation.count end end end