Sha256: 01b7b83f641d1e1317851db3e41932e5ae89228d37a0c012babd7c9b71bb5509

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

module BetterAr
  module AssociationCollection

    def self.included(base)
      base.class_eval do
        alias_method_chain :first, :better_ar
        alias_method_chain :count, :better_ar
      end
    end

    # Allows for the same interface as {BetterAr::Relation#first} on association collections
    #
    # example:
    #  User.first.records.first(:level => 2, :order! => :name)
    #
    # @param [Hash]
    #   Optional
    # @returns [ActiveRecord::Base]
    def first_with_better_ar(opts = {})
      if opts.empty?
        first_without_better_ar
      elsif opts.key?(:conditions)
        first_without_better_ar(opts)
      else
        scoped.first(opts)
      end
    end

    # Allows for the same interface as {BetterAr::Relation#count} on association collections
    #
    # example:
    #  User.first.records.count(:level => 2)
    #
    # @param [Hash]
    #   Optional
    # @returns [Integer]
    def count_with_better_ar(opts = {})
      if opts.empty?
        count_without_better_ar
      elsif opts.key?(:conditions)
        count_without_better_ar(opts)
      else
        scoped.count(opts)
      end
    end
  end
end

ActiveRecord::Associations::AssociationCollection.send(:include, BetterAr::AssociationCollection)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
better_ar-0.0.11 lib/better_ar/association_collection.rb
better_ar-0.0.8 lib/better_ar/association_collection.rb
better_ar-0.0.7 lib/better_ar/association_collection.rb