Class: Ibrain::Loaders::CountLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
app/graphql/ibrain/loaders/count_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, field) ⇒ CountLoader

Returns a new instance of CountLoader.



6
7
8
9
10
# File 'app/graphql/ibrain/loaders/count_loader.rb', line 6

def initialize(model, field)
  super()
  @model = model
  @field = field
end

Instance Method Details

#perform(ids) ⇒ Object



12
13
14
15
16
17
# File 'app/graphql/ibrain/loaders/count_loader.rb', line 12

def perform(ids)
  counts = @model.unscoped.where(@field => ids).group(@field).count

  counts.each { |id, count| fulfill(id, count) }
  ids.each { |id| fulfill(id, 0) unless fulfilled?(id) }
end