Skip to content

Commit

Permalink
Re-work statistics table to display rows w/ 0 values
Browse files Browse the repository at this point in the history
  • Loading branch information
Lubosky committed Sep 12, 2024
1 parent 5aea8b6 commit 1d92bb1
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions app/search/nomination_stats_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,13 +30,39 @@ 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

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

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

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

if ordered_by
_column, order = params[:sort].split(".")
desc = order == "desc"
scope = sort_by_ceremonial_county_name(scope, desc)
end

@search_results = scope.with(data: partial_results)
.select(%Q{
ceremonial_counties.id AS ceremonial_county_id,
ceremonial_counties.name AS ceremonial_county_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 +78,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("ceremonial_county_name #{sort_order(desc)}")
end

end

0 comments on commit 1d92bb1

Please sign in to comment.