lib/hightop.rb in hightop-0.2.0 vs lib/hightop.rb in hightop-0.2.1

- old
+ new

@@ -1,37 +1,11 @@ +# dependencies +require "active_support" + +# modules +require "hightop/enumerable" +require "hightop/kicks" require "hightop/version" -require "active_record" -module Hightop - def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil) - distinct ||= uniq - order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column - relation = group(column).order("count_#{distinct || 'all'} DESC, #{order_str}") - 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| - relation = relation.where("#{c} IS NOT NULL") - end - end - - if min - relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{min.to_i}") - end - - 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 +ActiveSupport.on_load(:active_record) do + extend Hightop::Kicks end - -ActiveRecord::Base.send :extend, Hightop