Sha256: f0b4f6d07d44c8e997155d67dec45fe70c26ca58063e6454214b9b8f79cb92ba

Contents?: true

Size: 1.22 KB

Versions: 1

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::CollectionAssociation.send(:include, BetterAr::AssociationCollection)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
better_ar-0.1.0 lib/better_ar/association_collection.rb