Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Spree::Variant inconsistency due to lack of product association #3043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ class Variant < Spree::Base
autosave: true

before_validation :set_cost_currency
before_validation :set_price
before_validation :build_vat_prices, if: -> { rebuild_vat_prices? || new_record? }
before_validation :set_price, if: -> { product && product.master }
before_validation :build_vat_prices, if: -> { rebuild_vat_prices? || new_record? && product }

validates :product, presence: true
validate :check_price
Copy link
Member

Choose a reason for hiding this comment

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

What about adding a presence validation on the product here? There are too many places where Solidus gives for granted that a product should be associated with a variant and I think it could make sense to don't allow saving a variant without a product associated. Also, this way users will have the right validation message attached to the invalid object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, after reviewing the code I'm having second thoughts. How can we validate product presence in variant if product requires a master variant to be present?

The funny thing is that product automatically builds master variant, but I was unable to find build_master method definition. Maybe I'm missing something ☹️

def find_or_build_master
master || build_master
end

Any idea?

Copy link
Member

Choose a reason for hiding this comment

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

build_master is a method coming from has_one association. That variant will be associated with the product automatically so I think it will be valid, even with the proposed product presence validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


validates :cost_price, numericality: { greater_than_or_equal_to: 0, allow_nil: true }
Expand Down Expand Up @@ -418,10 +419,7 @@ def set_master_out_of_stock

# Ensures a new variant takes the product master price when price is not supplied
def set_price
if price.nil? && Spree::Config[:require_master_price] && !is_master?
raise 'No master variant found to infer price' unless product && product.master
self.price = product.master.price
end
self.price = product.master.price if price.nil? && Spree::Config[:require_master_price] && !is_master?
end

def check_price
Expand Down
4 changes: 4 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'rails_helper'

RSpec.describe Spree::Variant, type: :model do
it { is_expected.to be_invalid }

let!(:variant) { create(:variant) }

it_behaves_like 'default_price'
Expand All @@ -22,6 +24,8 @@
expect(variant).to be_valid
variant.product = nil
expect(variant).to be_invalid
variant.price = nil
expect(variant).to be_invalid
end
end

Expand Down