lib/pact_broker/domain/pacticipant.rb in pact_broker-2.92.0 vs lib/pact_broker/domain/pacticipant.rb in pact_broker-2.93.0
- old
+ new
@@ -1,16 +1,46 @@
require "pact_broker/db"
require "pact_broker/messages"
require "pact_broker/repositories/helpers"
-require "pact_broker/versions/latest_version"
require "pact_broker/domain/label"
require "pact_broker/string_refinements"
require "pact_broker/pacticipants/generate_display_name"
module PactBroker
module Domain
+ class LatestVersionForPacticipantEagerLoader
+ def self.call(eo, **_other)
+ populate_associations(eo[:rows])
+ end
+
+ def self.populate_associations(pacticipants)
+ pacticipants.each { | pacticipant | pacticipant.associations[:latest_version] = nil }
+ pacticipant_ids = pacticipants.collect(&:id)
+
+ max_orders = PactBroker::Domain::Version
+ .where(pacticipant_id: pacticipant_ids)
+ .select_group(:pacticipant_id)
+ .select_append { max(order).as(latest_order) }
+
+ max_orders_join = {
+ Sequel[:max_orders][:latest_order] => Sequel[:versions][:order],
+ Sequel[:max_orders][:pacticipant_id] => Sequel[:versions][:pacticipant_id]
+ }
+
+ latest_versions = PactBroker::Domain::Version
+ .select_all_qualified
+ .join(max_orders, max_orders_join, { table_alias: :max_orders})
+
+ latest_versions.each do | version |
+ pacticipant = pacticipants.find{ | p | p.id == version.pacticipant_id }
+ pacticipant.associations[:latest_version] = version
+ end
+ end
+ end
+
class Pacticipant < Sequel::Model
+
include Messages
include PactBroker::Pacticipants::GenerateDisplayName
using PactBroker::StringRefinements
plugin :insert_ignore, identifying_columns: [:name]
@@ -19,11 +49,15 @@
set_primary_key :id
one_to_many :versions, :order => :order, :reciprocal => :pacticipant
one_to_many :labels, :order => :name, :reciprocal => :pacticipant
one_to_many :pacts
- one_to_one :latest_version, :class => "PactBroker::Versions::LatestVersion", primary_key: :id, key: :pacticipant_id
+ one_to_one :latest_version, :class => "PactBroker::Domain::Version",
+ primary_key: :id, key: :pacticipant_id,
+ dataset: lambda { PactBroker::Domain::Version.where(pacticipant_id: id).order(Sequel.desc(:order)).limit(1) },
+ eager_loader: LatestVersionForPacticipantEagerLoader
+
one_to_many :branch_heads, class: "PactBroker::Versions::BranchHead", primary_key: :id, key: :pacticipant_id
one_to_many :branches, class: "PactBroker::Versions::Branch", primary_key: :id, key: :pacticipant_id
dataset_module do
include PactBroker::Repositories::Helpers
@@ -57,13 +91,9 @@
def before_save
super
self.display_name = generate_display_name(name) if display_name.blank?
self.main_branch = nil if main_branch.blank?
- end
-
- def latest_version
- versions.last
end
def to_s
"Pacticipant: id=#{id}, name=#{name}"
end