Skip to content

Fix new-variant admin form when master variant has no default price - #6576

Open
wakqasahmed wants to merge 2 commits into
solidusio:mainfrom
wakqasahmed:fix/issue-4831-variant-new-no-master-price
Open

Fix new-variant admin form when master variant has no default price#6576
wakqasahmed wants to merge 2 commits into
solidusio:mainfrom
wakqasahmed:fix/issue-4831-variant-new-no-master-price

Conversation

@wakqasahmed

Copy link
Copy Markdown
Contributor

Fixes #4831.

Spree::Admin::VariantsController#new_before unconditionally clones the master variant's default_price onto the new variant. When the master variant has no default price (for example it was deleted), default_price is nil and calling .attributes on it raises, so opening the new-variant admin page for that product crashes.

This builds a blank price in the store's default currency instead when the master has none, the same way Spree::Variant#default_price_or_build already handles the same situation elsewhere in the codebase.

Testing

  • bundle exec rspec spec/controllers/spree/admin/variants_controller_spec.rb — 7 examples, 0 failures
  • bundle exec standardrb backend/app/controllers/spree/admin/variants_controller.rb backend/spec/controllers/spree/admin/variants_controller_spec.rb — no offenses

Ran both in a plain ruby:3.4-slim-bookworm container against the backend gem, sqlite test DB.

Fixes solidusio#4831.

Spree::Admin::VariantsController#new_before unconditionally cloned the
master variant's default_price into the new variant. When the master
variant has no default price (e.g. it was removed), default_price is
nil and .attributes on it raised, crashing the new-variant admin page.

Build a blank price in the store's default currency instead when the
master has none, matching how Spree::Variant#default_price_or_build
already handles this elsewhere.
@wakqasahmed
wakqasahmed requested a review from a team as a code owner September 3, 2026 10:19
@github-actions github-actions Bot added the changelog:solidus_backend Changes to the solidus_backend gem label Sep 3, 2026
@codecov

codecov Bot commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.26%. Comparing base (4523408) to head (0e11f9d).
⚠️ Report is 24 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6576   +/-   ##
=======================================
  Coverage   92.26%   92.26%           
=======================================
  Files        1037     1037           
  Lines       21228    21243   +15     
=======================================
+ Hits        19586    19600   +14     
- Misses       1642     1643    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +18 to +20
# Shallow Clone of the default price to populate the price field, or
# build a blank one in the store's default currency when the master
# variant has no default price (e.g. it was removed).

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.

This comment is not necessary.

jarednorman flagged the 3-line comment as unnecessary — the if/else
with the named master_default_price variable already makes the two
branches' intent clear without restating it in prose.
@wakqasahmed

Copy link
Copy Markdown
Contributor Author

Good call — removed the comment, the if/else with the named variable already reads clearly on its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_backend Changes to the solidus_backend gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy backend variant creation errors when master has no price

2 participants