Sha256: 28e2f045c2163eeb9ec104120c891692de104f0cf5390a0c21bf2378588f772e

Contents?: true

Size: 727 Bytes

Versions: 1

Compression:

Stored size: 727 Bytes

Contents

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}")
    if limit
      relation = relation.limit(limit)
    end

    unless options[:nil]
      (column.is_a?(Array) ? column : [column]).each do |c|
        relation = relation.where("#{c} IS NOT NULL")
      end
    end

    if options[:uniq]
      relation.uniq.count(options[:uniq])
    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.1.1 lib/hightop.rb