Skip to content

Commit

Permalink
Merge pull request solidusio#3987 from nebulab/waiting-for-dev/dont_a…
Browse files Browse the repository at this point in the history
…llow_nil_amount

Do not allow prices with nil amount
  • Loading branch information
kennyadsl authored Sep 1, 2021
2 parents 311512f + 2982058 commit ea200df
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
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_nil_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_nil_amount do
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 @@ -8,6 +8,13 @@ namespace :solidus do
] do
puts "Your Solidus install is ready for Solidus 3.0"
end

desc "Upgrade Solidus to version 3.0"
task three_point_zero: [
'solidus:migrations:delete_prices_with_nil_amount:up',
] do
puts "Your Solidus install is ready for Solidus 3.0"
end
end

desc "Upgrade to the current Solidus version"
Expand Down
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_nil_amount.rake')

RSpec.describe 'solidus:migrations:delete_prices_with_nil_amount' do
describe 'up' do
include_context(
'rake',
task_path: path,
task_name: 'solidus:migrations:delete_prices_with_nil_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
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

0 comments on commit ea200df

Please sign in to comment.