Sha256: e16d22058684539431c96ecd00b5347e74e4903d27511531f09218605ddade5c

Contents?: true

Size: 837 Bytes

Versions: 2

Compression:

Stored size: 837 Bytes

Contents

module BetterAr
  module Calculations
    # Does a count on the query
    #
    # example:
    #  User.count(:age => 20)
    #
    # is the same as:
    #  User.where(:age => 20).count
    #
    # if the key :conditions is present it will fall back to legacy
    #
    # @param [Hash]
    #   Optional follows same convention as {#all}
    # @return [Integer]
    def count(column_name = nil, count_options = {})
      if column_name.is_a?(Hash)
        count_options = column_name
        column_name = nil
      end

      if count_options.empty?
       super
      elsif count_options.key?(:conditions)
        super(column_name, count_options)
      else
        all(count_options).count
      end
    end
  end
end

ActiveSupport.on_load(:active_record) do
  class << ActiveRecord::Base
    prepend BetterAr::Calculations
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
better_ar-0.2.1 lib/better_ar/calculations.rb
better_ar-0.2.0 lib/better_ar/calculations.rb