Skip to content

Commit

Permalink
Merge pull request #727 from bitzesty/staging
Browse files Browse the repository at this point in the history
Production < staging
  • Loading branch information
TheDancingClown authored Sep 17, 2024
2 parents 6a1dc36 + 1c723bd commit 99817ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
38 changes: 31 additions & 7 deletions app/search/nomination_stats_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class NominationStatsSearch < Search
]

FETCH_QUERY = %Q{
ceremonial_counties.id AS ceremonial_county_id,
CASE WHEN ceremonial_counties.name IS NULL THEN 'Not assigned' ELSE ceremonial_counties.name END AS ceremonial_county_name,
#{TRACKED_STATES.map { |s| "COUNT(CASE WHEN form_answers.state = '#{s}' THEN 1 END) AS #{s}_count" }.join(',')},
COUNT(CASE WHEN form_answers.state IN (#{TRACKED_STATES.map { |s| "'#{s}'" }.join(',')}) THEN 1 END) AS total_count
}.squish.freeze

def self.default_search
{
sort: "ceremonial_county_name",
sort: "name",
search_filter: {
year: "all_years",
assigned_ceremonial_county: FormAnswerStatus::AdminFilter.values('assigned county')
Expand All @@ -29,13 +30,36 @@ def self.default_search
end

def results
super
@search_results = scope

@search_results = @search_results
if !!filter_params[:year].presence
@search_results = filter_by_year(@search_results, filter_params[:year].presence)
end

cte_query = @search_results
.select(FETCH_QUERY)
.left_joins(:ceremonial_county)
.group("ceremonial_counties.name")
.group("ceremonial_counties.id, ceremonial_counties.name")

base_query = CeremonialCounty.select(:id, :name)
base_scope = base_query.from("(#{base_query.to_sql} UNION ALL (SELECT NULL AS id, 'Not assigned' AS name)) AS ceremonial_counties")

if !!filter_params[:assigned_ceremonial_county].presence
base_scope = filter_by_assigned_ceremonial_county(base_scope, filter_params[:assigned_ceremonial_county].presence)
end

desc = params.fetch(:sort, "").split(".")&.last == "desc"
base_scope = sort_by_ceremonial_county_name(base_scope, desc)

@search_results = base_scope.with(data: cte_query)
.select(%Q{
ceremonial_counties.id,
ceremonial_counties.name,
#{TRACKED_STATES.map { |s| "COALESCE(data.#{s}_count, 0) AS #{s}_count" }.join(',')},
COALESCE(data.total_count, 0) AS total_count
}.squish)
.joins("LEFT OUTER JOIN data ON data.ceremonial_county_id = ceremonial_counties.id")

@search_results
end

Expand All @@ -51,11 +75,11 @@ def filter_by_assigned_ceremonial_county(scoped_results, value)
value = value.map do |v|
v == "not_assigned" ? nil : v
end
scoped_results.where(ceremonial_county_id: value)

scoped_results.where(id: value)
end

def sort_by_ceremonial_county_name(scoped_results, desc = false)
scoped_results.order("ceremonial_counties.name #{sort_order(desc)}")
scoped_results.order("name #{sort_order(desc)}")
end

end
4 changes: 2 additions & 2 deletions app/views/admin/statistics/nominations/_list.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ div role="region" aria-labelledby="table-list-nomination-statistics-caption" tab
thead.govuk-table__head
tr.govuk-table__row
th.sortable.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right scope="col" rowspan="2"
= sort_link f, 'Lieutenancy', object, :ceremonial_county_name
= sort_link f, 'Lieutenancy', object, :name
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right scope="col" rowspan="2"
| Submitted nominations
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right.text-center scope="col" colspan="4"
Expand Down Expand Up @@ -55,7 +55,7 @@ div role="region" aria-labelledby="table-list-nomination-statistics-caption" tab
- results.each do |row|
tr.govuk-table__row
th.govuk-table__header.kavs-table__header--dense.kavs-table__header--border-right
= row[:ceremonial_county_name]
= row[:name]
td.govuk-table__cell.kavs-table__cell--dense.kavs-table__cell--border-right
= row[:submitted_count]
td.govuk-table__cell.kavs-table__cell--dense.kavs-table__cell--border-right
Expand Down

0 comments on commit 99817ca

Please sign in to comment.