Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of queries used to build ministers index links #9418

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module PublishingApi
class MinistersIndexEnableReshufflePresenter < MinistersIndexPresenter
include GovspeakHelper

def links
{
ordered_cabinet_ministers: [],
Expand Down
61 changes: 22 additions & 39 deletions app/presenters/publishing_api/ministers_index_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module PublishingApi
class MinistersIndexPresenter
include GovspeakHelper

attr_accessor :update_type

def initialize(update_type: nil)
Expand Down Expand Up @@ -61,55 +59,40 @@ def details
end

def ordered_cabinet_ministers_content_ids
ministers = MinisterialRole
.cabinet
.occupied
.where(cabinet_member: true)
.map(&:current_role_appointment)
.map(&:person)
.uniq

sorted_ministers = ministers.sort_by do |person|
[person.current_roles.map(&:seniority).min, person.sort_key]
end

sorted_ministers.map(&:content_id)
Person.joins(role_appointments: :role)
.where(role_appointments: { ended_at: nil })
.where(role: { type: "MinisterialRole", cabinet_member: true })
.order("role.seniority")
.distinct
.pluck(:content_id)
end

def ordered_also_attends_cabinet_content_ids
ministers = MinisterialRole
.also_attends_cabinet
.occupied
.map(&:current_role_appointment)
.map(&:person)
.uniq

sorted_ministers = ministers.sort_by do |person|
[person.current_roles.map(&:seniority).min, person.sort_key]
end

sorted_ministers.map(&:content_id)
Person.joins(role_appointments: :role)
.where(role_appointments: { ended_at: nil })
.where(role: { type: "MinisterialRole" })
.where("role.attends_cabinet_type_id IS NOT NULL")
.order("role.seniority")
.distinct
.pluck(:content_id)
end

def ordered_ministerial_departments_content_ids
Organisation.ministerial_departments
.joins(:ministerial_roles)
.excluding_govuk_status_closed
.with_translations_for(:ministerial_roles)
.includes(ministerial_roles: [:current_people])
.order("organisations.ministerial_ordering, organisation_roles.ordering")
.uniq
.map(&:content_id)
.distinct
.pluck(:content_id)
end

def ordered_whips_content_ids(whip_type)
Role
.whip
.where(whip_organisation_id: whip_type.id)
.occupied
.order(:whip_ordering)
.map(&:current_role_appointment)
.map(&:person)
.map(&:content_id)
Person.joins(role_appointments: :role)
.where(role: { whip_organisation_id: whip_type.id })
.where(role_appointments: { ended_at: nil })
.order("role.whip_ordering")
.distinct
.pluck(:content_id)
end
end
end