diff --git a/app/search/nomination_stats_search.rb b/app/search/nomination_stats_search.rb index cd05edb16..015f4189b 100644 --- a/app/search/nomination_stats_search.rb +++ b/app/search/nomination_stats_search.rb @@ -13,6 +13,7 @@ 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 @@ -20,7 +21,7 @@ class NominationStatsSearch < Search def self.default_search { - sort: "ceremonial_county_name", + sort: "name", search_filter: { year: "all_years", assigned_ceremonial_county: FormAnswerStatus::AdminFilter.values('assigned county') @@ -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 @@ -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 diff --git a/app/views/admin/statistics/nominations/_list.html.slim b/app/views/admin/statistics/nominations/_list.html.slim index 62d71c2cb..78f416a78 100644 --- a/app/views/admin/statistics/nominations/_list.html.slim +++ b/app/views/admin/statistics/nominations/_list.html.slim @@ -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" @@ -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