Sha256: 175b998502757a38f9b45b3a719f97f0904da53dd9e9ec1eac9048008ee1b83a

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module PactBroker
  module Tags
    module EagerLoaders
      class HeadTag
        def self.call(eo, **_other)
          initialize_association(eo[:rows])
          populate_associations(eo[:rows])
        end

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

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

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

        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

        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

        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
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pact_broker-2.79.1 lib/pact_broker/tags/eager_loaders.rb
pact_broker-2.79.0 lib/pact_broker/tags/eager_loaders.rb
pact_broker-2.78.1 lib/pact_broker/tags/eager_loaders.rb
pact_broker-2.78.0 lib/pact_broker/tags/eager_loaders.rb
pact_broker-2.77.0 lib/pact_broker/tags/eager_loaders.rb