Skip to content

Commit

Permalink
Merge pull request #6 from populist-vote/h/office-subtitles-2
Browse files Browse the repository at this point in the history
add office subtitles
  • Loading branch information
wileymc authored Jun 26, 2024
2 parents f7e2210 + 7689689 commit 08bbe84
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 162 deletions.
2 changes: 1 addition & 1 deletion macros/generate_office_slug.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CASE -- test election_scope
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'hospital'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', hospital_district, ' ',{{ district }}, ' ', {{ seat }} ))
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ hospital_district }}, ' ',{{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'soil_and_water'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} ))
Expand Down
224 changes: 137 additions & 87 deletions macros/generate_office_subtitle.sql
Original file line number Diff line number Diff line change
@@ -1,97 +1,147 @@
{% macro generate_office_subtitle(state, office_name, election_scope, district_type, district, school_district, hospital_district, seat, county, municipality) %}

concat(
CASE WHEN state,
' - ',
office_name,
' - ',
CASE WHEN county is not null THEN concat(county,' County - ') END,
CASE WHEN district is not null THEN concat(district,' - ') END,
CASE
WHEN seat is NULL THEN ''
WHEN seat ILIKE 'At Large' THEN concat(seat,' - ')
ELSE concat(seat,' - ')
END,
CASE WHEN is_special_election = TRUE THEN 'Special Election - ' ELSE '' END,
CASE
WHEN race_type = 'primary' THEN concat(
'Primary - ', party
)
WHEN race_type = 'general' THEN 'General'
ELSE '' END,
' - ',
'2024' -- TODO: Update this to be dynamic
) AS title,






{% macro generate_office_subtitle(office_name, election_scope, district_type, district, school_district, hospital_district, seat, county, municipality) %}

