Class: PactBroker::Pacts::EagerLoaders::HeadPactPublicationsForTags

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/pacts/eager_loaders.rb

Class Method Summary collapse

Class Method Details

.call(eo) ⇒ Object



5
6
7
8
9
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 5

def self.call(eo)
  pact_publications = eo[:rows]
  initialize_association(pact_publications)
  populate_associations(group_by_consumer_and_provider_ids(pact_publications))
end

.group_by_consumer_and_provider_ids(pact_publications) ⇒ Object



15
16
17
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 15

def self.group_by_consumer_and_provider_ids(pact_publications)
  pact_publications.group_by{ |pact_publication| [pact_publication.consumer_id, pact_publication.provider_id] }
end

.hash_of_head_pact_publications(pact_publication_class, consumer, provider, tag_names) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 43

def self.hash_of_head_pact_publications pact_publication_class, consumer, provider, tag_names
  pact_publication_class
    .for_consumer(consumer)
    .for_provider(provider)
    .latest_for_consumer_tag(tag_names)
    .each_with_object({}) do | head_pact_publication, hash |
      hash[head_pact_publication.values.fetch(:tag_name)] = head_pact_publication
    end
end

.initialize_association(pact_publications) ⇒ Object



11
12
13
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 11

def self.initialize_association(pact_publications)
  pact_publications.each { |pp| pp.associations[:head_pact_publications_for_tags] = [] }
end

.populate_associations(grouped_pact_publications) ⇒ Object



19
20
21
22
23
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 19

def self.populate_associations(grouped_pact_publications)
  grouped_pact_publications.each do | key, pact_publications |
    populate_associations_for_consumer_and_provider(key, pact_publications)
  end
end

.populate_associations_for_consumer_and_provider(_key, pact_publications) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pact_broker/pacts/eager_loaders.rb', line 25

def self.populate_associations_for_consumer_and_provider(_key, pact_publications)
  head_pact_publications_by_tag = hash_of_head_pact_publications(
    pact_publications.first.class,
    pact_publications.first.consumer,
    pact_publications.first.provider,
    pact_publications.flat_map{ |pp| pp.consumer_version_tags.collect(&:name) }
  )

  pact_publications.each do | pact_publication |
    pact_publication.consumer_version_tags.collect(&:name).sort.each do | tag_name |
      # Not sure how this can ever be nil, but a PF error suggests that it has happend. Maybe a timing issue?
      if head_pact_publications_by_tag[tag_name]
        pact_publication.associations[:head_pact_publications_for_tags] << head_pact_publications_by_tag[tag_name]
      end
    end
  end
end