Add nil guard and zero default to downloads by season#1452
Conversation
| published_seasons.map do |season| | ||
| downloads_by_season(season_number: season).to_a.flatten | ||
| end.sort { |a, b| b[1] <=> a[1] } | ||
| downloads_by_season(season_number: season).to_a.flatten.presence |
There was a problem hiding this comment.
added .presence to catch empty arrays and revert them to nil, then remove them before sorting.
| end | ||
|
|
||
| def season_label(season_number) | ||
| I18n.t(".helpers.label.metrics.downloads_by_season", season_number: season_number) |
There was a problem hiding this comment.
realized this didn't make it into en.yml
| end | ||
|
|
||
| def downloads_by_season(season_number:) | ||
| return nil unless published_seasons.include?(season_number) |
There was a problem hiding this comment.
first guard to make sure that the season number exists.
| default = {"#{season_label(season_number)}": 0} | ||
|
|
||
| season_episodes_guids = episodes.published.where(season_number: season_number).pluck(:guid) | ||
| return default if season_episodes_guids.blank? |
There was a problem hiding this comment.
set the default at the top, and this guard checks to see if there are any published episodes given this season. if not, then just return the default.
| end.transform_keys { |k| "Season #{season_number}" } | ||
| end | ||
|
|
||
| if results.blank? |
There was a problem hiding this comment.
lastly, if the query returns no results, return the default.
| caption: Caption | ||
| credit: Credit | ||
| metrics: | ||
| chart: |
There was a problem hiding this comment.
i realized i missed this chunk of previously used labels that i forgot to remove during the design switch.
There was a problem hiding this comment.
Good catch! Always tricky to track down unused translations.
| caption: Caption | ||
| credit: Credit | ||
| metrics: | ||
| chart: |
There was a problem hiding this comment.
Good catch! Always tricky to track down unused translations.
aims to fix a bug where the seasons chart crashes when trying to sort nil query return.