Skip to content

Add nil guard and zero default to downloads by season#1452

Merged
radical-ube merged 4 commits intomainfrom
fix/downloads_by_season_nil_bug
Mar 16, 2026
Merged

Add nil guard and zero default to downloads by season#1452
radical-ube merged 4 commits intomainfrom
fix/downloads_by_season_nil_bug

Conversation

@radical-ube
Copy link
Copy Markdown
Contributor

aims to fix a bug where the seasons chart crashes when trying to sort nil query return.

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realized this didn't make it into en.yml

end

def downloads_by_season(season_number:)
return nil unless published_seasons.include?(season_number)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastly, if the query returns no results, return the default.

caption: Caption
credit: Credit
metrics:
chart:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i realized i missed this chunk of previously used labels that i forgot to remove during the design switch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Always tricky to track down unused translations.

Copy link
Copy Markdown
Member

@cavis cavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 looks good to me!

caption: Caption
credit: Credit
metrics:
chart:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Always tricky to track down unused translations.

@radical-ube radical-ube merged commit c69e778 into main Mar 16, 2026
3 checks passed
@radical-ube radical-ube deleted the fix/downloads_by_season_nil_bug branch March 16, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants