Class: PactBroker::Tags::EagerLoaders::HeadTag

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

Class Method Summary collapse

Class Method Details

.call(eo, **_other) ⇒ Object



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

def self.call(eo, **_other)
  initialize_association(eo[:rows])
  populate_associations(eo[:rows])
end

.group_by_pacticipant_id(tags) ⇒ Object



20
21
22
# File 'lib/pact_broker/tags/eager_loaders.rb', line 20

def self.group_by_pacticipant_id(tags)
  tags.to_a.group_by(&:pacticipant_id)
end

.initialize_association(tags) ⇒ Object



10
11
12
# File 'lib/pact_broker/tags/eager_loaders.rb', line 10

def self.initialize_association(tags)
  tags.each{|tag| tag.associations[:head_tag] = nil }
end

.latest_tags_for_pacticipant_id(pacticipant_id, tag_names, tag_class) ⇒ Object



39
40
41
42
43
# File 'lib/pact_broker/tags/eager_loaders.rb', line 39

def self.latest_tags_for_pacticipant_id(pacticipant_id, tag_names, tag_class)
  tag_class.latest_tags_for_pacticipant_ids_and_tag_names(pacticipant_id, tag_names).each_with_object({}) do | tag, hash |
    hash[[tag.pacticipant_id, tag.name]] = tag
  end
end

.populate_associations(tags) ⇒ Object



14
15
16
17
18
# File 'lib/pact_broker/tags/eager_loaders.rb', line 14

def self.populate_associations(tags)
  group_by_pacticipant_id(tags).each do | pacticipant_id, participant_tags |
    populate_associations_by_pacticipant(pacticipant_id, participant_tags)
  end
end

.populate_associations_by_pacticipant(pacticipant_id, tags) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pact_broker/tags/eager_loaders.rb', line 24

def self.populate_associations_by_pacticipant(pacticipant_id, tags)
  latest_tags_for_tags = latest_tags_for_pacticipant_id(
    pacticipant_id,
    tags.collect(&:name).uniq.compact,
    tags.first.class
  )
  self.populate_tags(tags, latest_tags_for_tags)
end

.populate_tags(tags, latest_tags_for_tags) ⇒ Object



33
34
35
36
37
# File 'lib/pact_broker/tags/eager_loaders.rb', line 33

def self.populate_tags(tags, latest_tags_for_tags)
  tags.each do | tag |
    tag.associations[:head_tag] = latest_tags_for_tags[[tag.pacticipant_id, tag.name]]
  end
end