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

Do not allow prices with nil amount #3987

Merged
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
2 changes: 1 addition & 1 deletion core/app/models/spree/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Price < Spree::Base
delegate :tax_rates, to: :variant

validate :check_price
validates :amount, allow_nil: true, numericality: {
validates :amount, numericality: {
greater_than_or_equal_to: 0,
less_than_or_equal_to: MAXIMUM_AMOUNT
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ChangeColumnNullOnPrices < ActiveRecord::Migration[5.2]
def change
change_column_null(:spree_prices, :amount, false)
end
end
13 changes: 13 additions & 0 deletions core/lib/tasks/migrations/delete_prices_with_nul_amount.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

namespace :solidus do
namespace :migrations do
namespace :delete_prices_with_nul_amount do
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved
task up: :environment do
print "Deleting prices wich amount attribute is nil ... "
Spree::Price.where(amount: nil).delete_all
puts "Success"
end
end
end
end
7 changes: 7 additions & 0 deletions core/lib/tasks/upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ namespace :solidus do
] do
puts "Your Solidus install is ready for Solidus 2.11"
end

desc "Upgrade Solidus to version 3.0"
task three_point_zero: [
'solidus:migrations:delete_prices_with_nul_amount:up',
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved
] do
puts "Your Solidus install is ready for Solidus 3.0"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require 'rails_helper'

path = Spree::Core::Engine.root.join('lib/tasks/migrations/delete_prices_with_nul_amount.rake')
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved

RSpec.describe 'solidus:migrations:delete_prices_with_nul_amount' do
describe 'up' do
include_context(
'rake',
task_path: path,
task_name: 'solidus:migrations:delete_prices_with_nul_amount:up'
)

it 'removes all prices which amount column is NULL' do
price = create(:price)
expect(Spree::Price).to receive(:where).with(amount: nil).and_return(Spree::Price.where(id: price))

task.invoke

expect { price.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
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 @@ -16,7 +16,7 @@

context 'when the amount is nil' do
waiting-for-dev marked this conversation as resolved.
Show resolved Hide resolved
let(:amount) { nil }
it { is_expected.to be_valid }
it { is_expected.not_to be_valid }
end

context 'when the amount is less than 0' do
Expand Down