Give a colourless goal's ring a real fallback colour

Merged
JA
1mo ago
+45-1

Summary

A goal with no colour now gets a progress ring. Its filled segment used to fall back to a CSS variable that the ring's d3 code can't parse, so the ring drew nothing but its label. The design choice: the fallback is Goal::COLORS.first, the colour Goals::AvatarComponent already uses for a goal without one, so a colourless goal's ring and avatar now match.

Closes #4030

Fork pre-flight PR: jaysbeekay/sure#346 (pull/346). Same branch, same commit; its CI and review history are there.

What changed

  • Goal#to_donut_segments_json: the saved segment's fallback changes from "var(--color-blue-500)" to Goal::COLORS.first. That's the whole code change.
  • Why that line: donut_chart_controller#transformRingColor runs every segment except unused/overage through d3.color(color) and then sets .opacity on the result. d3.color("var(--color-blue-500)") is null, so the controller threw TypeError: Cannot set properties of null (setting 'opacity') while connecting, and no arc was drawn.

Deliberately not changed:

  • The controller. With this fix nothing passes it an unparseable colour: the budget donuts' filled segments use Category#color, which is required hex, and their var(...) colours are the unused and overage segments, which skip d3.color. A guard there would fit better with #1899.
  • No backfill of existing rows; the fallback is display-only.
  • color is not made required on goals; that would be a behaviour change for API and console callers.

Demo data

No demo data needed: Demo::Generator gives every goal a colour (Goal::COLORS.sample), so demo goals draw as before.

Screenshots

A goal saved without a colour ($2,000 of $5,000), captured from a system-test session in headless Chromium.

BeforeAfter
before: no ringafter: ring drawn

Migration notes

No migration.

Verification

Tests were written first. On main, two of the three new tests in test/models/goal_test.rb failed with Expected: "#e99537", Actual: "var(--color-blue-500)":

  • "a goal without a colour gives its ring a real colour": the saved colour is Goal::COLORS.first and a hex string.
  • "a colourless goal's ring and avatar fall back to the same colour": the ring's colour equals Goals::AvatarComponent.new(goal:).color.

The third, "a goal with a colour keeps it in its ring, and the remaining track is unchanged", passes before and after, as a guard should.

Breaking the fix on purpose fails a test each time:

BreakageCaught by
CSS variable put backboth fallback tests
A different palette entry (COLORS.second)both fallback tests
Fallback used even when the goal has a colourthe keeps-its-colour test

Commands:

  • bin/rails test on e947ddf3e: 10,798 runs, 46,120 assertions, 0 failures, 0 errors, 33 skips
  • goal model, goals controller and goal component tests: 192 runs, 0 failures
  • bin/rubocop on the changed files: no offenses
  • bin/brakeman --no-pager: 0 warnings
  • erb_lint and npm run lint: not applicable, no ERB or JS changed

Blast radius

  • to_donut_segments_json has one caller, Goals::ProgressRingComponent (progress_ring_component.html.erb).
  • Only the saved segment of a goal with a blank colour changes. A goal with a colour sends exactly what it sent before, and the unused segment keeps var(--budget-unused-fill).
  • Goals::AvatarComponent is unchanged; it's where the fallback comes from, and a test pins the two together.

Summary by CodeRabbit

  • Bug Fixes
    • Goals without a custom color now use the standard goal color for the saved portion of the progress ring, matching the goal’s avatar color. Custom colors remain unchanged.