Sha256: e4565db07f616cf13a6aed3b11cea21e437adeb96a03b029f9c2ec4074786222

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require 'active_record/precount/count_loader_builder'

module ActiveRecord
  module Precount
    module RelationExtension
      def precount(*args)
        check_if_method_has_arguments!(:precount, args)
        spawn.precount!(*args)
      end

      def precount!(*args)
        CountLoaderBuilder.new(klass).build_from_query_methods(*args)

        self.preload_values += args.map { |arg| :"#{arg}_count" }
        self
      end

      def eager_count(*args)
        check_if_method_has_arguments!(:eager_count, args)
        spawn.eager_count!(*args)
      end

      def eager_count!(*args)
        CountLoaderBuilder.new(klass).build_from_query_methods(*args)

        self.eager_load_values += args.map { |arg| :"#{arg}_count" }
        self
      end

      private

      def apply_join_dependency(relation, join_dependency)
        relation = super

        # to count associated records in JOIN query, group scope is necessary
        join_dependency.reflections.each do |reflection|
          if reflection.macro == :count_loader
            ar = reflection.active_record
            relation = relation.group("#{ar.table_name}.#{ar.primary_key}")
          end
        end

        relation
      end
    end
  end

  Relation.prepend(Precount::RelationExtension)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
activerecord-precount-0.7.0 lib/active_record/precount/relation_extension.rb
activerecord-precount-0.7.0.beta1 lib/active_record/precount/relation_extension.rb
activerecord-precount-0.6.3 lib/active_record/precount/relation_extension.rb
activerecord-precount-0.6.2 lib/active_record/precount/relation_extension.rb
activerecord-precount-0.6.1 lib/active_record/precount/relation_extension.rb