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

Disallow creation of Spree::Price records with empty values #12126

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixed
  • Loading branch information
damianlegawiec committed Aug 20, 2024
commit 5aa9dc95280224dc8f02e1ec62acfca905467f94
11 changes: 9 additions & 2 deletions core/app/models/spree/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ class Price < Spree::Base
before_validation :ensure_currency
before_save :remove_compare_at_amount_if_equals_amount

validates :amount, allow_nil: -> { Spree::RuntimeConfig.allow_empty_price_amount }, numericality: {
# legacy behavior
validates :amount, allow_nil: true, numericality: {
greater_than_or_equal_to: 0,
less_than_or_equal_to: MAXIMUM_AMOUNT
}
}, if: -> { Spree::RuntimeConfig.allow_empty_price_amount }

# new behavior
validates :amount, allow_nil: false, numericality: {
greater_than_or_equal_to: 0,
less_than_or_equal_to: MAXIMUM_AMOUNT
}, unless: -> { Spree::RuntimeConfig.allow_empty_price_amount }

validates :compare_at_amount, allow_nil: true, numericality: {
greater_than_or_equal_to: 0,
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
allow(Spree::RuntimeConfig).to receive(:allow_empty_price_amount).and_return(true)
end

it { binding.pry; is_expected.to be_valid }
it { is_expected.to be_valid }
end

context 'new behavior' do
Expand Down
Loading