Sha256: 0b4e53ab3e563466a92937b86c20eb2697624ae36f3eee5edb12fa73235a1b8b

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

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
end

ActiveRecord::Base.send :extend, Hightop

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hightop-0.2.0 lib/hightop.rb