Class: BatchLoaders::AwardEmojiVotesBatchLoader

Inherits:
Object
  • Object
show all
Defined in:
app/graphql/batch_loaders/award_emoji_votes_batch_loader.rb

Class Method Summary collapse

Class Method Details

.load_downvotes(object, awardable_class: nil) ⇒ Object



9
10
11
# File 'app/graphql/batch_loaders/award_emoji_votes_batch_loader.rb', line 9

def self.load_downvotes(object, awardable_class: nil)
  load_votes_for(object, AwardEmoji::DOWNVOTE_NAME, awardable_class: awardable_class)
end

.load_upvotes(object, awardable_class: nil) ⇒ Object



5
6
7
# File 'app/graphql/batch_loaders/award_emoji_votes_batch_loader.rb', line 5

def self.load_upvotes(object, awardable_class: nil)
  load_votes_for(object, AwardEmoji::UPVOTE_NAME, awardable_class: awardable_class)
end

.load_votes_for(object, vote_type, awardable_class: nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/graphql/batch_loaders/award_emoji_votes_batch_loader.rb', line 13

def self.load_votes_for(object, vote_type, awardable_class: nil)
  awardable_class ||= object.class.name
  batch_key = "#{object.class.base_class.name}-#{vote_type}"

  BatchLoader::GraphQL.for(object.id).batch(key: batch_key) do |ids, loader, _args|
    counts = AwardEmoji.votes_for_collection(ids, awardable_class).named(vote_type).index_by(&:awardable_id)

    ids.each do |id|
      loader.call(id, counts[id]&.count || 0)
    end
  end
end