Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Give a colourless goal's ring a real fallback colour (#4032)

jaysbeekay7f7566831153
diff --git a/app/models/goal.rb b/app/models/goal.rb
index 948b57d035..9b77d80abd 100644
--- a/app/models/goal.rb
+++ b/app/models/goal.rb
@@ -746,7 +746,10 @@ class Goal < ApplicationRecord
end
segments = []
- segments << { color: color.presence || "var(--color-blue-500)", amount: filled, id: "saved" } if filled.positive?
+ # A real colour, not a CSS variable: the ring runs this segment through
+ # d3.color, which cannot parse var(...) and left the ring blank (#4030).
+ # The same fallback as Goals::AvatarComponent, so ring and avatar match.
+ segments << { color: color.presence || Goal::COLORS.first, amount: filled, id: "saved" } if filled.positive?
segments << { color: "var(--budget-unused-fill)", amount: rem, id: "unused" } if rem.positive?
segments
end
diff --git a/test/models/goal_test.rb b/test/models/goal_test.rb
index a7c53d441f..0e491315ae 100644
--- a/test/models/goal_test.rb
+++ b/test/models/goal_test.rb
@@ -1261,7 +1261,48 @@ class GoalTest < ActiveSupport::TestCase
assert_not goal.any_consumption?
end
+ # The ring's saved segment goes through d3.color, which cannot parse a CSS
+ # variable; a goal with no colour must still hand it a real colour (#4030).
+ test "a goal without a colour gives its ring a real colour" do
+ goal = ring_goal(color: "#6471eb")
+ goal.update_columns(color: nil)
+
+ saved = Goal.find(goal.id).to_donut_segments_json.find { |segment| segment[:id] == "saved" }
+
+ assert_equal Goal::COLORS.first, saved[:color]
+ assert_match(/\A#\h{6}\z/, saved[:color])
+ end
+
+ test "a goal with a colour keeps it in its ring, and the remaining track is unchanged" do
+ goal = ring_goal(color: "#6471eb")
+
+ segments = Goal.find(goal.id).to_donut_segments_json.index_by { |segment| segment[:id] }
+
+ assert_equal "#6471eb", segments["saved"][:color]
+ assert_equal "var(--budget-unused-fill)", segments["unused"][:color]
+ end
+
+ # One fallback for both: a colourless goal's ring and avatar show the same
+ # colour, so changing either fallback alone fails here.
+ test "a colourless goal's ring and avatar fall back to the same colour" do
+ goal = ring_goal(color: "#6471eb")
+ goal.update_columns(color: nil)
+ goal = Goal.find(goal.id)
+
+ saved = goal.to_donut_segments_json.find { |segment| segment[:id] == "saved" }
+
+ assert_equal Goals::AvatarComponent.new(goal: goal).color, saved[:color]
+ end
+
private
+ # A goal with something saved and something left, so both segments exist.
+ def ring_goal(color:)
+ account = Account.create!(family: @family, accountable: Depository.new, name: "Ring savings", currency: "USD", balance: 2_000)
+ @family.goals.create!(name: "Ring goal", target_amount: 5_000, currency: "USD", color: color) do |g|
+ g.goal_accounts.build(account: account)
+ end
+ end
+
# 5,000 saved, 2,000 of it since spent on the thing itself.
def spent_goal