Sha256: b5d156f9e948e1098f86d7a208931e892df802b0ffe2610a9d463804d5623d77

Contents?: true

Size: 742 Bytes

Versions: 7

Compression:

Stored size: 742 Bytes

Contents

# frozen_string_literal: true

module Ibrain
  module Loaders
    class AssociationCountLoader < GraphQL::Batch::Loader
      def initialize(model, association_name)
        super()
        @model = model
        @association_name = association_name
      end

      def perform(records)
        reflection = @model.reflect_on_association(@association_name)
        reflection.check_preloadable!

        klass = reflection.klass
        field = reflection.join_primary_key
        counts = klass.unscoped.where(field => records).group(field).count

        records.each do |record|
          record_key = record[reflection.active_record_primary_key]
          fulfill(record, counts[record_key] || 0)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ibrain-core-0.5.15 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.13 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.12 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.10 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.9 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.8 app/graphql/ibrain/loaders/association_count_loader.rb
ibrain-core-0.5.7 app/graphql/ibrain/loaders/association_count_loader.rb