CASE -- test election_scope
WHEN {{ election_scope }} = 'state' THEN
CASE
WHEN {{ office_name }} = 'U.S. Senate' OR ({{ district }} IS null AND {{ seat }} IS null) THEN
-- use full state name when there is no district or seat, or if U.S. Senate
-- SELECT name FROM us_states WHERE code ILIKE 'MN'
'Minnesota'
WHEN {{ seat }} ILIKE '%At Large%' THEN
concat('MN', ' - ', {{ seat }})
ELSE
concat('MN', ' - Seat ', {{ seat }})
END
WHEN {{ election_scope }} = 'county' THEN
CASE
WHEN {{ district }} IS null THEN
concat({{ county }}, ' County, ', 'MN')
ELSE
concat({{ county }}, ' County, ', 'MN', ' - District ', {{ district }})
END
WHEN {{ election_scope }} = 'city' THEN
CASE -- this list of municipalities have dupes so must add the county
WHEN {{ municipality }} IN ('Beaver Township', 'Becker Township', 'Clover Township',
'Cornish Township', 'Fairview Township', 'Hillman Township',
'Lawrence Township', 'Long Lake Township', 'Louisville Township',
'Moose Lake Township', 'Stokes Township', 'Twin Lakes Township') THEN
concat(
{{ municipality }},
' - ',
{{ county }},
' County, ',
'MN',
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
ELSE
concat(
{{ municipality }},
', ',
'MN',
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
END
WHEN {{ election_scope }} = 'district' THEN
CASE -- test district_types
WHEN {{ district_type }} = 'us_congressional' THEN concat('MN', ' - District ', {{ district }})
WHEN {{ district_type }} = 'state_house' THEN concat('MN', ' - House District ', {{ district }})
WHEN {{ district_type }} = 'state_senate' THEN concat('MN', ' - Senate District ', {{ district }})
WHEN {{ district_type }} = 'county' THEN concat({{ county }}, ' County, ', 'MN', ' - District ', {{ district }})

WHEN {{ district_type }} = 'city' THEN
CASE -- any numbers mean add 'District' before it, otherwise add whatever is in District (no offices with seats here)
WHEN {{ district }} ~ '^\d+$' THEN concat({{ municipality }}, ', ', 'MN', ' - District ', {{ district }})
ELSE concat({{ municipality }}, ', ', 'MN', ' - ', {{ district }})
END

WHEN {{ district_type }} = 'school' THEN
CASE -- tests if District exists, and then tests if Seat exists for each case
WHEN {{ district }} IS null THEN
-- e.g. "MN - ISD #508 - At Large"
concat(
'MN',
' - ',
{{ school_district }},
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
WHEN {{ district }} ~ '^\d+$' THEN
-- e.g. "MN - ISD #709 - District 3"
concat(
'MN',
' - ',
{{ school_district }},
' - District ',
{{ district }},
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
ELSE
-- e.g. "MN - ISD #2365 - Gibbon District"
concat(
'MN',
' - ',
{{ school_district }},
' - ',
{{ district }},
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
END

WHEN {{ district_type }} = 'judicial' THEN concat('MN', ' - Seat ', {{ seat }})

DECLARE @subtitle AS TEXT = ''
WHEN {{ district_type }} = 'hospital' THEN
CASE
WHEN {{ district }} IS null THEN
-- e.g. "Canby Community - At Large"
concat(
{{ hospital_district }},
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
WHEN {{ district }} ~ '^\d+$' THEN
-- e.g. "Cook County - District 1"
concat(
{{ hospital_district }},
' - District ',
{{ district }},
CASE
WHEN {{ seat }} IS null THEN ''
WHEN {{ seat }} ILIKE '%At Large%' THEN ' - ' || {{ seat }}
ELSE ' - Seat ' || {{ seat }}
END
)
END

CASE -- test election_scope
-- state
WHEN {{ election_scope }} = 'state'
THEN
CASE
WHEN {{ district }} is null AND {{ seat }} is null -- ignore for U.S. Senate, seat is used for Class
THEN
concat(@subtitle, 'Minnesota') -- use full state name
WHEN {{ district_type }} = 'soil_and_water' THEN concat({{ county }}, ' County, ', 'MN', ' - District ', {{ district }})

ELSE
CASE
WHEN {{ district }} = number
THEN
concat(@subtitle, )
-- if (district and seat = null then use full state name
-- else use two letter abbreviation of state
-- if district is just a number add 'District' before
-- if name is State House add 'House District' before
-- if name is State Senate add 'Senate District' before
-- districts and seats
-- hospital district, judges, justices, school board
-- maybe city council
concat({{ state }}, ' - ', {{ district }}, ' ', {{ seat }}))
-- county
WHEN {{ election_scope }} = 'county'
THEN
slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} ))
-- city
WHEN {{ election_scope }} = 'city'
THEN
slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', REPLACE({{ municipality }},'Township','Twp'), ' ', {{ county }}, ' county ', {{ school_district }}, ' ', {{ district }}, ' ', {{ seat }} ))
-- district
WHEN {{ election_scope }} = 'district'
THEN
CASE -- test district_types
WHEN {{ district_type }} = 'county'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'city'
THEN
slugify(concat('mn', ' ', REPLACE({{ office_name }},'&','and'), ' ', REPLACE({{ municipality }},'Township','Twp'), ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'school'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ school_district }}, ' ', {{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'judicial'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'hospital'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', hospital_district, ' ',{{ district }}, ' ', {{ seat }} ))
WHEN {{ district_type }} = 'soil_and_water'
THEN
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ county }}, ' county ', {{ district }}, ' ', {{ seat }} ))
ELSE -- us_congressional, state_house, state_senate
slugify(concat('mn', ' ', {{ office_name }}, ' ', {{ district }}, ' ', {{ seat }} ))
null
END
ELSE -- national
''
END

ELSE -- national
null
END

{% endmacro %}
Loading

0 comments on commit 08bbe84

Please sign in to comment.