Add unified chart option for temperature#240
Conversation
Let users overlay temperature series on the main shot chart while keeping the split chart layout as the default.
📝 WalkthroughWalkthroughImplements a feature enabling users to optionally display temperature data on the main shot chart alongside other variables rather than in a separate chart, controlled by a new Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Controller
participant ShotChart
participant View
rect rgba(100, 200, 100, 0.5)
Note over User,View: Unified Chart Enabled (user.unified_chart = true)
User->>Controller: Load shot with unified_chart: true
Controller->>ShotChart: Render with user.unified_chart?
ShotChart->>ShotChart: shot_chart_data returns all data<br/>(includes temperature)
ShotChart->>ShotChart: temperature_chart_data returns empty
ShotChart->>View: Assign secondary Y-axis for temperature series
View->>View: Render main chart with temperature<br/>(secondary_axis)
View->>View: Skip temperature chart (empty)
end
rect rgba(200, 100, 100, 0.5)
Note over User,View: Unified Chart Disabled (user.unified_chart = false)
User->>Controller: Load shot with unified_chart: false
Controller->>ShotChart: Render with user.unified_chart?
ShotChart->>ShotChart: shot_chart_data excludes temperature keys
ShotChart->>ShotChart: temperature_chart_data returns temperature only
View->>View: Render main chart (no temperature)
View->>View: Render separate temperature chart
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Duplicate per-series chart settings before applying unified chart overrides so temperature axis changes do not leak across requests.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
app/models/shot_chart.rb (1)
54-54: Avoid mutating theChartSettingsentry in place.Line 54 writes back into
settingjust to deriveyAxis. IfChartSettings#for_labelreturns a shared or frozen hash, unified-chart requests can leaksecondary_axisinto later series or raise on mutation. A local flag is safer here.♻️ Proposed change
- setting["secondary_axis"] = true if user&.unified_chart? && label.include?("temperature") + secondary_axis = setting["secondary_axis"] || (user&.unified_chart? && label.include?("temperature")) if setting["comparison"] dash_style = setting["dashed"] ? "DashDot" : "ShortDot" else dash_style = setting["dashed"] ? "Dash" : "Solid" end @@ - yAxis: setting["secondary_axis"] ? 1 : 0 + yAxis: secondary_axis ? 1 : 0 }#!/bin/bash set -euo pipefail for file in $(fd 'chart_settings\.rb$' app lib); do echo "=== $file ===" sed -n '1,220p' "$file" rg -n -C2 'def for_label|freeze|dup|deep_dup|secondary_axis' "$file" doneExpected result:
ChartSettings#for_labelshould return a dup, or callers should avoid mutating the returned hash.Also applies to: 74-74
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/models/shot_chart.rb` at line 54, The code mutates the hash returned by ChartSettings.for_label by setting setting["secondary_axis"] = true, which can leak state or raise if the hash is shared/frozen; instead compute a local flag (e.g., secondary_axis = user&.unified_chart? && label.include?("temperature")) and use that when deriving yAxis (or downstream consumers) without writing back into the returned setting; update occurrences around the setting variable and references at the same pattern (including the other instance at line 74) so callers don’t mutate ChartSettings.for_label results.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@test/models/user_test.rb`:
- Around line 53-58: The test "unified_chart follows the user preference"
currently uses build(:user) which does not persist the unified_chart flag;
change the test to persist the user(s) (e.g., use create(:user) and
create(:user, unified_chart: true) or call user.save and user.reload) so the
assertions exercise the stored DB value for unified_chart?; update references to
build(:user) in that test to create(:user) (or save+reload) and assert against
the persisted record.
---
Nitpick comments:
In `@app/models/shot_chart.rb`:
- Line 54: The code mutates the hash returned by ChartSettings.for_label by
setting setting["secondary_axis"] = true, which can leak state or raise if the
hash is shared/frozen; instead compute a local flag (e.g., secondary_axis =
user&.unified_chart? && label.include?("temperature")) and use that when
deriving yAxis (or downstream consumers) without writing back into the returned
setting; update occurrences around the setting variable and references at the
same pattern (including the other instance at line 74) so callers don’t mutate
ChartSettings.for_label results.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 19d827d9-6c32-49f7-8452-89c5eb763279
📒 Files selected for processing (11)
app/controllers/profiles_controller.rbapp/models/shot_chart.rbapp/models/user.rbapp/views/profiles/_form.html.erbapp/views/shots/compare.html.erbapp/views/shots/show.html.erbdb/migrate/20260311180000_add_unified_chart_to_users.rbdb/schema.rbtest/factories/users.rbtest/models/shot_chart_test.rbtest/models/user_test.rb
| test "unified_chart follows the user preference" do | ||
| user = build(:user) | ||
| assert_not user.unified_chart? | ||
|
|
||
| user = build(:user, unified_chart: true) | ||
| assert user.unified_chart? |
There was a problem hiding this comment.
Persist this flag in the test instead of only building it.
The assert_not user.unified_chart? branch is currently proving the factory default from test/factories/users.rb, not the stored preference this PR adds. A broken DB default or update path would still pass here.
🧪 Suggested change
test "unified_chart follows the user preference" do
- user = build(:user)
- assert_not user.unified_chart?
-
- user = build(:user, unified_chart: true)
- assert user.unified_chart?
+ user = User.create!(attributes_for(:user).except(:unified_chart))
+ assert_not user.reload.unified_chart?
+
+ user.update!(unified_chart: true)
+ assert user.reload.unified_chart?
end🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@test/models/user_test.rb` around lines 53 - 58, The test "unified_chart
follows the user preference" currently uses build(:user) which does not persist
the unified_chart flag; change the test to persist the user(s) (e.g., use
create(:user) and create(:user, unified_chart: true) or call user.save and
user.reload) so the assertions exercise the stored DB value for unified_chart?;
update references to build(:user) in that test to create(:user) (or save+reload)
and assert against the persisted record.
Resolves #238
Let users overlay temperature series on the main shot chart while keeping the split chart layout as the default.
Summary by CodeRabbit
New Features
Tests